Text Formattinglstlistoflistings toc vertical spacings

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
MakezHD
Posts: 10
Joined: Thu Jan 21, 2016 8:19 pm

lstlistoflistings toc vertical spacings

Post by MakezHD »

A need my lstlistoflistings in the table of contents to be grouped by chapters and to use the same vertical spacings that are used in figures and tables listings.

What do I need to do add/modify to get those spacings?

Thank you.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

lstlistoflistings toc vertical spacings

Post by rais »

I guess that depends on the class you're using.
For report class, you could try sth. like

Code: Select all

\usepackage{etoolbox}
\makeatletter
  \patchcmd{\@chapter}{\chaptermark{#1}}{%
    \chaptermark{#1}%
    \addtocontents{lol}{\protect\addvspace{10\p@}}%
    }{\typeout{Chapters patched for list-of-listings.}}{\typeout{Could not patch chapters for list-of-listings.}}
\makeatother
south of loading listings in your preamble.

KR
Rainer
MakezHD
Posts: 10
Joined: Thu Jan 21, 2016 8:19 pm

lstlistoflistings toc vertical spacings

Post by MakezHD »

rais wrote:I guess that depends on the class you're using.
For report class, you could try sth. like

Code: Select all

\usepackage{etoolbox}
\makeatletter
  \patchcmd{\@chapter}{\chaptermark{#1}}{%
    \chaptermark{#1}%
    \addtocontents{lol}{\protect\addvspace{10\p@}}%
    }{\typeout{Chapters patched for list-of-listings.}}{\typeout{Could not patch chapters for list-of-listings.}}
\makeatother
south of loading listings in your preamble.

KR
Rainer
I forgot to say it's a book. Sorry.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

lstlistoflistings toc vertical spacings

Post by rais »

MakezHD wrote: I forgot to say it's a book.
if you mean the standard book class, it should work the same way.
If you're using another class that typesets books, you'll have to let us know which one.

KR
Rainer
MakezHD
Posts: 10
Joined: Thu Jan 21, 2016 8:19 pm

lstlistoflistings toc vertical spacings

Post by MakezHD »

rais wrote:I guess that depends on the class you're using.
For report class, you could try sth. like

Code: Select all

\usepackage{etoolbox}
\makeatletter
  \patchcmd{\@chapter}{\chaptermark{#1}}{%
    \chaptermark{#1}%
    \addtocontents{lol}{\protect\addvspace{10\p@}}%
    }{\typeout{Chapters patched for list-of-listings.}}{\typeout{Could not patch chapters for list-of-listings.}}
\makeatother
south of loading listings in your preamble.

KR
Rainer
Chapters are being grouped fine now but vertical spaces are yet not identical to the ones in the figures and tables listings.

Image
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

lstlistoflistings toc vertical spacings

Post by rais »

In a book, LaTeX tries to keep the last line consistent throughout the pages. To that end, it may add vertical space between paragraphs, list entries, ...
So with different numbers of entries in LOF/LOL, you get different such added space.
If you don't want that, add

Code: Select all

\raggedbottom
to your preamble.
If that is not enough (you may now notice different space between headlines and first entries in LOF/LOT), add

Code: Select all

\renewcommand\lstlistoflistings{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\lstlistlistingname}%
      \@mkboth{\MakeUppercase\lstlistlistingname}%
              {\MakeUppercase\lstlistlistingname}%
    \@starttoc{lol}%
    \if@restonecol\twocolumn\fi
  }
to above \makeatletter..\makeatother block. This will redefine \lstlistoflistings to behave the same way as \listoffigures does in a book class, hence the vertical space between `Listings' and the first entry in LOL should now be the same as between `List of figures' and the first entry in LOF (if there's the same number of entries LOF/LOL or \raggedbottom is active).

KR
Rainer
MakezHD
Posts: 10
Joined: Thu Jan 21, 2016 8:19 pm

lstlistoflistings toc vertical spacings

Post by MakezHD »

rais wrote:In a book, LaTeX tries to keep the last line consistent throughout the pages. To that end, it may add vertical space between paragraphs, list entries, ...
So with different numbers of entries in LOF/LOL, you get different such added space.
If you don't want that, add

Code: Select all

\raggedbottom
to your preamble.
If that is not enough (you may now notice different space between headlines and first entries in LOF/LOT), add

Code: Select all

\renewcommand\lstlistoflistings{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\lstlistlistingname}%
      \@mkboth{\MakeUppercase\lstlistlistingname}%
              {\MakeUppercase\lstlistlistingname}%
    \@starttoc{lol}%
    \if@restonecol\twocolumn\fi
  }
to above \makeatletter..\makeatother block. This will redefine \lstlistoflistings to behave the same way as \listoffigures does in a book class, hence the vertical space between `Listings' and the first entry in LOL should now be the same as between `List of figures' and the first entry in LOF (if there's the same number of entries LOF/LOL or \raggedbottom is active).

KR
Rainer

Thank you so much!
Post Reply