4

I want to write the chapter names to my LOF, LOT, and LOL to make them easier to read and found the following peace of code here

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

It works perfectly fine for the LOF and the LOT but it won't work for the LOL. Im using the package listings to generate my listings, could that be a problem? Here is a complete example, showing the problem:

\documentclass[12pt,headsepline,toc=bibliography,toc=listof,chapteratlists=entry]{scrbook}

\AfterTOCHead[lof]{\addtokomafont{chapterentrypagenumber}{\nullfont}}
\AfterTOCHead[lot]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

\usepackage{listings}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings

\chapter{Chapter 1}
\begin{figure}
\caption{Figure 1}
\end{figure}
\begin{figure}
\caption{Figure 2}
\end{figure}

\chapter{Chapter 2}

\chapter{Chapter 3}
\begin{figure}
\caption{Figure 3}
\end{figure}
\begin{table}
\caption{Table 1}
\end{table}

\begin{lstlisting}[caption={Listing 1}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 2}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 3}]
\end{lstlisting}

\end{document}

Working list of tables non functional list of listings

So in short: How can I make the chapter name appear on the list of listings as they do on the list of tables?

4

In the code of yours, the following happens: The KOMA class macro \doforeachtocfile calls all registered list files, checks whether they have the property chapteratlist, and then writes the section entry to the respective list file. The listings package doesn't, however, register the list of listings with the KOMA class. But you can easily supply that. Just add:

  • \addtotoclist{lol} to add the list of listings to the list of lists,
  • \setuptoc{lol}{chapteratlist} to add the property chapteratlist to the list of listings.
\documentclass[12pt,headsepline,toc=bibliography,toc=listof,chapteratlists=entry]{scrbook}

\AfterTOCHead[lof]{\addtokomafont{chapterentrypagenumber}{\nullfont}}
\AfterTOCHead[lot]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

\addtotoclist{lol}
\setuptoc{lol}{chapteratlist}

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

\usepackage{listings}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings

\chapter{Chapter 1}
\begin{figure}
\caption{Figure 1}
\end{figure}
\begin{figure}
\caption{Figure 2}
\end{figure}

\chapter{Chapter 2}

\chapter{Chapter 3}
\begin{figure}
\caption{Figure 3}
\end{figure}
\begin{table}
\caption{Table 1}
\end{table}

\begin{lstlisting}[caption={Listing 1}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 2}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 3}]
\end{lstlisting}

\end{document}

enter image description here

4

KOMA-Script also provides package scrhack that improves several other packages, e.g., patches listings to use tocbasic. If you use this package, chapteratlists=entry automatically also works for \listoflistings:

\documentclass[12pt,headsepline,toc=bibliography,toc=listof,chapteratlists=entry]{scrbook}

\AfterTOCHead[lof]{\addtokomafont{chapterentrypagenumber}{\nullfont}}
\AfterTOCHead[lot]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

%%%%% from http://www.komascript.de/comment/5070#comment-5070 (Markus Kohm)
\makeatletter
\let\chapterhas@original@addcontentsline\addcontentsline
\renewcommand*{\addcontentsline}[1]{%
  \immediate\write\@auxout{\string\chapterhas{\thechapter}{#1}}%
  \chapterhas@original@addcontentsline{#1}%
}
\newcommand*{\chapterhas}[2]{%
  \global\@namedef{chapterhas@#1@#2}{true}%
}
\renewcommand*{\addchaptertocentry}[2]{%
  \addtocentrydefault{chapter}{#1}{#2}%
  \if@chaptertolists
    \doforeachtocfile{%
      \iftocfeature{\@currext}{chapteratlist}{%
        \ifundefinedorrelax{chapterhas@\thechapter @\@currext}{%
        }{%
          \addxcontentsline{\@currext}{chapteratlist}[{#1}]{#2}%
        }%
      }{}%
    }%
    \@ifundefined{float@addtolists}{}{\scr@float@addtolists@warning}%
  \fi
}
\makeatother
%%%%

\usepackage{listings}
\usepackage{scrhack}% use tocbasic for \listoflistings (and more)
\AfterTOCHead[lol]{\addtokomafont{chapterentrypagenumber}{\nullfont}}

\begin{document}

\tableofcontents
\listoffigures
\listoftables
\lstlistoflistings

\chapter{Chapter 1}
\begin{figure}
\caption{Figure 1}
\end{figure}
\begin{figure}
\caption{Figure 2}
\end{figure}

\chapter{Chapter 2}

\chapter{Chapter 3}
\begin{figure}
\caption{Figure 3}
\end{figure}
\begin{table}
\caption{Table 1}
\end{table}

\begin{lstlisting}[caption={Listing 1}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 2}]
\end{lstlisting}
\begin{lstlisting}[caption={Listing 3}]
\end{lstlisting}

\end{document}

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.