# Add Chapter name in the list of figures and list of tables

I need to add the chapter name in the list of tables and figures. I used this code:

\newcommand{\newchapter}[1]{%
\chapter{#1}
Chapter \thechapter: #1 \vspace{10pt}
}
Chapter \thechapter: #1 \vspace{10pt}
}


}

However, this code prints the chapter names that also do not have any figures and tables. Any way to go around it? Thanks in advance.

• Can you provide a complete Minimal Working Example (MWE) including \documentclass and \begin{document} etc.? Also, I am not sure how the desired output should look like, can you elaborate on that? – carlosvalderrama Jun 26 '18 at 22:34

Taking and adjusting the answer from here, I came up with the following:

\documentclass{book}
\usepackage{etoolbox}

\makeatletter

\newcommand{\thechapternamef}{}
\newcounter{chapter@lastf}
\newcommand{\thechapternamet}{}
\newcounter{chapter@lastt}

\newcommand{\insidefigure}{}
\newcommand{\insidetable}{}
\newcommand{\isinside}{1}
\let\oldfigure\figure
\def\figure{\oldfigure\renewcommand{\insidefigure}{1}}
\let\oldendfigure\endfigure
\def\endfigure{\oldendfigure\renewcommand{\insidefigure}{}}
\let\oldtable\table
\def\table{\oldtable\renewcommand{\insidetable}{1}}
\let\oldendtable\endtable
\def\endtable{\oldendtable\renewcommand{\insidetable}{}}

\renewcommand{\chaptermark}[1]
{%
\markboth{#1}{}%
\renewcommand{\thechapternamef}{#1}%
\renewcommand{\thechapternamet}{#1}%
}

\pretocmd{\caption}
{%
\ifnumequal%
{\value{chapter}}%
{\value{chapter@lastf}}%
{%
\ifx\insidefigure\isinside%
\setcounter{chapter@lastf}{\value{chapter}}%
\fi%
}%
{}%
{}% failed
\ifnumequal%
{\value{chapter}}%
{\value{chapter@lastt}}%
{%
\ifx\insidetable\isinside%
\setcounter{chapter@lastt}{\value{chapter}}%
\fi%
}%
{}%
{}%  failed, could put some error message here...
}%
\makeatother

\begin{document}
\listoffigures
\listoftables
\chapter{no. 1}
\begin{figure}
content...
\caption{this is a fig.}
\end{figure}

\chapter{no. 2}
\begin{table}
content...
\caption{this is a table.}
\end{table}

\chapter{no. 3}
\begin{figure}
content...
\caption{this is also a fig.}
\end{figure}
\end{document}