131 lines
3.0 KiB
Markdown
131 lines
3.0 KiB
Markdown
@slide(layout=chapter-slide)
|
|
|
|
@title
|
|
Source code listings
|
|
|
|
@slide(layout=content-and-preview)
|
|
|
|
@title
|
|
Installation <span class="emoji">🖥️</span>
|
|
|
|
@content
|
|
`Minted`, another LaTeX package, is very useful to display source code. It requires the Python programming language.
|
|
|
|
As soon as **[Python is installed](https://www.python.org/)**, we can download the corresponding Python package **[`Pygments`](http://pygments.org/)** using a command prompt:
|
|
|
|
``` {.lang-sh .hljs}
|
|
pip install Pygments
|
|
```
|
|
|
|
Add the LaTeX package and we are good to go:
|
|
|
|
``` {.lang-tex .hljs}
|
|
\usepackage{minted}
|
|
```
|
|
|
|
@todo
|
|
Update source code?
|
|
|
|
@preview
|
|
{ .thin-padding }
|
|
|
|
@slide(layout=content-only)
|
|
|
|
@title
|
|
Compiler settings
|
|
|
|
@content
|
|
<div class="box warning">
|
|
We need to pass the additional flag `--shell-escape` to our compiler in order to use `minted`.
|
|
</div>
|
|
|
|
In TeXstudio, go to **`Options → Configure TeXstudio → Commands`** and add the flag in the PdfLaTeX row before `%.tex`:
|
|
|
|
<code>pdflatex -syntex=1 -interaction=nonstopmode **\-\-shell-escape** %.tex</code>
|
|
|
|
@slide(layout=content-and-preview)
|
|
|
|
@title
|
|
Listings within LaTeX documents
|
|
|
|
@content
|
|
++ Within a dedicated environment:
|
|
|
|
``` {.lang-tex .hljs .fragment}
|
|
\section*{Haskell Magic}
|
|
Squares of all even % …
|
|
\begin{minted}{haskell}
|
|
[x^2 | x <- [1..200], even x]
|
|
\end{minted}
|
|
```
|
|
|
|
++ Directly inline:
|
|
|
|
``` {.lang-tex .hljs .fragment}
|
|
\section*{An HTML Example}
|
|
A headline is denoted the following way:
|
|
\mint{html}|<h2>LaTeX at University</h2>|
|
|
```
|
|
|
|
@preview
|
|

|
|
|
|
@slide(layout=content-and-preview)
|
|
|
|
@title
|
|
External source code
|
|
|
|
@content
|
|
We can avoid redundancy by including source code directly from its source file.
|
|
|
|
``` {.lang-tex .hljs}
|
|
\section*{Simple Java Application}
|
|
\inputminted{java}{Test.java}
|
|
```
|
|
|
|
``` {.lang-java .hljs data-sourcefile="Test.java"}
|
|
public class HelloWorld {
|
|
public static void main(/*…*/) {
|
|
System.out.println(/*…*/);
|
|
}
|
|
}
|
|
```
|
|
|
|
@preview
|
|

|
|
|
|
@slide(layout=content-only)
|
|
|
|
@title
|
|
Configuring `minted`
|
|
|
|
@content
|
|
Optional parameters allow us to activate line numbers, automated line breaks, and syntax highlighting based on numerous color schemes.
|
|
|
|
``` {.lang-tex .hljs}
|
|
\usemintedstyle{monokai}
|
|
\begin{minted}[
|
|
linenos=true,
|
|
breaklines=true,
|
|
]{javascript}
|
|
% ...
|
|
\end{minted}
|
|
```
|
|
|
|
<span class="emoji">🔗</span> **[introduction](https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted)** and **[official documentation](https://ctan.kako-dev.de/macros/latex/contrib/minted/minted.pdf)**
|
|
|
|
@slide(layout=task)
|
|
|
|
@task-number
|
|
9
|
|
|
|
@title
|
|
Listing source code
|
|
|
|
@content
|
|
* Section 2.4 contains two **listings** that are currently commented out.
|
|
* ++ Include the **`minted` package** in the right place.
|
|
* ++ Make use of the **`minted` environment** to activate syntax highlighting for the first listing.
|
|
* ++ Move the second listing to a **new file** and include it using `\inputminted`.
|
|
* ++ Change the **color scheme** to `borland`.
|