2.4 KiB
2.4 KiB
@slide(layout=chapter-slide)
@number 12
@title Source code listings
@slide(layout=content-and-preview)
@title In-situ listings
@content
The listings package provides a dedicated environment:
\section*{Haskell Magic}
Squares of all even % …
\begin{lstlisting}[language=Haskell]
[x^2 | x <- [1..200], even x]
\end{lstlisting}
@slide(layout=content-and-preview)
@title External source code
@content
Conveniently, 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)
@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)
@title Further resources
@content
- introduction to the package
- official documentation
- predefined UTF-8 configuration
xcolorpackage for colors- two solarized themes for
listings
@slide(layout=task)
@task-number 12
@title Listings
@content
- Include the Java source code
Source.javain the filesource-code-listings.tex(mind the relative file path!). - Enable special characters like umlauts via the
literateoption. - 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.
- Remove the signs for spaces in strings.
If you have questions, try to consult the documentation of the listings package.