2

For my thesis I need to create a list of source code (as a table) that has the same style as my other table of contents. For all of my source code examples I use \lstinputlisting in order to load it from a specific file.

The current display of items in my table is as following: enter image description here

I need the format like in this example: enter image description here

Here a MWE:

\documentclass{report}
\usepackage{listings}

\begin{document}

\lstlistoflistings

\clearpage
\begin{lstlisting}[caption=first code example]
<some code here>
\end{lstlisting}

\begin{lstlisting}[caption=second code example]
<some code here>
\end{lstlisting}

\end{document}

How do I add the prefix "Code" in front of the numbers, followed by a colon and finished by left aligning the whole line?

Edit: After including the solution that has been provided by @Tiuri I noticed a problem with too long captions that are distributed over multiple lines.

enter image description here

How can the caption text be aligned evenly, like in the first image?

3

You can redefine \numberline to put some text before every entry in the table of listings. In this example, I added "Code" to every entry:

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,ngerman]{babel}
\usepackage{listings}
\usepackage{showframe}

\begin{document}

\begingroup
\let\oldnumberline\numberline
\renewcommand{\numberline}[1]{\hspace{-1.5em}Code \oldnumberline{#1:}}
\lstlistoflistings
\endgroup

\clearpage
\begin{lstlisting}[caption=first code example]
<some code here>
\end{lstlisting}

\begin{lstlisting}[caption=second code example]
<some code here>
\end{lstlisting}

\end{document}

enter image description here

Edit: Added a colon and shifted the captions to the left page border.

  • This helps me a lot, thank you. However, it doesn't solve another problem, that of too long captions. How should I proceed if I want the caption (that breaks over multiple lines) to be aligned evenly (also on the left hand side)? Also the numbering is not evenly aligned vertically. – neuronalbit Jan 29 '18 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.