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

3.2 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}

@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 -synctex=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 10

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