附录中的图表列表中包含章节失败

附录中的图表列表中包含章节失败

我使用以下问题的第三个答案在文档的图表列表中添加包含章节号的内容行:

是否在带有 titletoc 的图表列表中包含章节?

一切都很好,除了当我添加附录时出现以下错误消息:

! Missing number, treated as zero.! You can't use \numexpr in horizontal mode. \ifnumcomp ...\ifnum \numexpr #1\relax #2\numexpr...

但它编译得很好!所以尽管出现错误消息,它还是给出了所需的结果!

此错误信息是什么原因造成的?我该如何避免它?

我给出原始文件,其中我只添加了附录的命令:

\documentclass{book}
\usepackage{etoolbox}

    \usepackage[toc,page]{appendix}  %% ADDED TO THE ORIGINAL FILE

\makeatletter
\def\thischaptertitle{}
\apptocmd{\@chapter}{\gdef\thischaptertitle{#1}}{}{}

\newcommand{\DeclareDividedList}[1]%
  {\newcounter{#1@chapter}\setcounter{#1@chapter}{0}}

\pretocmd{\addcontentsline}%
  {\ifltxcounter{#1@chapter}%
   {%
     \ifnumgreater{\thechapter}{\value{#1@chapter}}{%
       \setcounter{#1@chapter}{\thechapter}%
       \addtocontents{#1}{\protect\contentsline{chapter}%
         {\protect\numberline {\thechapter} {\thischaptertitle}}{}{} }
     }{}%
   }{}%
  }{}{}
\makeatother

\DeclareDividedList{lof}
\DeclareDividedList{lot}

\usepackage[paperheight=12cm,vscale=0.9]{geometry}
\begin{document}

\tableofcontents
\listoffigures
\listoftables

\mainmatter

\chapter{Introduction with no Figures}

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

\chapter{Test Chapter with Tables but no Figures}
\begin{table}
\caption{caption text}
\end{table}
\begin{table}
\caption{caption text}
\end{table}

\chapter{Test Chapter with Figures and Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
%%% ADDED APPENDIX  
    \begin{appendices}
    \renewcommand{\chaptername}{Appendix}
    \chapter{Appendix One}
    \begin{figure}
    \caption{Figure 1 of appendix 1}
    \end{figure}
    \end{appendices}
\end{document}

得到的结果为:

在此处输入图片描述

答案1

tl;dr:当您在比较中使用计数器或设置计数器时,您需要使用\value{counter},而不是\thecounter。并注意附录中的计数器重置。

\thecounter打印该计数器的文本表示,但这不一定是数字(例如,章节变为 A)。这意味着期望数字的命令看到的是字母(“缺少数字...”),这导致您的输出在标题前出现虚假的“A¿A”。

但是,一旦使用\value{counter},就会遇到另一个问题:由于章节编号在附录中重新开始,您\ifnumgreater不再看到更大的数字,因此不会写入\thischaptertitle。解决方案是重置除法计数器。这导致:

\documentclass{book}
\usepackage{etoolbox}

    \usepackage[toc,page]{appendix}  %% ADDED TO THE ORIGINAL FILE

\makeatletter
\def\thischaptertitle{}
\apptocmd{\@chapter}{\gdef\thischaptertitle{#1}}{}{}

\newcommand{\DeclareDividedList}[1]{%
  \newcounter{#1@chapter}%
  \setcounter{#1@chapter}{0}%
  \AtBeginEnvironment{appendices}{\setcounter{#1@chapter}{0}}% added
}

\pretocmd{\addcontentsline}%
  {\ifltxcounter{#1@chapter}%
   {%
     \ifnumgreater{\value{chapter}}{\value{#1@chapter}}{% \the -> \value{}
       \setcounter{#1@chapter}{\value{chapter}}% \the -> \value{}
       \addtocontents{#1}{\protect\contentsline{chapter}%
         {\protect\numberline {\thechapter} {\thischaptertitle}}{}{} }
     }{}%
   }{}%
  }{}{}
\makeatother

\DeclareDividedList{lof}
\DeclareDividedList{lot}

\usepackage[paperheight=12cm,vscale=0.9]{geometry}
\begin{document}

\tableofcontents
\listoffigures
\listoftables

\mainmatter

\chapter{Introduction with no Figures}

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

\chapter{Test Chapter with Tables but no Figures}
\begin{table}
\caption{caption text}
\end{table}
\begin{table}
\caption{caption text}
\end{table}

\chapter{Test Chapter with Figures and Tables}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
\begin{table}
\caption{caption text}
\end{table}
\begin{figure}
\caption{caption text}
\end{figure}
%%% ADDED APPENDIX  
    \begin{appendices}
    \renewcommand{\chaptername}{Appendix}
    \chapter{Appendix One}
    \begin{figure}
    \caption{Figure 1 of appendix 1}
    \end{figure}
    \end{appendices}
\end{document}

相关内容