我希望有一个图表列表,但我希望某些图表和表格不带标签,但在使用时\caption*[short title]{long title}
会遇到问题。请参阅示例:
\documentclass{article}
\usepackage{caption}
\usepackage{imakeidx}
\makeindex
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\section{Introduction}
\begin{table}[hbt]
\caption[Interesting table]{A really interesting table.}
\label{tab:interesting}
So interesting.
\end{table}
\begin{table}[hbt]
\caption*[Boring table]{A really boring table.}
\label{tab:boring}
ARGH, what's going wrong?
\end{table}
\end{document}
我怎样才能解决这个问题?
附言——大多数/全部如果有帮助的话,这些未标记的图表将位于文档的开始或结尾(序言或附录)。
答案1
您可以\caption*{…}
结合使用\addcontentsline
,具体取决于您是否希望未编号的条目与编号条目的表格编号左对齐,还是与未编号条目的文本左对齐,使用以下任一方法:
\addcontentsline{\csname ext@table\endcsname}{table}{Boring table}
或者
\addcontentsline{\csname ext@table\endcsname}{table}{\numberline{}Boring table}
这里有一个例子来说明差异:
\documentclass{article}
\usepackage{caption}
\usepackage{imakeidx}
\makeindex
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\section{Introduction}
\begin{table}[hbt]
\caption[Interesting table]{A really interesting table.}
\label{tab:interesting}
So interesting.
\end{table}
\begin{table}[hbt]
\caption*{A really boring table.}
% Use only one of the following lines:
\addcontentsline{\csname ext@table\endcsname}{table}{Boring table}
\addcontentsline{\csname ext@table\endcsname}{table}{\numberline{}Boring table}
\label{tab:boring}
ARGH, what's going wrong?
\end{table}
\end{document}
我使用了\csname ext@table\endcsname
而不是lot
,因为 table 的辅助文件的扩展名可能会改变。例如,附录中有单独的表格列表的解决方案,它们使用了 的这种重新定义\ext@table
。
还请注意,\label{tab:boring}
在示例中没有意义。软件包caption
向添加了有关该问题的警告log
:
Package caption Warning: \label without proper reference on input line 20. See the caption package documentation for explanation.
和\ref{tab:boring}
或\pageref{tab:boring}
将导致:
LaTeX Warning: Reference `tab:boring' on page 1 undefined on input line ….
为了避免此类问题,您可以使用类似以下方法:
\documentclass{article}
\usepackage{caption}
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\section{Introduction}
\begin{table}[hbt]
\caption[Interesting table]{A really interesting table.}
\label{tab:interesting}
So interesting.
\end{table}
\begin{table}[hbt]
\caption*{A really boring table.}
% another suggestion for the table number in the list of tables
\addcontentsline{\csname ext@table\endcsname}{table}{\numberline{n.n.}Boring table}
% avoid problems with \label and \ref
\makeatletter
\def\@currentlabel{without number}% use text "without number" instead of number for the label
\makeatother
\label{tab:boring}
ARGH, what's going wrong?
\end{table}
See table~\ref{tab:boring} on page \pageref{tab:boring}.
\end{document}
为了使最后的解决方案更加实用,我建议您定义自己的命令\nonumcaption
:
\documentclass{article}
\usepackage{caption}
\usepackage{xparse}
\newcommand*{\nonumbername}{no number}
\newcommand*{\nonumbernumber}{n.n.}
\makeatletter
\DeclareDocumentCommand{\nonumcaption}{ o m }{%
\caption*{#2}%
\IfNoValueTF{#1}
{\addcontentsline{\@nameuse{ext@\@captype}}{\@captype}{\protect\numberline{\protect\nonumbernumber}#2}}%
{\addcontentsline{\@nameuse{ext@\@captype}}{\@captype}{\protect\numberline{\protect\nonumbernumber}#1}}%
\def\@currentlabel{\nonumbername}%
}
\makeatother
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\section{Introduction}
\begin{table}[hbt]
\caption[Interesting table]{A really interesting table.}
\label{tab:interesting}
So interesting.
\end{table}
\begin{table}[hbt]
\nonumcaption[Boring table]{A really boring table.}
\label{tab:boring}
ARGH, what's going wrong?
\end{table}
See table~\ref{tab:boring} on page \pageref{tab:boring}.
\end{document}
现在,可以非常轻松地重新配置文档序言中所有此类表格的结果。您还可以将该命令用于图形或其他浮点数。