[Tex/LaTex] Add both part and chapter titles to list of figures with formatting and line wrapping

formattingtable of contentstitletoc

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.

Best Answer

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