0% found this document useful (0 votes)
0 views

Code_Presentations_Example__different_ways__shown_in_Beamer_Metropolis_

The document discusses various methods for displaying code using LaTeX packages, starting from basic to more advanced techniques. It covers environments like exttt{}, verbatim, and listings, highlighting their features and limitations. Additionally, it addresses bugs, code highlighting, and the use of packages like Knitr and minted for enhanced code presentation.

Uploaded by

Mau Ribeiro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Code_Presentations_Example__different_ways__shown_in_Beamer_Metropolis_

The document discusses various methods for displaying code using LaTeX packages, starting from basic to more advanced techniques. It covers environments like exttt{}, verbatim, and listings, highlighting their features and limitations. Additionally, it addresses bugs, code highlighting, and the use of packages like Knitr and minted for enhanced code presentation.

Uploaded by

Mau Ribeiro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Code-Presentations

LATEX Ninja
June 2019
Introduction
Intro i

When I thought about it, I realized there are actually so many ways of
displaying code using LaTeX packages. So I’ll start with the most
basic and then go on to the more advanced ones ;)

1/15
Intro ii

This one isn’t a verbatim way to express code, but it will


texttt change the font to typewriter, so it ’looks like code’. However,
in these short bits of code, you will have to use escape
sequences for reserved characters.
\texttt{}
I make massive use of when ’talking’ about code or, you
know, writing explanatory text sequences. It is especially
useful for bits of code where there is no excessive (or none
at all) use of escape sequences. Then it is a really handy
way to quickly typeset code. Once you have lots of reserved
characters, you might be better of just using this next one.

2/15
Intro iii

This one really is a staple and pretty failsafe, but


The verbatim also doesn’t have code highlighting which you
environment might want in most cases, apart from very short
bits of code where highlighting isn’t important.
begin{verbatim} You can use verbatim as an environment for
multiple lines of code which appear like a quote as
code goes here a separate block in your text. In other cases, where
end{verbatim} you just want but without having to escape
reserved characters, you might want to use the
some code command. You can use any characters
as delimiters to denote beginng and end of code.
So it can also be test. The idea is that you can
choose one which you will not need inside the
code, as not to ’confuse’ the enviroment.

3/15
Bugs

1. listing all the bugs


2. oops, this already is one of the main bugs:
• If you were to use the enumitem package
• you would get a fatal error
• but no output
• due to package conflicts

4/15
Tcblisting
Tcblisting

The code is quite tiny because I set the font size to very small in the
definition of the little myr

Load files
1 # text <- readLines(file.choose())
2 filePath <- "http://www.link.com/a_text.txt"
3 text <- readLines(filePath)

5/15
lstlisting
Using Code Listings

my_ v e c t o r <− c ( ” t e s t i n g ” , ” v e c t o r s ” )
my_ v e c t o r # a t e s t

6/15
Using Code Listings II

This time with caption but without code highlighting.

Listing 1: Hello World! in C


i n t main ( )
{
p r i n t f ( ” H e l l o World ! ” ) ;
return 0;
}

7/15
Code highlighting with listings

Listing 2: A demonstration
import numpy as np

You can also use the inline shorthand for small snippets:
while{$a || $b}

8/15
List of listings

Well, this is how it’s supposed to be, but sadly, using sections inside
frames will add up to this result. – So you can’t use this list of
listings here.

9/15
Importing listing from file

<codelisting ref="#test">
<fun>
<bla>
lalalalala <lb/>
<!-- comment -->
</bla>
</fun>
</codelisting>

This will only be in colour if you use the settings.

10/15
Using Knitr

# Create sequence
my_sequence = 1:5

# use summary function to display stats


summary(my_sequence)

## Min. 1st Qu. Median Mean 3rd Qu. Max.


## 1 2 3 3 4 5

Then output values inside the text: 1, 2, 3, 4, 5.

11/15
Using Knitr with options

# This package (gutenbergr) is not available


# on Overleaf
# So we set KnitR with eval=FALSE
# so it will not evaluate
# and litter our slides with error messages
library(gutenbergr)

# echo=FALSE hides the code but displays the results


# echo=1:3 only displays first three lines
# background=#FFFFFF

12/15
Using minted

int main() {
printf("hello, world");
return 0;
}

Can also typeset $code$ inline. Yay to \LaTeX{}!

13/15
Using minted

There also is the option to include math mode stuff in the comments.

/*
π = limn→∞ Pdn
*/
const double pi = 3.1415926535;

14/15
Using minted with options

1 import numpy as np
2

3 test = 5

15/15
This is it ⌣

15/15

You might also like