Text FormattingChapter Headings in LoF and LoT

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Chapter Headings in LoF and LoT

Post by BaCaRoZzo »

Hi all,

I've searched the forum and the Internet for clues about this point but, as far as I've searched, I didn't resolve my problem.

I want to had the chapter titles, along with their numbers, in the LoF and LoT. I've found the two commands to do this. Unfortunately none of them suits my needs. If I choose to add, after the chapter declaration, the command

Code: Select all

\addcontentsline{lof}{chapter}{\protect\numberline{\arabic{chapter}} {Introduction}}
The chapter is correctly set BUT also the page number is displayed and hyperlinked (hyperref).
If I use the following one, instead,

Code: Select all

\addtocontents{lof}{\protect\numberline{\thechapter}{Introduction}}
the chapter is not inserted in bold text (as previously) and no hyperlinked page number is shown BUT a line break is generated. In other words, the chapter number is set on a row and the chapter title is printed in the subsequent line.

Basically what I want is to insert the title in bold with the chapter number and without the corresponding page number. What can be wrong with the second command? Can I use the first command forcing the hiding of the page number?

As a second point. I would like to typeset this automatically. E.g. I would like that the

Code: Select all

\chapter
command calls the chapter addition to the list of figures/tables. Or something like that. Is it possible?

Thanks in advance for your kind answers.
Best,
F.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Chapter Headings in LoF and LoT

Post by 5gon12eder »

The first thing is very simple. Just replace

Code: Select all

\addcontentsline{lof}{chapter}{\protect\numberline{\arabic{chapter}} {Introduction}}
with

Code: Select all

