# Separate List of Figures and List of Tables by chapter

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

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

• Welcome to TeX.SE. Fragments of code are not useful. – user31729 Oct 26 '17 at 17:44
• Thanks for the hint. I've edited the question showing the entire code now. – berlinesther Oct 27 '17 at 5:53
• There you can find an example (in German) how to do so using scrreprt instead of report. – Schweinebacke Oct 27 '17 at 6:04
• Thanks, but the solution below worked very well. – berlinesther Oct 27 '17 at 8:16

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
\fi
\fi
\ifnum\pdfstrcmp{\@captype}{table}=0
\ifnum\value{chapter}=\value{chapter@last@table}\else

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.