latex-wochenende/slides/chapter-11.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/chapter-10/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/chapter-10/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
* 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.&thinsp;e., to `main-exercises.tex`.)
* 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.