latex-wochenende/slides/source-code-listings.en.md

110 lines
2.5 KiB
Markdown

@slide(layout=chapter-slide)
@number
11
@title
Source code listings
@slide(layout=content-and-preview)
@title
In-situ listings
@content
The `listings` package provides a dedicated environment:
``` {.lang-tex .hljs}
\section*{Haskell Magic}
Squares of all even % …
\begin{lstlisting}[language=Haskell]
[x^2 | x <- [1..200], even x]
\end{lstlisting}
```
@preview
![](svg/source-code-listings/listings-haskell-english-crop.svg)
@slide(layout=content-and-preview)
@title
External source code
@content
Conveniently, `listings` offers an import command:
``` {.lang-tex .hljs}
\section*{Simple Java Application}
\lstinputlisting[language=Java]{Test.java}
```
``` {.lang-java .hljs data-sourcefile="Test.java"}
public class Test {
public static void main(/*…*/) {
System.out.println(/*…*/);
}
}
```
@preview
![](svg/source-code-listings/listings-java-english-crop.svg)
@slide(layout=content-only)
@title
Themes and styles
@content
`listings` does not provide any themes by default, but can be configured extensively.
``` {.lang-tex .hljs}
\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](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide) to the package
* official [documentation](https://www.overleaf.com/learn/latex/Code_listing#Reference_guide)
* predefined [UTF-8 configuration](https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings#Encoding_issue)
* [`xcolor` package](https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX) for colors
* two [solarized themes](https://github.com/jez/latex-solarized) for `listings`
@slide(layout=task)
@task-number
11
@title
Listings
@content
* **Include** the Java source code `Source.java` in the file `source-code-listings.tex` (mind the relative file path!).
* Enable **special characters** like umlauts 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.
* Remove the signs for **spaces in strings**.
If you have questions, try to consult the documentation of the `listings` package.