4

I want to include a separate index of code listings in my document. Therefore the Listings should start counting from 0 again. But when i reset the counter the listings appear (of course), in the table of listings in the first part of the document again. But instead I want them to be listed in a new index in the appendix. (I hope it is not to hard to understand.. I am sorry for the bad explanation).

The following is what I want to avoid: enter image description here

I don't want the listings to be counted in the beginning of the whole document but I want a new index in the appendix.

\documentclass[a4paper,12pt,parskip,bibtotoc,liststotoc]{article}

\usepackage{listings}


\begin{document}

\addcontentsline{toc}{section}{Listingverzeichnis}
\lstlistoflistings 

\begin{lstlisting}[language=Ruby,caption={listing 1}]
this is the first listing
\end{lstlisting}



\begin{appendix}

\begin{lstlisting}[language=Ruby,caption={listing 2}]
this is the second listing
\end{lstlisting}

\end{appendix}

\end{document}

The example of the second listing is what I want to be indexed in a seperate table of listings within the appendix.

I am happy for any suggestions Thanks for your help!

4
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Depending on how you're defining what goes into this separate index, this MWE may be as simple as two chapters with a single listing in each. Mar 18, 2018 at 21:47
  • Thank you for the welcoming words. I added some code as suggested. Mar 18, 2018 at 22:25
  • 1
    So the first listing goes in an initial List of Listings at the front of the document, and the second one goes a the second List of Listings? Mar 18, 2018 at 22:31
  • appendix is no environment, so \begin{appendix} is not really correct here.
    – user31729
    Mar 18, 2018 at 22:50

1 Answer 1

3

This applies a local patch to \lstlistoflistings in order to use .apl (meaning appendix listings) and saves the file handle \tf@lol and stores the appendix listings to another file.

Just in case hyperref should be used: I've provided a new hyper anchor for the lstlisting counter as well in the appendix.

\documentclass[a4paper,12pt]{article}

\usepackage{listings}
\usepackage{xpatch}
%\usepackage{hyperref}% Just in case....


\providecommand{\phantomsection}{}

\makeatletter
\newcommand{\appendixlistingname}{Appendix Listings}
\newcommand{\listofappendixlistings}{%
  % Applying the same trick as listings does with \lstlistoflistings: Modifying \@starttoc such that it can load only .apl files
  \begingroup
  \let\@starttoc@orig\@starttoc%
  \renewcommand{\contentsname}{\appendixlistingname}
  \renewcommand{\@starttoc}[1]{%
    \@starttoc@orig{apl}%
  }%    
  \phantomsection
  \addcontentsline{toc}{section}{\appendixlistingname}
  \tableofcontents% Calling \tableofcontents with the `.apl` file instead of `.toc`
  \endgroup
}

\xapptocmd{\appendix}{%
  \setcounter{lstlisting}{0}%
  \@ifundefined{theHlstlisting}{%
    }{%
      \renewcommand{\theHlstlisting}{appendix}
    }%
  \write\@auxout{%
    \string\let\string\latex@tf@lol\string\tf@lol% Store the original `\tf@lol` file handle
    \string\let\string\tf@lol\string\tf@apl% 
  }%
}{}{}
\makeatother

\begin{document}

\addcontentsline{toc}{section}{Listingverzeichnis}
\lstlistoflistings 

\begin{lstlisting}[language=Ruby,caption={listing 1}]
this is the first listing
\end{lstlisting}

\begin{lstlisting}[language=Ruby,caption={listing 2}]
this is another listing in the main part
\end{lstlisting}


\clearpage



\appendix
\listofappendixlistings
\clearpage
\begin{lstlisting}[language=Ruby,caption={listing 3}]
  This is the third listing
\end{lstlisting}


\begin{lstlisting}[language=Ruby,caption={listing 4}]
  This is the fourth listing
\end{lstlisting}

\end{document}

enter image description here

1
  • @MaxHeumann: Gern geschehen. Fröhliches TeXen ;-)
    – user31729
    Mar 18, 2018 at 23:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .