# Customize the name of custom listings in their "list of"

As I would like to separate the listings in my document, I decided to create two separate lists of them: one for the algorithms, and one for the sources.

So, with the following code in the preamble and inserting \lstlistofsourcecode in the right place I can achieve two separated lists.

\makeatletter
\newcommand{\lstlistsourcecodename}{Elenco dei codici}%
\lst@UserCommand\lstlistofsourcecode{\bgroup
\let\contentsname\lstlistsourcecodename
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{los}}
\tableofcontents \egroup}%
\newcommand{\lstinputsourcecode}[2][]{{%
\renewcommand{\lstlistingname}{Sorgente}
\lstinputlisting[#1]{#2}%
}}
\makeatother


The image shows the results:

The goal I want to achieve is to change the name of \lstinputsourcecode listings into their los (i.e., list of ...): from "Algoritmo B.1" to "Sorgente B.1".

The strange behaviour is that \lstlistingname command works for the captions.

For example, if I do:

\lstinputsourcecode[language=cpp, caption=Esempio di funzione \acs{RTE} esportata, label=lst:callpoint-impl]{codes/callpointimpl.cpp}


Then I obtain the right caption (ie., "Sorgente: ..." in the image):

How can I tell to the \lstlistofsourcecode to use "Sorgente" and not "Algoritmo" ?

Note: I'm using the classicthesis template.

## Solution

The final solution adopted (thanks to @Werner) is:

\makeatletter
\newcommand{\sourcecodename}{Sorgente}
\newcommand{\listofsourcecodename}{Elenco dei sorgenti}%
\lst@UserCommand\listofsourcecode{\bgroup
\let\contentsname\listofsourcecodename
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{los}}
\newlength{\sourcecodelabelwidth}
\settowidth{\sourcecodelabelwidth}{\sourcecodename~999}
\def\l@lstlisting##1##2{\@dottedtocline{1}{0em}{\sourcecodelabelwidth}{##1}{##2}}
\def\numberline##1{\hb@xt@\@tempdima{\sourcecodename{} ##1\hfil}}
\tableofcontents \egroup}%
\newcommand{\inputsourcecode}[2][]{{%
\renewcommand{\lstlistingname}{\sourcecodename}%
\lstinputlisting[#1]{#2}%
}}
\makeatother

• Regardless of how complete this question may seem to you, it is always best and extremely appreciated if you could compose what is called a minimal working example (MWE) for the community to work with. Not only does it allow us to hit the ground running, it also speeds up getting solutions to you. Sep 8 '13 at 4:04

Here are possible elements you can include in your redefinition of \lstlistofsourcecode:

\lst@UserCommand\lstlistofsourcecode{\bgroup
\let\contentsname\lstlistsourcecodename
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{los}}
\tableofcontents \egroup}%


The update to \l@lstlisting increases the space allocated for the \numberline macro from 2.3em to 5.2em (the third argument). You may want to experiment with this if you wish for it to be similar to the inclusion of your algorithms (I don't have a MWE replicating the situation, so I just guessed 5.2em). The update to \numberline inserts Sorgente before the number as needed.

The reason for this type of "intervention" is because the setting of the listing and the listing ToC is completely separate. Packages like tocloft ease this manual intervention.

• I was trying to accomplish the same thing with tocloft and I had almost succeeded. I missed the inclusion of entry (precisely of its caption) .. Sep 8 '13 at 14:53