6.8 KiB
@slide(layout=chapter-slide)
@title Source code listings
@slide(layout=content-only)
@title Our options
@content There are two popular ways to display source code in a LaTeX document.
- ++
mintedrenders very pretty source code listings, but it requires some extra configuration and might not work on every operating system. - ++
listingsis an easy-to-use (but not as pretty) option for those having troubles withminted.
++ Their commands are very similar.
@slide(layout=content-and-preview)
@title Installation 🖥️
@content
minted 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 packages for minted or listings and we are good to go:
\usepackage{minted}
\usepackage{listings}
@slide(layout=content-only)
@title Compiler settings
@content
Do this only for documents you trust!
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-only)
@title A note on our script
@content
Our exercise material allows you to choose between minted and listings.
By default, we use listings.
To activate minted, create a file called listings-mode.tex in the project's root directory and add the following line:
\newcommand\listingsmode{minted}
After compiling again, your listings should be rendered by minted.
@slide(layout=content-and-preview-with-category)
@category minted
@title In-situ listings
@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-with-category)
@category minted
@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-with-category)
@category minted
@title Themes and styles
@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=content-and-preview-with-category)
@category listings
@title In-situ listings
@content Within a dedicated environment:
\section*{Haskell Magic}
Squares of all even % …
\begin{lstlisting}[language=Haskell]
[x^2 | x <- [1..200], even x]
\end{lstlisting}
The listings package does not provide an inline command.
@slide(layout=content-and-preview-with-category)
@category listings
@title External source code
@content
Conveniently, also listings offers an import command:
\section*{Simple Java Application}
\lstinputlisting[language=Java]{Test.java}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
@slide(layout=content-only-with-category)
@category listings
@title Themes and styles
@content
listings does not provide any themes by default, but can be configured extensively.
\begin{lstlisting}[
language=Java,
basicstyle=\footnotesize\ttfamily,
breaklines=true,
keywordstyle=\color{ForestGreen},
commentstyle=\color{DarkGray},
literate={ö}{{\"o}}1
]
% …
\end{lstlisting}
Have a look at the \lstset command for creating your own themes.
@slide(layout=content-only-with-category)
@category listings
@title Further resources
@content
- introduction to the package
- official documentation
xcolorpackage for colors- two solarized themes for
listings
@slide(layout=task)
@task-number 10
@title
Listings with minted
@content
- In the directory
exercises/source-code-listingsyou can find a file namedSource.java. - Include it 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.) - Activate syntax highlighting by stating the programming language Java.
- Number the code lines and add line breaks.
- 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.
@slide(layout=task)
@task-number 10
@title
Listings with listings
@content
- In the directory
exercises/source-code-listingsyou can find a file namedSource.java. - Include it 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.) - Activate syntax highlighting by stating the programming language Java.
- Number the code lines.
- Set the
basicstyleto a proper mono-spaced font (\ttfamily \small) - Change the keyword color to blue.
- Don't show special characters for spaces in strings.
If you have questions, try to consult the documentation of the listings package.