2

In a table of contents style list of figures I would like to combine the appearance of each of these questions.

Part title in list of figures and tables?

Include chapters in List of Figures with titletoc?

Part title

Chapter title

List of Figures in this chapter

Chapter title

List of Figures in this chapter

Part title

Chapter title

List of Figures in this chapter

Chapter title

List of Figures in this chapter

I have tried to combine the parts, but in my MWE below, I don't seem to get the inclusion of the parts titles and I can't seem to change the formatting of these newly added titles. I need to add a little more space between them and make them more visibly different from the titles of the figures I am referencing?

\documentclass{report}
    \usepackage{etoolbox}  % or xpatch
    \usepackage{bookmark}
    \usepackage{hyperref}


\makeatletter

    % initial definitions of the part info (name and number)
    \def\thisparttitle{}\def\thispartnumber{}
    \def\thischaptertitle{}\def\thischapternumber{}

    \newtoggle{noFigs}
    \newtoggle{noTabs}

    \apptocmd{\@chapter}{\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}\global\toggletrue{noFigs}}{}{}
    \apptocmd{\@part}{\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}\global\toggletrue{noFigs}\global\toggletrue{noTabs}}{}{}

    % the figure environment does the job: the first time it is used after a \chapter command, it writes the information of the chapter to the LoF
    \AtBeginDocument{%
        \AtBeginEnvironment{figure}{%
            \iftoggle{noFigs}{
                \addtocontents{lof}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{3.2em} }
                \addtocontents{lof}{\protect\contentsline {chapter}%
                    {\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
                \global\togglefalse{noFigs}
            }{}
        }%
        \AtBeginEnvironment{table}{%
            \iftoggle{noTabs}{
                \addtocontents{lot}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{} }
                \addtocontents{lot}{\protect\contentsline {chapter}%
                    {\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
                \global\togglefalse{noTabs}
            }{}
        }%
    }


\makeatother

\begin{document}

    \tableofcontents
    \listoffigures


    \part{The first}

    \chapter{Introduction with no Figures}

    \chapter{Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

    \chapter{Test Chapter with no Figures}

    \part{The second}

    \chapter{Another Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\end{document}

UPDATE

If I replace the document block of the solution below with this:

\begin{document}

\tableofcontents
\listoffigures


\part{The first}

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\chapter{Test Chapter with no Figures}

\part{The second}

\chapter{Look no figures here either}

\chapter{Another Test Chapter with Figures}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\chapter{Another in the same part}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}

\end{document}

I then get:

LOF snapshot

which duplicates Part titles even when the chapters are sequential (4 and 5 circled in blue to highlight this) and aren't interspersed with a chapter missing figures.

  • You've made a comment but apparently deleted it, something about grouping? – user31729 Jul 2 '15 at 20:41
  • @ChristianHupfer - Yeah, sorry about that. I was debating whether I would post it as a distinct question or not. I got an excellent answer more common to the spirit of the original attempts I had made using the code in the linked questions, and started to feel sort of bad about asking to change it, so I deleted it. I will restate it below in case it's actually a trivial change. – EngBIRD Jul 3 '15 at 1:57
  • @ChristianHupfer - My grouping question was pointing out that I made a bad example in the MWE. The block quote is actually more relevant. If I include 2 chapters with figures in a single part, using your solution below I get a repetition of the common part title above each chapter. As the quote block above describes it would be best if there was not repetition. – EngBIRD Jul 3 '15 at 2:00
  • I am confused. Do you really use my MWE or is there anything in the real document you want to obtain that changes something? I'll add a screenshot about the output of my 'solution'. There is only one part title per chapter group. However, the culprit is, that there is also a part title when chapters without figures are included. That should be changed, in my point of view. – user31729 Jul 3 '15 at 6:45
  • @ChristianHupfer - "There is also a part title when chapters without figures are included". I am a little surprised at this behaviour, I guess I don't fully understand what is responsible for this. I only have 1 part in my document that has figures, and they are in two sequential chapters. This problem would make sense if there was a chapter without figures in between to chapters, but I guess it does make a certain amount of sense that any missing chapter would mess up the counters... I am updating my question above to include the screenshot your solution MWE output gives me. – EngBIRD Jul 3 '15 at 13:44
3

The \contentsline stuff is wrongly used in the O.P.'s document, i.e. wrongly placed {} delimiters.

For example

 \addtocontents{lof}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{3.2em} }

tries to write a \numberline with the part title as the number, leaving out the title and setting the page number to be 3.2em which is in fact a length.

One correct way is

\addtocontents{lof}{%
\protect\contentsline{part}{
  \protect\numberline{\thispartnumber}\thisparttitle}{% page number group -- empty here 
}% end of \contentsline
}% end of \addtocontents

The same holds true for the chapter titles and the LoT.

\documentclass{report}
\usepackage{etoolbox}  % or xpatch

\makeatletter

    % initial definitions of the part info (name and number)
\def\thisparttitle{}\def\thispartnumber{}
\def\thischaptertitle{}\def\thischapternumber{}

\newtoggle{noFigs}
\newtoggle{noTabs}

\apptocmd{\@chapter}{\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}\global\toggletrue{noFigs}}{}{}
\apptocmd{\@part}{\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}\global\toggletrue{noFigs}\global\toggletrue{noTabs}}{}{}

