[Tex/LaTex] Include Chapters have figures with caption In List of Figures

chapterstable of contents

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

Best Answer

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