latex-wochenende/slides/chapter-10.en.md

3.0 KiB

@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, we can download the corresponding Python package Pygments using a command prompt:

pip install Pygments

Add the LaTeX package and we are good to go:

\usepackage{minted}

@todo Update source code?

@preview { .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 -syntex=1 -interaction=nonstopmode --shell-escape %.tex

@slide(layout=content-and-preview)

@title Listings within LaTeX documents

@content ++ Within a dedicated environment:

\section*{Haskell Magic}
Squares of all even % …
\begin{minted}{haskell}
[x^2 | x <- [1..200], even x]
\end{minted}

++ Directly inline:

\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.

\section*{Simple Java Application}
\inputminted{java}{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.

\usemintedstyle{monokai}
\begin{minted}[
linenos=true,
breaklines=true,
]{javascript}
    % ...
\end{minted}

🔗 introduction and official documentation

@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.