7

I am using the code in Include chapters in List of Figures with titletoc? to include chapters in the LOF. The problem is that some chapters may have at most one figure but with no caption. How to ignore these chapters? Blew is the code:

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

\makeatletter
% initial definitions of the chapter info (name and number)
\def\thischaptertitle{}\def\thischapternumber{}
\newtoggle{noFigs}

\apptocmd{\@chapter}%
  {\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}%
    \global\toggletrue{noFigs}}{}{}

% 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 {chapter}%
        {\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
      \global\togglefalse{noFigs}
    }{}
  }%
}

\makeatother

\begin{document}

\tableofcontents
\listoffigures

\mainmatter

\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 ‎only ‎one‎ Figure and no caption}
\begin{figure}
\rule{1cm}{1cm}
\end{figure}

\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

1
  • Perhaps my second answer (the one at the bottom of my post there) to this question here (tex.stackexchange.com/questions/253223/…) might be a solution for your problem too. The page-numbers can be added again ;-) It has to be changed of course for the non-caption-figure - feature
    – user31729
    Jul 16, 2015 at 17:26

1 Answer 1

3

I've reused the code from my answer to this question Add both part and chapter titles to list of figures with formatting and line wrapping

and just added a condition if figure counter is greater than zero, a \caption has been used (most likely). Clearly, this requires a per chapter resetting of the figure counter, which is true in standard classes (and unless not changed with chngcntr or \@remfromreset)

Note that the chapters without figures or with figures but without caption, they aren't added to the LoF!

\documentclass{book}

\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}


\makeatletter

\AtBeginDocument{%
  \AfterEndEnvironment{figure}{%
    \nottoggle{noFigs}{%
      \ifnumgreater{\value{figure}}{0}{%
      \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}
       }{}%
    }{}%
  }

  \AfterEndEnvironment{table}{%
    \nottoggle{noTabs}{%
      \ifnumgreater{\value{table}}{0}{%
        \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}%
      }{}%
    }{}
  }%
}
\makeatother

\begin{document}

\tableofcontents
\listoffigures
\listoftables

\part{The first}

\chapter{Introduction with no Figures}

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


\chapter{A chapter with figure, but no caption}
\begin{figure}
% One without figure
\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}

\begin{figure}
% No caption
\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}




\part{Without figs}

\chapter{A chapter with a table}

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

\end{document}

enter image description here

4
  • First chapter in List of Tables have not title! please check. Jul 20, 2015 at 18:27
  • @MathMan: Yes, I've forgotten to change \AtEndEnvironment to \AfterEndEnvironment for the table environment. Thanks for notifying me
    – user31729
    Jul 20, 2015 at 18:46
  • Not worked! Mr Hupfer Jul 20, 2015 at 18:56
  • @MathMan: Not worked is not very helpful. I can clearly see a chapter title in the list of tables
    – user31729
    Jul 20, 2015 at 19:06

You must log in to answer this question.

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