% the figure environment does the job: the first time it is used after a \chapter command, it writes the information of the chapter to the LoF
\AtBeginDocument{%
    \AtBeginEnvironment{figure}{%
        \iftoggle{noFigs}{%
          \addtocontents{lof}{\protect\contentsline{part}{\protect\numberline{\thispartnumber}\thisparttitle}{}}%
          \addtocontents{lof}{\protect\contentsline{chapter}{\protect\numberline{\thischapternumber}\thischaptertitle}{}}%
          \global\togglefalse{noFigs}%
        }{}
    }%
    \AtBeginEnvironment{table}{%
        \iftoggle{noTabs}{%
          \addtocontents{lot}{\protect\contentsline{part}{\protect\numberline{\thispartnumber}\thisparttitle}{}}%
          \addtocontents{lot}{\protect\contentsline{chapter}{\protect\numberline{\thischapternumber}\thischaptertitle}{}}%
          \global\togglefalse{noTabs}%
        }{}
      }%
}


\makeatother

\begin{document}

\tableofcontents
\listoffigures


\part{The first}

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}

\chapter{Test Chapter with no Figures}

\part{The second}

\chapter{Another Test Chapter with Figures}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}

\end{document}

enter image description here

Edit

Now with hyperref support, but without showing the page number for chapters and parts. (Please note: due to the toggle style, this won't work if a figure is not in a chapter!) The better way would to ask for the figure number.

\documentclass{report}

\usepackage{xpatch}


\makeatletter

% initial definitions of the part info (name and number)
\def\thisparttitle{}\def\thispartnumber{}
\def\thischaptertitle{}\def\thischapternumber{}

\newtoggle{noFigs}
\newtoggle{noTabs}
\newtoggle{newpart}


\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
  \xdef\thisparttitle{#1}%    Evaluate the parameter #1
  \global\togglefalse{noFigs}%
  \global\togglefalse{noTabs}%
  \global\toggletrue{newpart}%
}{\typeout{Patch success}}{\typeout{Patch failure}}


\xpatchcmd{\@chapter}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}% 
  \xdef\thischaptertitle{#1}%  Evaluate the parameter #1
  \global\togglefalse{noFigs}%
  \global\togglefalse{noTabs}%
}{\typeout{Patch success}}{\typeout{Patch failure}}
\makeatother



\usepackage{hyperref}

\AtBeginDocument{%
  \AtBeginEnvironment{figure}{%
    \nottoggle{noFigs}{%
      \iftoggle{newpart}{%
      \addtocontents{lof}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}}%
      \global\togglefalse{newpart}%
      }{}%
      \addtocontents{lof}{\protect\contentsline{chapter}{\protect\numberline{\thechapter}\thischaptertitle}{}{chapter.\theHchapter}}%
      \global\toggletrue{noFigs}
    }{}
  }

  \AtBeginEnvironment{table}{%
    \nottoggle{noTabs}{%
      \iftoggle{newpart}{%
      \addtocontents{lot}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}}%
      \global\togglefalse{newpart}%
      }{}%
      \addtocontents{lot}{\protect\contentsline{chapter}{\protect\numberline{\thechapter}\thischaptertitle}{}{chapter.\theHchapter}}%
      \global\toggletrue{noTabs}%
    }{}
  }
}


\begin{document}

\tableofcontents
\listoffigures
\listoftables

\part{The first}

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
\begin{figure}
    \caption{caption text}
\end{figure}
\begin{figure}
    \caption{caption text}
\end{figure}

\chapter{Test Chapter with no Figures}

\part{The second}

\chapter{Look no figures here either}


\chapter{Another Test Chapter with Figures}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}

\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}

%\chapter{Another chapter, without figures}


\chapter{Another in the same part}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}




\part{Without figs}

\chapter{A chapter with a table}

\begin{table}
\caption{Some table}
\end{table}

\end{document}

enter image description here

  • It appears your solution is incompatible with the bookmark and hyperref packages. cleveref is fine however. Any thoughts? – EngBIRD Jul 2 '15 at 1:17
  • @EngBIRD: The problem is the \addtocmd stuff, which I used unchanged from your example, so the problem is already there. I'll try to change this – user31729 Jul 2 '15 at 6:34
  • @EngBIRD: See my update please – user31729 Jul 2 '15 at 7:28
  • Thanks, I still can't compile your code with your solution in the presence of hyperref and bookmark. I have updated my MWE's preamble, but not the actual area that your code targets to fix. Basically it's saying that chapter doesn't match it's definition. – EngBIRD Jul 2 '15 at 13:05
  • 1
    @EngBIRD: No worries about excluding etoolbox. xpatch loads it anyway, since egreg (the author of xpatch) extends etoolbox actually – user31729 Jul 2 '15 at 14:02

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.