listings
Source-code listings typeset entirely in LaTeX — no external tools required.
- Example
- Preamble
\Cref{lst:XML} shows source code written in XML.
\Cref{line:comment} contains a comment.
\begin{lstlisting}[
language=XML,
caption={Example XML Listing},
label={lst:XML}]
<listing name="example">
<!-- comment --> (* \label{line:comment} *)
<content>not interesting</content>
</listing>
\end{lstlisting}
\begin{lstlisting}[
% one can adjust spacing here if required
% aboveskip=2.5\baselineskip,
% belowskip=-.8\baselineskip,
float,
language=XML,
caption={Example XML listing -- placed as floating figure},
label={lst:flXML}]
<listing name="example">
Floating
</listing>
\end{lstlisting}
\begin{lstlisting}[
float,
language=json,
caption={Example JSON listing -- placed as floating figure},
label={lst:json}]
{
key: "value"
}
\end{lstlisting}
\begin{lstlisting}[
caption={Example Java listing},
label=lst:java,
language=Java,
float]
public class Hello {
public static void main (String[] args) {
System.out.println("Hello World!");
}
}
\end{lstlisting}
% Code Listings
\usepackage{listings}
\definecolor{eclipseStrings}{RGB}{42,0.0,255}
\definecolor{eclipseKeywords}{RGB}{127,0,85}
\colorlet{numb}{magenta!60!black}
% JSON definition
% Source: https://tex.stackexchange.com/a/433961/9075
\lstdefinelanguage{json}{
basicstyle=\normalfont\ttfamily,
commentstyle=\color{eclipseStrings}, % style of comment
stringstyle=\color{eclipseKeywords}, % style of strings
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frame=lines,
% backgroundcolor=\color{gray}, %only if you like
string=[s]{"}{"},
comment=[l]{:\ "},
morecomment=[l]{:"},
literate=
*{0}{{{\color{numb}0}}}{1}
{1}{{{\color{numb}1}}}{1}
{2}{{{\color{numb}2}}}{1}
{3}{{{\color{numb}3}}}{1}
{4}{{{\color{numb}4}}}{1}
{5}{{{\color{numb}5}}}{1}
{6}{{{\color{numb}6}}}{1}
{7}{{{\color{numb}7}}}{1}
{8}{{{\color{numb}8}}}{1}
{9}{{{\color{numb}9}}}{1}
}
\lstset{
% everything between (* *) is a latex command
escapeinside={(*}{*)},
%
language=json,
%
showstringspaces=false,
%
basicstyle=\footnotesize\ttfamily,
%
commentstyle=\slshape,
%
% default: \rmfamily
stringstyle=\ttfamily,
%
breaklines=true, % Lines are wrapped
%
breakatwhitespace=true,
%
% alternative: fixed
columns=flexible,
%
tabsize=2, % Groesse von Tabs
%
numbers=left,
%
numberstyle=\tiny,
%
basewidth=.5em,
%
xleftmargin=.5cm,
%
% aboveskip=0mm,
%
% belowskip=0mm,
%
captionpos=b
}
\lstloadlanguages{% Check dokumentation for further languages...
%[Visual]Basic
%Pascal
%C
%C++
%XML
%HTML
}