包括章节中有带标题的图表的图表列表

包括章节中有带标题的图表的图表列表

我正在使用代码是否在带有 titletoc 的图表列表中包含章节?将章节包含在 LOF 中。问题是有些章节可能最多只有一张图,但没有标题。如何忽略这些章节?代码如下:

\documentclass{book}
\usepackage{etoolbox}  % or xpatch

\makeatletter
% initial definitions of the chapter info (name and number)
\def\thischaptertitle{}\def\thischapternumber{}
\newtoggle{noFigs}

\apptocmd{\@chapter}%
  {\gdef\thischaptertitle{#1}\gdef\thischapternumber{\thechapter}%
    \global\toggletrue{noFigs}}{}{}

% the figure environment does the job: the first time it is used after a \chapter command, 
% it writes the information of the chapter to the LoF
\AtBeginDocument{%
  \AtBeginEnvironment{figure}{%
    \iftoggle{noFigs}{
      \addtocontents{lof}{\protect\contentsline {chapter}%
        {\protect\numberline {\thischapternumber} {\thischaptertitle}}{}{} }
      \global\togglefalse{noFigs}
    }{}
  }%
}

\makeatother

\begin{document}

\tableofcontents
\listoffigures

\mainmatter

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}

\chapter{Test Chapter with ‎only ‎one‎ Figure and no caption}
\begin{figure}
\rule{1cm}{1cm}
\end{figure}

\chapter{Another Test Chapter with Figures}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{figure}
\caption{caption text}
\end{figure}

\end{document}

在此处输入图片描述

答案1

我重复使用了我对这个问题的回答中的代码将部分和章节标题添加到带有格式和换行功能的图表列表中

并且刚刚添加了一个条件,如果figure计数器大于零,\caption则已使用 a(最有可能)。显然,这需要每章重置图形计数器,这在标准类中是正确的(除非未使用或chngcntr更改\@remfromreset

请注意,没有图表或有图表但没有标题的章节不会添加到 LoF!

\documentclass{book}

\usepackage{xpatch}

\makeatletter


% initial definitions of the part info (name and number)
\def\thisparttitle{}\def\thispartnumber{}
\def\thischaptertitle{}\def\thischapternumber{}

\newtoggle{noFigs}
\newtoggle{noTabs}
\newtoggle{newpart}



\xpatchcmd{\@part}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
  \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
  \xdef\thisparttitle{#1}%    Evaluate the parameter #1
  \global\togglefalse{noFigs}%
  \global\togglefalse{noTabs}%
  \global\toggletrue{newpart}%
}{\typeout{Patch success}}{\typeout{Patch failure}}


\xpatchcmd{\@chapter}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}% 
  \xdef\thischaptertitle{#1}%  Evaluate the parameter #1
  \global\togglefalse{noFigs}%
  \global\togglefalse{noTabs}%
}{\typeout{Patch success}}{\typeout{Patch failure}}
\makeatother




\usepackage{hyperref}


\makeatletter

\AtBeginDocument{%
  \AfterEndEnvironment{figure}{%
    \nottoggle{noFigs}{%
      \ifnumgreater{\value{figure}}{0}{%
      \iftoggle{newpart}{%
        \addtocontents{lof}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}}%
        \global\togglefalse{newpart}%
      }{}%
        \addtocontents{lof}{\protect\contentsline{chapter}{\protect\numberline{\thechapter}\thischaptertitle}{}{chapter.\theHchapter}}%
        \global\toggletrue{noFigs}
       }{}%
    }{}%
  }

  \AfterEndEnvironment{table}{%
    \nottoggle{noTabs}{%
      \ifnumgreater{\value{table}}{0}{%
        \iftoggle{newpart}{%
          \addtocontents{lot}{\protect\contentsline{part}{\thepart\hspace{1em}\thisparttitle}{}{part.\theHpart}}%
          \global\togglefalse{newpart}%
        }{}%
        \addtocontents{lot}{\protect\contentsline{chapter}{\protect\numberline{\thechapter}\thischaptertitle}{}{chapter.\theHchapter}}%
        \global\toggletrue{noTabs}%
      }{}%
    }{}
  }%
}
\makeatother

\begin{document}

\tableofcontents
\listoffigures
\listoftables

\part{The first}

\chapter{Introduction with no Figures}

\chapter{Test Chapter with Figures}
\begin{figure}
    \caption{caption text Number one}
\end{figure}
\begin{figure}
    \caption{caption text Number two}
\end{figure}


\chapter{A chapter with figure, but no caption}
\begin{figure}
% One without figure
\end{figure}

\chapter{Test Chapter with no Figures}

\part{The second}

\chapter{Look no figures here either}


\chapter{Another Test Chapter with Figures}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}

\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}
\begin{figure}
    \caption{caption text from chapter \thechapter}
\end{figure}

\begin{figure}
% No caption
\end{figure}


\chapter{Another in the same part}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}
    \begin{figure}
        \caption{caption text}
    \end{figure}




\part{Without figs}

\chapter{A chapter with a table}

\begin{table}
\caption{Some table}
\end{table}

\end{document}

在此处输入图片描述

相关内容