在图表列表中添加章节名称

在图表列表中添加章节名称

我需要在表格和图表列表中添加章节名称。我使用了以下代码:

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

}

但是,此代码打印的章节名称也没有任何图表和表格。有什么办法可以解决这个问题吗?提前致谢。

答案1

采取并调整答案这里,我想到了以下几点:

结果

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

相关内容