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:
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
ortable
separately. So, instead of just using achapter@last
counter, use something likechapter@last@<type>
for each float type:With this, you can update your prependage to
\caption
as follows:The above conditions on whether
\caption
is called within afigure
or atable
. 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
.