static.wiai.de/latex-weekend/slides/source-code-listings.en.md
2023-11-18 01:26:09 +01:00

2.6 KiB
Raw Permalink Blame History

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

@preview

@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(/*…*/);
    }
}

@preview

@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

@slide(layout=task)

@task-number 13

@title Listings

@content

  • In the directory exercises/source-code-listings you can find a file named Source.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., to main-exercises.tex.)
  • Enable special characters via the literate option.
  • Activate syntax highlighting by stating the programming language Java.
  • Number the code lines.
  • Set the basicstyle to 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.