0

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

\newcommand{\newchapter}[1]{%
\chapter{#1}
\addcontentsline{lof}{chapter}{%
    Chapter \thechapter: #1 \vspace{10pt}
}
\addcontentsline{lot}{chapter}{%
    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
1

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

result

\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}}%
    {}%  already added
    {%
        \ifx\insidefigure\isinside%
        \addtocontents{lof}%
        {\protect\numberline{\bfseries\thechapter\quad\thechapternamef}}%
        \setcounter{chapter@lastf}{\value{chapter}}%
        \fi%
    }%
    {}%
    {}% failed
\ifnumequal%
    {\value{chapter}}%
    {\value{chapter@lastt}}%
    {}%  already added
    {%
        \ifx\insidetable\isinside%
        \addtocontents{lot}%
        {\protect\numberline{\bfseries\thechapter\quad\thechapternamef}}%
        \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.