9

I want to add a colon ":" after each lstlistingname in the lstlistoflistings.

At the moment the it shows me "Code 1 StatusCalculator Initialize....." But I want a colon after lstlistingname "Code 1: StatusCalculator Initialize..."

I didn't get it to run. Can anyone help me?

\documentclass[12pt, a4paper, oneside]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage[ngerman]{babel}
\usepackage[hidelinks=true]{hyperref}
\usepackage[final]{pdfpages}

\definecolor{grey}{gray}{0.9}

\usepackage{listings}
\renewcommand\lstlistingname{Code}
\lstloadlanguages{Ruby}
\lstset{%
basicstyle=\small\ttfamily\color{black},
commentstyle = \ttfamily\color{red},
keywordstyle=\ttfamily\color{blue},
stringstyle=\color{orange},
backgroundcolor=\color{grey},
columns=fullflexible}

\usepackage{color}
\usepackage{xcolor}

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\let\oldlstlistoflistings\lstlistoflistings
\renewcommand{\lstlistoflistings}{%
  \begingroup%
  \let\oldnumberline\numberline%
  \renewcommand{\numberline}{\lstlistingname~\oldnumberline}%
  \oldlstlistoflistings%
  \endgroup}

\usepackage{geometry}
\geometry{a4paper, top=25mm, left=25mm, right=25mm, bottom=20mm, headsep=10mm, footskip=12mm}

\begin{document}

 \renewcommand\lstlistlistingname{Codelisting}
 \addcontentsline{toc}{section}{Codelisting}
 \lstlistoflistings
 \newpage

  \begin{lstlisting}[language=Ruby, caption=StatusCalculator Initialize, label=initialize]
  def initialize(project_id)
    @project = Project.find(project_id)
  end
  \end{lstlisting}

\end{document}
6

To achieve what you want, the trick

\let\oldlstlistoflistings\lstlistoflistings
\renewcommand{\lstlistoflistings}{%
  \begingroup%
  \let\oldnumberline\numberline%
  \renewcommand{\numberline}{\lstlistingname~\oldnumberline}%
  \oldlstlistoflistings%
  \endgroup}

is not enough...

You have to load the package tocloft and play with it.

First of all, you have to register the list of listings to tocloft, as explained in this answer of egreg with the code

\makeatletter
\begingroup\let\newcounter\@gobble\let\setcounter\@gobbletwo
  \globaldefs\@ne \let\c@loldepth\@ne
  \newlistof{listings}{lol}{\lstlistlistingname}
\endgroup
\let\l@lstlisting\l@listings
\makeatother

Then, playing a little with tocloft commands:

\setlength{\cftlistingsindent}{1.5em}
\renewcommand*{\cftlistingspresnum}{\lstlistingname~}
\settowidth{\cftlistingsnumwidth}{\cftlistingspresnum}
\addtolength{\cftlistingsnumwidth}{2.3em}
\renewcommand{\cftlistingsaftersnum}{:}

Finally, remove the first mentioned code with the discussed ones, that is modify your MWE to

\documentclass[12pt, a4paper, oneside]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{tocloft}
\usepackage[ngerman]{babel}
\usepackage[hidelinks=true]{hyperref}
\usepackage[final]{pdfpages}

\definecolor{grey}{gray}{0.9}

\usepackage{listings}
\renewcommand\lstlistingname{Code}
\lstloadlanguages{Ruby}
\lstset{%
basicstyle=\small\ttfamily\color{black},
commentstyle = \ttfamily\color{red},
keywordstyle=\ttfamily\color{blue},
stringstyle=\color{orange},
backgroundcolor=\color{grey},
columns=fullflexible}

\usepackage{color}
\usepackage{xcolor}

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\makeatletter
\begingroup\let\newcounter\@gobble\let\setcounter\@gobbletwo
  \globaldefs\@ne \let\c@loldepth\@ne
  \newlistof{listings}{lol}{\lstlistlistingname}
\endgroup
\let\l@lstlisting\l@listings
\makeatother

\setlength{\cftlistingsindent}{1.5em}
\renewcommand*{\cftlistingspresnum}{\lstlistingname~}
\settowidth{\cftlistingsnumwidth}{\cftlistingspresnum}
\addtolength{\cftlistingsnumwidth}{2.3em}
\renewcommand{\cftlistingsaftersnum}{:}

\usepackage{geometry}
\geometry{a4paper, top=25mm, left=25mm, right=25mm, bottom=20mm, headsep=10mm, footskip=12mm}

\begin{document}

 \renewcommand\lstlistlistingname{Codelisting}
 \addcontentsline{toc}{section}{Codelisting}
 \lstlistoflistings
 \newpage

  \begin{lstlisting}[language=Ruby, caption=StatusCalculator Initialize, label=initialize]
  def initialize(project_id)
    @project = Project.find(project_id)
  end
  \end{lstlisting}

\end{document} 

and you will get

enter image description here

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.