1

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.

In addition, the sources must be loaded from an external file.

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

\makeatletter
\let\oldaddcontentsline\addcontentsline
\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}
  \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{los}{##2}{##3}}%
  \lstinputlisting[#1]{#2}%
}}
\makeatother

The image shows the results:

enter image description here

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):

enter image description here

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


Note: I'm using the classicthesis template.


Thanks in advance.

Solution

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

\makeatletter
\newcommand{\sourcecodename}{Sorgente}
\let\oldaddcontentsline\addcontentsline
\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}
  \addtolength{\sourcecodelabelwidth}{1.8em}
  \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}%
  \renewcommand{\addcontentsline}[3]{\oldaddcontentsline{los}{##2}{##3}}%
  \lstinputlisting[#1]{#2}%
}}
\makeatother 
1
  • 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.
    – Werner
    Sep 8 '13 at 4:04
2

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}}
    \def\l@lstlisting##1##2{\@dottedtocline{1}{1.5em}{5.2em}{##1}{##2}}% Added
    \def\numberline##1{\hb@xt@\@tempdima{Sorgente ##1\hfil}}% Added
    \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.

1
  • 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) ..
    – leodido
    Sep 8 '13 at 14:53

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.