@slide(layout=chapter-slide) @title Source code listings @slide(layout=content-and-preview) @title Installation 🖥️ @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} ``` @preview ![](svg/chapter-10/minted-overview-english-crop.svg){ .thin-padding } @slide(layout=content-only) @title Compiler settings @content
We need to pass the additional flag `--shell-escape` to our compiler in order to use `minted`.
In TeXstudio, go to **`Options → Configure TeXstudio → Commands`** and add the flag in the PdfLaTeX row before `%.tex`: pdflatex -synctex=1 -interaction=nonstopmode **\-\-shell-escape** %.tex @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}|

LaTeX at University

| ``` @preview ![](svg/chapter-10/minted-haskell-english-crop.svg) @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 ![](svg/chapter-10/minted-java-english-crop.svg) @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} ``` 🔗 **[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 8 @title Listing source code @content * In the directory `exercises/source-code-listings` you can find a file named `Source.java`, which we want to include in our document. * ++ **Include** the java code in the file `task-1.raw.tex1`. * ++ **Number** the code lines. * ++ Use the **theme** `native`. * ++ Change the **background colour** to dark blue. * ++ Now only include **lines 5 to 7**. * ++ Delete the **spaces** at the beginning of the lines by using a suitable option (Hint: The documentation speaks of `gobble`). * ++ If you have questions, try to consult the documentation of the Minted package.