Skip to main content

listings

Source-code listings typeset entirely in LaTeX — no external tools required.

Rendered LaTeX output
% After a period, LaTeX sets the wider sentence space by default. The exception: a period after a capital letter
% is taken for an abbreviation or initial ("T. Scientist", "NASA") and gets the narrow inter-word space.
% "XML." ends a sentence, so \@ before the period restores the sentence space.
% (The other way round, "e.g.\ details" forces the narrow space after a lowercase abbreviation; ~ is a
% non-breaking inter-word space, as in "Prof.~Dr.~Eike Kraft".)
% An abbreviation set with \gls{xml} needs no \@: glossaries ends it with a non-letter, so the period after it
% gets the sentence space. The plain "XML" here is for documents without the glossaries package.
\Zcref{lst:XML} shows source code written in XML\@.
\Zcref{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}
Rendered LaTeX output
\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}
Rendered LaTeX output
\begin{lstlisting}[
float,
language=json,
caption={Example JSON listing -- placed as floating figure},
label={lst:json}]
{
key: "value"
}
\end{lstlisting}
Rendered LaTeX output
\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}