3.3 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}
@slide(layout=content-only)
@title Compiler settings
@content
In TeXstudio, go to Options → Configure TeXstudio → Commands (TeXstudio → Settings on macOS) 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>|
@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 Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
@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-listingsyou can find a file namedSource.java, which we want to include in our document. - Include the java code in the file
source-code-listings.tex. (Keep in mind that the file path is relative to the main LaTeX document, i. e., tomain.tex.) - 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.