2.6 KiB
2.6 KiB
@slide(layout=chapter-slide)
@number 13
@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 13
@title 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-exercises.tex.) - Enable special characters 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.
- Don't show special characters for spaces in strings.
If you have questions, try to consult the documentation of the listings package.