I am writing my master thesis using LaTeX, a typesetting system most commonly used for scientific documents. Since my thesis is in computer science, I have the need to display a fair amount of code. After searching around the net and with some trial and error, I have come up with some settings that does this in a nice way.
It is based on the listings package, we define a lstset that handles the code. This in combination with a nice caption makes for a nice presentation of source code.
The language must obviously be changed to the language you are using, to provide the correct syntax coloring.
the numbers parameter chooses whether to show line numbers or not, the prebreak parameter adds an arrow to indicate that the line is broken. The upquote parameter ensures that no fancu “‘s are used, so that you can copy the code and still be able to run it. The other parameters should be self-explanatory, and can be modified as you see fit.
To use this in your document, you can insert code directly with:
\begin{lstlisting}[label=some-samplecode, caption=Example for code straight from the .tex file]
<insert code here>
\end{lstlisting}
or you can insert code from an external file using:
\lstinputlisting[label=samplecode,caption=Example for code from a file]{sourceCode/K53.java}
all of these can now be referenced from other places in the document with the \ref{labelname} command, and will show up in your overview of listings.
Citing / displaying source code in LaTeX
I am writing my master thesis using LaTeX, a typesetting system most commonly used for scientific documents. Since my thesis is in computer science, I have the need to display a fair amount of code. After searching around the net and with some trial and error, I have come up with some settings that does this in a nice way.
It is based on the listings package, we define a lstset that handles the code. This in combination with a nice caption makes for a nice presentation of source code.
Here is the final result:
LaTeX Source Code Screenshot
“Read more” for the rest of the article
The packages needed are:
We now define the caption as:
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
This makes a gray box for the caption, with white text.
Now define the lstset with the following:
\lstset{
language=Java,
keywordstyle=\bfseries\ttfamily\color[rgb]{0,0,1},
identifierstyle=\ttfamily,
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\ttfamily\color[rgb]{0.627,0.126,0.941},
showstringspaces=false,
basicstyle=\small,
numberstyle=\footnotesize,
numbers=left,
stepnumber=1,
numbersep=10pt,
tabsize=2,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
breakatwhitespace=false,
aboveskip={1.5\baselineskip},
columns=fixed,
upquote=true,
extendedchars=true,
frame=bottomline,
inputencoding=utf8
}
The language must obviously be changed to the language you are using, to provide the correct syntax coloring.
the numbers parameter chooses whether to show line numbers or not, the prebreak parameter adds an arrow to indicate that the line is broken. The upquote parameter ensures that no fancu “‘s are used, so that you can copy the code and still be able to run it. The other parameters should be self-explanatory, and can be modified as you see fit.
To use this in your document, you can insert code directly with:
\begin{lstlisting}[label=some-samplecode, caption=Example for code straight from the .tex file]
<insert code here>
\end{lstlisting}
or you can insert code from an external file using:
\lstinputlisting[label=samplecode,caption=Example for code from a file]{sourceCode/K53.java}
all of these can now be referenced from other places in the document with the \ref{labelname} command, and will show up in your overview of listings.
Related posts: