Rework the source code chapter

This commit is contained in:
Knoch 2022-01-08 16:28:10 +01:00
parent 658ca6a689
commit 211f1af408
9 changed files with 69 additions and 43 deletions

View File

@ -1,7 +1,8 @@
\newpage
\section{Using listings}
\subsection{Installation}
\pkg{listings} is a \LaTeX{} package of itself and does not require any additional installation.
\pkg{listings} is a \LaTeX{} package of itself and does not require any additional installation as long as the \pkg{xcolor} package is installed as well.
We can therefore jump straight into typesetting source code.
\subsection{Defining listings}
@ -22,8 +23,10 @@ Once again, Overleaf and the package documentation\footnote{Available at \url{ht
\subsection{Drawbacks and caveats}
As mentioned earlier, \pkg{listings} is not our first choice for source code listings.
Its renderings are of rather peculiar appearance and, unfortunately, it is not shipped with any pre-defined themes.
This makes configuration rather tedious.\footnote{Speaking of configuration, if you would like to specify your own colors, this page might help you: \url{https://mmoredo.github.io/latex-color-converter/}}
Its renderings are of rather peculiar appearance.
The package is also a bit older than its alternative, causing UTF8 special characters to break.
If this happens to you, have a look at the \mono{literate} option of the \mono{lstset} command.
With some amount of configuration, we can overcome the most disturbing default settings.
Although \pkg{listings} is not shipped with any pre-defined themes, you can define your own and use them throughout your project with the \mono{lstdefinestyle} command.\footnote{Have a look at it in the package documentation. For the very impatient, here is a solarized theme for \pkg{listings}: \url{https://github.com/jez/latex-solarized}}
The package is also a bit older than its alternative, causing \acro{UTF8} special characters to break.
If this happens to you, have a look at the \mono{literate} option of the \pkg{listings} commands.

View File

@ -3,8 +3,15 @@
There are many ways in \LaTeX{} to display source code.
We have come to appreciate the package \pkg{minted}, which causes some additional installation overhead, but generates very appealing source code renderings.
However, especially on macOS, the installation of minted has caused a lot of headache in the early days of this workshop.
That's why we will also have a look at an alternative called \pkg{lstlistings}.
\paragraph{A note on colors.} Both of these packages require you to define colors.
We can recommend using the \pkg{xcolor} package.
There are very helpful resources\footnote{If you would like to specify your own colors , these pages might help you: \url{https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX} for a list of pre-defined colors in the \pkg{xcolor} package, \url{https://mmoredo.github.io/latex-color-converter/} for defining your own colors} available online such that we will not go into details here.
\input{./content/source-code-listings-minted}
\input{./content/source-code-listings-listings}
\todo{Are the files minted.done.tex and listings.done.tex available in the solution-free zip? They should not.}

View File

@ -0,0 +1,7 @@
\lstinputlisting[
language=Java,
numbers=left, % line numbers
showstringspaces=false, % spaces in strings
keywordstyle=\color{blue}, % keyword color
basicstyle=\ttfamily\small % overall font and size
]{exercises/source-code-listings/Source.java}

View File

@ -0,0 +1,13 @@
\usemintedstyle{native}
\definecolor{ourBackgroundColor}{rgb}{0.1,0.1,0.2}
\inputminted[
breaklines, % line breaks
linenos=true, % line numbers
bgcolor=ourBackgroundColor, % background color
firstline=5, % first line to be included
lastline=7, % last line to be included
gobble=4 % remove leading spaces
]{java}{exercises/source-code-listings/Source.java}
\usemintedstyle{default}

View File

@ -1,26 +1,15 @@
\usemintedstyle{native}
\definecolor{ourBackgroundColor}{rgb}{0.1,0.1,0.2}
% Show rendering with respect to the installed software
\ifthenelse{\equal{\listingsmode}{minted}}{%
\subsubsection{Minted rendering}
\input{exercises/source-code-listings/minted.done.tex}
}{%
\subsubsection{Listings rendering}
\input{exercises/source-code-listings/listings.done.tex}
}
\inputminted[
breaklines,
linenos=true,
bgcolor=ourBackgroundColor,
firstline=5,
lastline=7,
gobble=4
]{java}{exercises/source-code-listings/Source.java}
\subsubsection{Minted solution}
\codeblock{latex}{./exercises/source-code-listings/minted.done.tex}
\usemintedstyle{default}
\begin{minted}{latex}
\usemintedstyle{native}
\definecolor{ourBackgroundColor}{rgb}{0.1,0.1,0.2}
\subsubsection{Listings solution}
\codeblock{latex}{./exercises/source-code-listings/listings.done.tex}
\inputminted[
breaklines, % line breaks
linenos=true, % line numbers
bgcolor=ourBackgroundColor, % background color
firstline=5, % first line to be included
lastline=7, % last line to be included
gobble=4 % remove leading spaces
]{java}{exercises/source-code-listings/Source.java}
\end{minted}

View File

@ -1,6 +1,9 @@
You will find a file called \file{Source.java} in the folder
\file{exercises/source-code-listings}.
We will now include it into our document and adjust its display to fit our needs.
If you have questions, consult the \pkg{minted} or \pkg{listings} package documentation.
\section*{Minted tasks}
\begin{enumerate}
\item Include the file into
@ -14,7 +17,17 @@ We will now include it into our document and adjust its display to fit our needs
option. (Hint: The documentation speaks of \code{latex}{gobble}.)
\end{enumerate}
\noindent If you have questions, consult the minted package documentation.
\section*{Listings tasks}
\begin{enumerate}
\item Include the file into
\file{exercises/source-code-listings/source-code-listings.tex}.
\item Activate syntax highlighting by stating the programming language Java.
\item Set the \mono{basicstyle} to a proper mono-spaced font (\mono{\textbackslash ttfamily \textbackslash small})
\item Add line numbers.
\item Change the keyword color to blue.
\item Include only lines 5 to 7.
\end{enumerate}
\exercisematerial{exercises/source-code-listings/source-code-listings}
\usemintedstyle{} % Reset experiments from exercise material

View File

@ -1,5 +1,5 @@
public class Test {
public static void main(/**/) {
public static void main(/*...*/) {
System.out.println("Welcome, " +
"fellow LaTeX learners!");
}

View File

@ -5,24 +5,17 @@
\definecolor{codeblue}{rgb}{0.36,0.76,0.85}
\definecolor{backcolour}{rgb}{0.1,0.1,0.2}
\lstdefinestyle{fswiai}{
\lstinputlisting[
language=Java,
backgroundcolor=\color{backcolour},
keywordstyle=\color{codeblue},
identifierstyle=\color{codegreen},
stringstyle=\color{codeyellow},
basicstyle=\footnotesize\ttfamily\color{white},
basicstyle=\footnotesize
\ttfamily\color{white},
commentstyle=\color{codegray},
numberstyle=\tiny\color{codegray},
breakatwhitespace=false,
breaklines=true,
keepspaces=true,
numbers=left,
numbersep=5pt,
showstringspaces=false,
showtabs=false,
tabsize=1
}
\lstset{style=fswiai}
\lstinputlisting[language=Java]{listings/source-code-listings/Test.java}
]{listings/source-code-listings/Test.java}

View File

@ -1 +1,2 @@
\lstinputlisting[language=Java]{listings/source-code-listings/Test.java}
\lstinputlisting[language=Java]
{listings/source-code-listings/Test.java}