\addcontentsline{lof}{chapter}{\protect\numberline{\thechapter\quad{}Introduction}}
i.e., put everything into one \numberline. (I've also added a \quad space and wrote \thechapter which is generally the preferred way since it allows a user to modify the style more flexible.)

If you want to do things more automatically, it gets a little tricky. I don't think that appending some code to the definition of \chapter would do what one expects. For example, it'd add entries
0 Contents
0 List of Figures
0 List of Tables
etc. to the list of figures which is probably not what you want.

If you only want chapters to appear that actually contain some figures, I think it is safer to let \caption do the work. In the following suggestion, using Philipp Lehman's etoolbox package, I define the counter chapter@last that stores the number of the last chapter added to the LoF. If it matches the current chapter number, \caption won't do anything special, otherwise it adds a line with the current chapter name. This name is stored in the macro \thechaptername which is defined in the redefinition of \chaptermark.

Code: Select all

\usepackage{etoolbox}

\makeatletter

\newcommand{\thechaptername}{}
\newcounter{chapter@last}

\renewcommand{\chaptermark}[1]
             {%
               \markboth{#1}{}%
               \renewcommand{\thechaptername}{#1}%
             }

\pretocmd{\caption}
         {%
           \ifnumequal%
               {\value{chapter}}%
               {\value{chapter@last}}%
               {}%  already added
               {%
                 \addtocontents{lof}%
                               {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%
                 \setcounter{chapter@last}{\value{chapter}}%
               }%
         }
         {}
         {}%  failed, could put some error message here...

\makeatother
Best
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Chapter Headings in LoF and LoT

Post by BaCaRoZzo »

Hi 5gon12eder,
thanks for your kind reply.

As soon as I submitted my question I realized that adding code to \chapter would make no sense. Indeed it is not so obvious that every chapter has at least a figure.

That said, I read your suggestion and implemented your code: it worked like a charm. The used package is really powerful, never heard about it! Good point. :D

Understood your code I have added it to my style file. The command about the header have been moved to the fancy header part. The "core" part of your solution has been changed to a command, which is called at a certain point. A variable CHAPTERS is evaluated so it can be chosen if the chapters are added or not to the LoFT, via such variable.

Here's the code:

Code: Select all

\newcommand{\chapters}{
\ifbool{CHAPTERS}{
\newcounter{chapterF@last}
\newcounter{chapterT@last}

\pretocmd{\figure}
         {%
           \ifnumequal%
               {\value{chapter}}%
               {\value{chapterF@last}}%
               {}%  already added
               {%
                 \addtocontents{lof}%
                               {\vspace{5pt}}%
                 \addtocontents{lof}%
                               {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%             
                 \setcounter{chapterF@last}{\value{chapter}}%
               }%
         }
         {}
         {}%  failed, could put some error message here...
%        
%        
\pretocmd{\table}
         {%
           \ifnumequal%
               {\value{chapter}}%
               {\value{chapterT@last}}%
               {}%  already added
               {%
                 \addtocontents{lot}%
                               {\vspace{5pt}}%
                 \addtocontents{lot}%
                               {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%             
                 \setcounter{chapterT@last}{\value{chapter}}%
               }%
         }
         {}
         {}%  failed, could put some error message here...        
}
{}
}
As you can see I check the \figure and \table commands so that I can add chapter to both LoF and LoT. It works and I'm ok with it.

Now the question part.
1) Do you think there is a stronger and safer way to implement it or is it a valid solution? I mean, Did I do the wrong choice in checking \figure and \table? I actually cannot see other solutions.
2) Partially related to the first question. If I put your(mine) code somewhere in the class file, without the variable check, it works ok. If I wrap the code inside an if statement (like the one in the code above) it does not work either if the variable is true or false. I've solved the problem by wrapping the if stamentent in the command \chapters as you can see above. Hence, the question: why the if statement works only inside a command environment? What was wrong with that?

Thanks again.
Bests,
F.
BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Re: Chapter Headings in LoF and LoT

Post by BaCaRoZzo »

Ok,

I've got the answer to the second STUPID question.
Sorry for that.
I didn't realized that the code is executed BEFORE the evaluated variable is set in the main file. Again, sorry for that.

My first question still remains.
Thanks,
F.
5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Chapter Headings in LoF and LoT

Post by 5gon12eder »

Glad to see it worked for you and -- even better -- you were able to understand and extend the suggested solution.

I think it's fairly acceptable but if you wish, you could make it a little more portable. For example, you could make it a little package and share the code among multiple documents. Also, you needn't duplicate the code for each floating environment if you write a more generic command that does the hooking. Finally, note the \AtBeginEnvironment command from the etoolbox package.

Here is a suggestion for a very small package. Just name it chaptersinindex.sty and put it somewhere LaTeX can find it.

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{chaptersinindex}[2012/01/21]

\RequirePackage{etoolbox}

\providecommand{\thechaptername}{}
\renewcommand{\chaptermark}[1]
             {%
               \markboth{#1}{}%
               \renewcommand{\thechaptername}{#1}%
             }


\newcommand%
    {\AddChaptersToIndex}[2]%  #1: index name (e.g. `lof')
                            %  #2: environment to patch (e.g. `figure')
    {%
      \newcounter{chapter@last@#1}
      \AtBeginEnvironment
          {#2}
          {
            \ifnumequal%
                {\value{chapter}}%
                {\value{chapter@last@#1}}%
                {}%  already added
                {%
                  \addtocontents{#1}{\protect\numberline{\vspace{\BeforeChapterInIndex}}}%
                  \addtocontents%
                      {#1}%
                      {\protect\numberline{\ChapterInIndex{\thechapter}{\thechaptername}}}%
                      \setcounter{chapter@last@#1}{\value{chapter}}%
                  \addtocontents{#1}{\protect\numberline{\vspace{\AfterChapterInIndex}}}%
                }
          }
    }

\newlength{\BeforeChapterInIndex}
\setlength{\BeforeChapterInIndex}{0ex plus 1ex}
\newlength{\AfterChapterInIndex}
\setlength{\AfterChapterInIndex}{0ex}

\newcommand{\ChapterInIndex}[2]%  #1: number,  #2: title
           {{\bfseries#1\quad#2}}
Then in your document, the relevant code reduces to

Code: Select all

\usepackage{chaptersinindex}

\AddChaptersToIndex{lof}{figure}
\AddChaptersToIndex{lot}{table}
If the user (you) wish, she can redefine the macro \ChapterInIndex that takes the chapter number as its first and the chapter name as its second argument and typesets them in the indices. Also, the dimensions \BeforeChapterInIndex and \AfterChapterInIndex can be changed by the user to adjust the spacing before and after the chapter entries. Note however that these dimensions add to the height of a single line of text. You could make the solution even more flexible by allowing the heading level (\chapter at the moment) to be specified as a package option so the package would also be useful to an article, say.

The posted solution is not so nice however in the way that the \renewcommand{\chaptermark}[1]... stuff is likely to be broken by a user who uses customized headings. Maybe you have a better idea for implementing that.

Happy TeXing!
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
BaCaRoZzo
Posts: 10
Joined: Sun Dec 18, 2011 7:53 pm

Re: Chapter Headings in LoF and LoT

Post by BaCaRoZzo »

Hi again Egon and thanks for your time! Your suggestions are always inspiring. :)

I can just add that I TOTALLY agree with your point. I also was a little afraid for the \chaptermark breakable code. About the duplicated code, actually I didn't think about that! I could have been smarter...

I'm working on a thesis template for my PhD course and I would love to make it as usable as possible but also robust. Since only ME is using the template right now, I'm going to concentrate on my thesis: time is running out! :o

I partially implemented your code (the parametrization and the usage of the "AtBeginEnvironment" command). I will fully implement your suggestions as soon as the template will be released as the official template. Credits will also go to you! ;)

I got other two questions. As you have seen, I added a little spacing (cleverly transformed by you in commands "BeforeChapterInIndex" and "AfterChapterInIndex"). It is actually a workaround for not splitting a chapter title and its figures/tables. I try to explain better. Currently, rows are print to fill the page (obviously). If a chapter title is print at the end of a page and there's no more space, its figures/tables are printed on the next page. Which is quite ugly in my opinion. I'm not aware of TOC behaviour, but I would love to force chapter printing to the new page IF it is not possible to print AT LEAST a picture/table reference under it, in the current page. Is there a way to do that? Some already made code that can borrowed?

Another reason for adding space was that the distance between chapter in TOC and in LOFT seemed greater. Is there a specific variable defining the distance in TOC? In case that would be the default value for LOFT.

Thanks again for your time. My apologies for the bad English.
Bests,
F.
5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Chapter Headings in LoF and LoT

Post by 5gon12eder »

You can tell LaTeX that a certain place would be a good place for a page break via \pagebreak[n] where n is a number between 1 and 4. The higher the better for breaking. Similairly, \nopagebreak[n] does quite the opposite. I think that should work for your purpose. Simply add it to the \vspace.
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
rsze
Posts: 1
Joined: Mon Sep 14, 2020 6:42 pm

Chapter Headings in LoF and LoT

Post by rsze »

I could sucessfully include also sections by simply exchanging "chapter" with "section" in the .sty file. However, it is not working for subsections and subsubsections. Can anyone help?
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Chapter Headings in LoF and LoT

Post by Ijon Tichy »

What is "it"?

Adding the headings to LoF or LoT should also work for subsections and subsubsections:

Code: Select all

\documentclass{article}
\usepackage{mwe}
\begin{document}
\tableofcontents
\listoffigures
\section{Testsection}
\addcontentsline{lof}{section}{\protect\numberline{\thesection}Testsection}
\blindtext

\subsection{Testsubsection}
\addcontentsline{lof}{subsection}{\protect\numberline{\thesubsection}Testsubsection}
\blindtext

\subsubsection{Testsubsubsection}
\addcontentsline{lof}{subsubsection}{\protect\numberline{\thesubsubsection}Testsubsubsection}
\blindtext

\begin{figure}
  \caption{Testfigure}
\end{figure}
\end{document}
Depending in the used class, it could be needed to increase counter secnumdepth. For LoT and LoF this is the same as for ToC.

So please show us a Infominimal working example that can reproduce your problem.

BTW: I do not think, that is make sense to add all sectioning headings to the LoF or LoT.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply