将表格添加到具有自定义章节细分的表格列表中

将表格添加到具有自定义章节细分的表格列表中

运行下面的代码应该会修改我的表格列表,以显示与该章节相关的每组表格上方的章节编号。

\usepackage{etoolbox}

\preto\table{%
  \ifnum\value{table}=0\addtocontents{lot}{{\bfseries Chapter \thechapter\vskip10pt}}\fi
}

然而,由于某些不为人知的原因,它还会在没有表格的部分添加章节编号。运行代码的输出如下所示:

Chapter 0
Chapter 0
Chapter 2
  2.1  Table name                                 page number
  2.2  Table name                                 page number
Chapter 3
  3.1  Table name                                 page number
Chapter 6

其中没有第 0 章,第 6 章是最后一章。它显示了第 2 章和第 3 章中正确数量的表格,但我不确定为什么它还包括没有表格的第 0 章和第 6 章。

我已找到问题的根源,即标题页和附录中的表格。有没有办法修改上述\preto语句以忽略某些\include文件?

答案1

与@MMA一样,我无法使用以下MWE重现症状(并且LoT中除第一章标题之外的所有标题都缩进不正确)。请查看您的文档与此文档之间的区别,以缩小问题发生的位置:

\documentclass{report}
\usepackage{etoolbox}

\preto\table{%
  \ifnum\value{table}=0\addtocontents{lot}{{\bfseries Chapter \thechapter\vskip10pt}}\fi
}

\begin{document}

\listoftables

\chapter{This chapter has no tables}
\chapter{This chapter also has no tables}
\chapter{This chapter has tables}
\begin{table} \caption{Some caption} \begin{tabular}{l}0\end{tabular} \end{table}
\begin{table} \caption{Some other caption} \begin{tabular}{l}0\end{tabular} \end{table}
\chapter{Yet another table-free chapter}
\chapter{Followed by another chapter with tables}
\begin{table} \caption{Third caption} \begin{tabular}{l}0\end{tabular} \end{table}
\begin{table} \caption{Fourth caption} \begin{tabular}{l}0\end{tabular} \end{table}
\chapter{Third chapter with tables}
\begin{table} \caption{Fifth caption} \begin{tabular}{l}0\end{tabular} \end{table}
\begin{table} \caption{Sixth caption} \begin{tabular}{l}0\end{tabular} \end{table}

\end{document}

从您编辑的示例开始,我不得不清理一些东西才能使其编译,但我仍然无法重现症状。一定是有其他原因导致了这个问题。

\documentclass[12pt,twoside]{report}
% appendix, tocloft, etoolbox, titlesec, amsmath, and smartref appear to be irrelevant.
% Commented or not, the LoT problem hasn't yet appeared.
\usepackage[titletoc,title]{appendix}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage{titlesec}
\usepackage{amsmath} \numberwithin{figure}{chapter}
\usepackage{smartref} \addtoreflist{chapter}
\setcounter{tocdepth}{2}

\preto\table{% Both these \preto commands appear to be irrelevant.
  \ifnum\value{table}=0\addtocontents{lot}{{\bfseries Chapter \thechapter\vskip10pt}}\fi
}
\preto\section{%
  \ifnum\value{section}=0\addtocontents{toc}{\vskip10pt}\fi
}

\begin{document}

%\begin{preliminary} % This environment is currently undefined.
\tableofcontents
\addcontentsline{toc}{chapter}{List of Tables}
\listoftables
%\end{preliminary}

\chapter{One} \chapter{Two}

\chapter{Three}
Here's a table.
\begin{table}[ht]
\begin{center}
\begin{tabular}[ht]{|c|lr|c|} 
%c stands for centre, l for left, r for right; the | puts lines in between, and the hline puts a horizontal line in
\hline
$n$ & $\alpha$ &$n\alpha$ & $\beta$\\
\hline
1 & 0.2 & 0.2 & 5\\
\hline
2 & 0.3 & 0.6 & 4\\
\hline
3 & 0.7 & 2.1 & 3\\
\hline
\end{tabular}
\caption{A random table \label{tab1}}
\end{center}
\end{table}

\chapter{Four} \chapter{Five} \chapter{Six}
\section{Section}
\end{document}  

相关内容