[Tex/LaTex] Separate List of Figures and List of Tables by chapter

table of contents

I would like to separate the List of Figures and the List of Tables by chapter. I've tried different codes (e.g. Include chapters in List of Figures with titletoc? and Include Chapters have figures with caption In List of Figures), but finally ended up with the following code:

\documentclass[a4paper,oneside,11pt]{report}
\usepackage{etoolbox}

\makeatletter

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

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

\pretocmd{\caption}
 {\ifnumequal
  {\value{chapter}}
  {\value{chapter@last}}
  {}
  {
   \addtocontents{lot}
    {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}
   \addtocontents{lof}
    {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}
   \setcounter{chapter@last}{\value{chapter}}
  }
  }
  {}
  {}

\makeatother

\begin{document}

\tableofcontents
\listoffigures
\listoftables

\newpage

\chapter{Test Chapter with no Figures}

\chapter{Test Chapter with Figures and Tables}
\begin{figure}
\caption{test figure 1}
\end{figure}
\begin{table}
\caption{test table 1}
\end{table}
\begin{figure}
\caption{test figure 2}
\end{figure}
\begin{table}
\caption{test table 2}
\end{table}

\chapter{Test Chapter with no Figures}

\chapter{Test Chapter with Figures only}
\begin{figure}
\caption{test figure 3}
\end{figure}
\begin{figure}
\caption{test figure 4}
\end{figure}
\begin{figure}
\caption{test figure 5}
\end{figure}

\chapter{Test Chapter with Tables only}
\begin{table}
\caption{test table 3}
\end{table}
\begin{table}
\caption{test table 4}
\end{table}
\begin{table}
\caption{test table 5}
\end{table}

\end{document}

Unfortunately, the code produces the chapter titles of chapters that have no figures or tables in it, as can be seen in the examples below:

List of Figures
List of Tables

Could someone help solve this issue, so the code produces only the chapter titles of the chapters with figures or tables in it?

Best Answer

I would keep track of the last figure or table separately. So, instead of just using a chapter@last counter, use something like chapter@last@<type> for each float type:

\newcounter{chapter@last@figure}
\newcounter{chapter@last@table}

With this, you can update your prependage to \caption as follows:

\pretocmd{\caption}{
  \ifnum\pdfstrcmp{\@captype}{figure}=0
    \ifnum\value{chapter}=\value{chapter@last@figure}\else
      \addtocontents{lof}
        {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%
    \fi
  \fi
  \ifnum\pdfstrcmp{\@captype}{table}=0
    \ifnum\value{chapter}=\value{chapter@last@table}\else
      \addtocontents{lot}
        {\protect\numberline{\bfseries\thechapter\quad\thechaptername}}%
    \fi
  \fi  
  \expandafter\setcounter\expandafter{chapter@last@\@captype}{\value{chapter}}%
}{}{}

The above conditions on whether \caption is called within a figure or a table. If these are the only two types of floats in your document, one could probably simplify it a little.

The update of each chapter@last@<type> counter is done automatically at the end of the code based on the \@captype.

enter image description here