13

I'm writing my thesis and I have certain code-snippets to show. I'm using the the listings package embedded in a custom float environment to display them.

To list them in an index at the beginning of my document - similar to \listoffigures - I use:

\listof{code}{List of Code}

This (technically) works as desired but looks completely different to my list of figures. See screenshot below (List of Figures on the left, List of Code on the right).

lof vs loc

Why, and how to fix this? Or how to modify the \listof{}{} layout to look similar to the one of \listoffigures?

I tried this workaround which redefines the layout but it's not working as desired (still looks different) and is throwing more errors than I could post here.

Update 1: After some hours of trying to figure out of what wrong with my document (It's really bloated already.) I'll try to explain this with a minimal working example. Notice the screenshots below.

\documentclass[12pt,a4paper,twoside,openright]{report}
 \usepackage[english]{babel}
 \usepackage{float}
  \newfloat{algo}{tbp}{loa}[chapter]
  \setlength{\parskip}{3mm}     % !
  \setlength{\parindent}{3mm}   % !
  \frenchspacing
  \sloppy

 \begin{document}
  \listoffigures
  \listof{algo}{List of Algorithms}

  \chapter{foo}
   \begin{figure}
    \centering
    \rule{1cm}{1cm}
    \caption{A figure}
   \end{figure}
   \begin{algo}
    (algo)
    \caption{An algorithm}
   \end{algo}
   \begin{figure}
    \centering
    \rule{1cm}{1cm}
    \caption{Another figure}
   \end{figure}
   \begin{algo}
    (algo)
    \caption{Another algorithm}
   \end{algo}

  \chapter{bar}
   \begin{figure}
    \centering
    \rule{1cm}{1cm}
    \caption{Yet another figure}
   \end{figure}
   \begin{algo}
    (algo)
    \caption{Yet another algorithm}
   \end{algo}

 \end{document}

parskip

Parskip and parindent seem to play an important role, still I dont get a result as mentioned in lockstep's answer (with the spacing between chapters) ...

Update 2: Okay, now I found the reason why it's not working. It's the hyperref package. Updated MWE:

\documentclass[12pt,a4paper,twoside,openright]{report}
 \usepackage[english]{babel}
 \usepackage{float}
  \newfloat{algo}{tbp}{loa}[chapter]
%  \setlength{\parskip}{3mm}
%  \setlength{\parindent}{3mm}
  \frenchspacing
  \sloppy
% \usepackage{hyperref}                    % !!!
%  \hypersetup{pdftitle={Bachelorthesis}}  % !!!
%  \hypersetup{colorlinks=false}           % !!!
%  \hypersetup{plainpages=false}           % !!!
%  \hypersetup{pdfborder={0 0 0}}          % !!!
 \usepackage{etoolbox}
 \makeatletter
  \patchcmd{\@chapter}%
  {\addtocontents{lof}}%
  {\addtocontents{loa}{\protect\addvspace{10pt}}%
   \addtocontents{lof}}%
  {\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
 \makeatother


 \begin{document}
  \listoffigures
  \listof{algo}{List of Algorithms}

  \chapter{foo}
   \begin{figure}
    \centering
    \rule{1cm}{1cm}
    \caption{A figure}
   \end{figure}
   \begin{algo}
    (algo)
    \caption{An algorithm}
   \end{algo}
   \begin{figure}
    \centering
    \rule{1cm}{1cm}
    \caption{Another figure}
   \end{figure}
   \begin{algo}
    (algo)
    \caption{Another algorithm}
   \end{algo}

  \chapter{bar}
   \begin{figure}
    \centering
    \rule{1cm}{1cm}
    \caption{Yet another figure}
   \end{figure}
   \begin{algo}
    (algo)
    \caption{Yet another algorithm}
   \end{algo}

 \end{document}

Uncommenting the hyperref package and settings messed up the layout of the custom \listof.

So to break down this issue:

  1. How to adjust \parskip and \parindent without messing up the lists?
  2. How to avoid hyperref package messing around with the layout?

Update 3: Closed this question:

  1. How to adjust \parskip and \parindent without messing up the lists? I asked a follow-up question here: How to avoid \parskip being applied to \listoffigures etc.?
  2. How to avoid hyperref package messing around with the layout? Solved: Simply loading hyperref at the end of all packages and after \makeatother.

Thanks to Frank Mittelbach's workaround which helped me to solve the layout issue till this point.

2
  • Which document class do you use? Jun 4, 2012 at 16:38
  • \documentclass[12pt,a4paper,twoside,openright]{report}, added MWE now.
    – q9f
    Jun 4, 2012 at 17:59

2 Answers 2

10

This is a fault of the floatpackage you might say. It defines the \listofcommand as follows:

\newcommand*{\listof}[2]{%
  \@ifundefined{ext@#1}{\float@error{#1}}{%
    \@namedef{l@#1}{\@dottedtocline{1}{1.5em}{2.3em}}%
    \float@listhead{#2}%
    \begingroup\setlength{\parskip}{\z@}%
      \@starttoc{\@nameuse{ext@#1}}%
    \endgroup}}

This means that it a) fixes the \parskip to zero (which would be usually ok but still somewhat questionable) and b) gives each "listof" line a hard-wired meaning, by defining \l@<type>. The values used are the default values for \@lfigure in the article class. However, if the class you use has different values (and it looks like it has) they are obviously not picked up.

Resolution: provide your own copy of \listof looking like this:

\makeatletter
\renewcommand*{\listof}[2]{%
  \@ifundefined{ext@#1}{\float@error{#1}}{%
    \expandafter\let\csname l@#1\endcsname \l@figure  % <- use layout of figure
    \float@listhead{#2}%
    \begingroup
      \setlength\parskip{0pt plus 1pt}%               % <- or drop this line completely
      \@starttoc{\@nameuse{ext@#1}}%
    \endgroup}}
\makeatother

This will make the formatting of the "list-of" lines the same as those for figures in the "List of Figures" listing and it will reset the spacing between paragrpahs (and each line here is a paragraph) to the default used by the article/report class. The alternative would be to take that setting completely out.

However, this is not quite enough in case report or book class is used. As mentioned in the answer by @lockstep the \chapter command also adds vertical space material to such listings. So in addition the \@chapter command needs to be patched to include this extra space. So you need something like

   \usepackage{etoolbox}
   \makeatletter
   \patchcmd{\@chapter}%
     {\addtocontents{lof}}%
     {\addtocontents{alg}{\protect\addvspace{10pt}}%  % <-- "alg" is the extension you use
      \addtocontents{lof}}%
     {\typeout{*** SUCCESS ***}}{\typeout{*** FAIL ***}}
   \makeatother

where you have to replace "alg" with whatever the file extension is that you use

6
  • Understanding the problem but not exactly your provided code. And it's not working. My document class is report.
    – q9f
    Jun 4, 2012 at 15:32
  • By not working you mean it crashes (hope not) or that it doesn't give the same results? I have updated the answer as with report class there \parskip spacing will be a problem as well as the extra space added by \chapter. Hope those are now all differences. Jun 4, 2012 at 17:10
  • The last updated code snippet doesnt not throw any errors, even tho I don't see any changes in layout. I added a MWE to my question now.
    – q9f
    Jun 4, 2012 at 18:00
  • well, did you change "alg" to "loa"? And did you drop or change the setting of the \parskip line? As you use 3mm you have to use that there too (or remove the line completely). When I run your MWE with my code added it works as expected. Jun 4, 2012 at 19:00
  • it's working with the MWE now, but not with my main document. it's too complex to figure out whats wrong i guess :(
    – q9f
    Jun 5, 2012 at 9:25
3

The workaround you tried is for the book class which features a different defintion of \chapter. For report, the following works for me (i.e., yields identical formatting for the List of Figures and the newly defined List of Algorithms):

\documentclass[a4paper]{report}

\usepackage{float}
\newfloat{algo}{tbp}{loa}[chapter]

\usepackage{xpatch}
\makeatletter
\xapptocmd{\@chapter}{%
  \addtocontents{loa}{\protect\addvspace{10\p@}}%
}{}{}
\makeatother

\begin{document}

\listoffigures

\listof{algo}{List of Algorithms}

\chapter{foo}

\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{A figure}
\end{figure}

\begin{algo}
(algo)
\caption{An algorithm}
\end{algo}

\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{Another figure}
\end{figure}

\begin{algo}
(algo)
\caption{Another algorithm}
\end{algo}

\chapter{bar}

\begin{figure}
\centering
\rule{1cm}{1cm}
\caption{Yet another figure}
\end{figure}

\begin{algo}
(algo)
\caption{Yet another algorithm}
\end{algo}

\end{document}

enter image description here

enter image description here

Note: The screenshot you provided does not look like the default LoF spacing for the report class (there's additional spacing between entries located in the same chapter). You should provide a MWE showing the relevant packages and/or individual customizations.

2
  • my guess is that this is the parskip spacing and the fact that report has flushbottom Jun 4, 2012 at 17:11
  • @FrankMittelbach By default (i.e., with oneside and without twocolumn), report features \raggedbottom.
    – lockstep
    Jun 4, 2012 at 17:16

You must log in to answer this question.

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