30 lines
2.0 KiB
TeX
30 lines
2.0 KiB
TeX
\section{Using listings}
|
|
|
|
\subsection{Installation}
|
|
\pkg{listings} is a \LaTeX{} package of itself and does not require any additional installation.
|
|
We can therefore jump straight into typesetting source code.
|
|
|
|
\subsection{Defining listings}
|
|
Similar to \pkg{minted}, you can define listings using a dedicated environment (cf.\ \cref{lst:listings-environment}) and you can use another command to import source code directly from external files (cf.\ \cref{lst:listings-external}).
|
|
|
|
\example{lst:listings-environment}{source-code-listings/listings-environment}{Exemplary source code listing}
|
|
\example{lst:listings-external}{source-code-listings/listings-import}{Including from a separate file}
|
|
|
|
\noindent Both commands need a language specification\footnote{See this guide on Overleaf for a list of all languages: \url{https://www.overleaf.com/learn/latex/Code_listing\#Reference_guide}} as an optional parameter to apply syntax highlighting.
|
|
|
|
\subsection{Configuring listings}
|
|
The \pkg{listings} package can also be tweaked in an almost infinite number of ways.
|
|
Numerous optional parameters give you a lot of freedom.
|
|
|
|
Once again, Overleaf and the package documentation\footnote{Available at \url{https://www.overleaf.com/learn/latex/Code_listing\#Code_styles_and_colours} and \url{https://ctan.org/pkg/listings}, respectively.} come in handy.
|
|
|
|
\example{lst:listings-external-styled}{source-code-listings/listings-import-styled}{Themes and further options}
|
|
|
|
\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/}}
|
|
|
|
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.
|