![标题周围的间距:文档类的作用](https://linux22.com/image/250757/%E6%A0%87%E9%A2%98%E5%91%A8%E5%9B%B4%E7%9A%84%E9%97%B4%E8%B7%9D%EF%BC%9A%E6%96%87%E6%A1%A3%E7%B1%BB%E7%9A%84%E4%BD%9C%E7%94%A8.png)
我有一张如下表所写:
\begin{table}
\centering
\begin{tabular}{|l||c|c|}
\hline&Column 1&Column 2\
\hline\hline
cell1 & cell2 & cell3 \\
\hline
cell1 & cell2 & cell3 \\\hline
\end{tabular}
\caption{A test caption}
\label{tab:attempt1}
\end{table}
现在标题非常靠近表格。另外,标题后面有一个很大的垂直空白...我该如何修复它,使其看起来像任何带有标题的普通表格?
只有表格才是这种情况,图形才能正常工作。
答案1
答案2
引起我注意的是另一个已发布在 TeX-LaTeX Meta 上。尽管这个问题已经问了将近六年,但——当然,恕我直言!——它仍然吸引着糟糕的答案,目前为止发布的所有答案中也没有令人满意的答案,包括此问题的前一个版本。这种说法可能听起来像是一种严厉的批评,显然需要充分的论证,我将在下面尝试提供;但我首先要明确一点,由于标题周围的间距可能取决于许多相互关联的因素(首先是文档类的选择),因此对问题的全面分析会让我们走得太远。在这个答案中,我选择专注于包如何caption
帮助控制间距,并在必要时覆盖文档类做出的决定。
正如我所说的,这个答案是对以前明显不令人满意的版本的广泛修改,它或多或少对应于现在涵盖该类的caption
包的使用的部分article
。
标题周围的间距:文档类的作用
作为大卫·卡莱尔评论道问题发布几分钟后,其中描述的行为与标准类(article
、report
、books
)的行为不一致;不久之后,Barbara Beeton 补充道当表格标题被amsart
放置amsbook
在amsproc
以下表格本身,因为它们假设只有在figure
s 中标题才会出现在底部,而在table
s(以及所有其他类型的浮动对象)中,标题都放在顶部。(例如,参见 ,amsart.cls
第 1401-1408 行。)其他文档类也做出了类似的假设,因此,如果您坚持在底部为表格添加标题,就像标准类期望您做的那样,在使用这些类时,您将遇到类似的问题。llncs
班级就是其中之一(参见llncs.cls
,第 789–799 页和第 811–820 页)。我们选择仅展示amsart
和 的行为article
。
amsart
该类的“原生”行为
以下来源
\documentclass[a4paper]{amsart}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
\usepackage{booktabs}
\usepackage{mwe}
\begin{document}
In figure~\ref{fig:sample} the caption is below
and in table~\ref{tab:sample} it is above:
with the \textsf{amsart} class, they both look right.
\begin{figure}[tbp]
\centering
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\end{figure}
\begin{table}[tbp]
\centering
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\end{table}
Table~\ref{tab:inverted} has the caption below:
with the \textsf{amsart} class, the spacing is wrong.
\begin{table}[tbp]
\centering
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
输出结果如下:
当标题位于表格下方时,间距是错误的。这和楼主的情况类似。
article
该类的“原生”行为
源代码:
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
\usepackage{booktabs}
\usepackage{mwe}
\begin{document}
In figure~\ref{fig:sample} the caption is below
and in table~\ref{tab:sample} it is above:
with the \textsf{article} class, the former looks right,
but the latter does not.
\begin{figure}[tbp]
\centering
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\end{figure}
\begin{table}[tbp]
\centering
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\end{table}
On the other hand, table~\ref{tab:inverted}, which has the caption below,
looks right with the \textsf{article} class.
\begin{table}[tbp]
\centering
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
输出:
\abovecaptionskip
尽管标准类也允许你通过改变两个样式参数和的值(\belowcaptionskip
即标题上方和下方的垂直空间)进行自定义,但它们做出了一个不幸的假设:全部各种类型的标题相对于它们引用的对象放置在相同的位置(全部在上方,或全部在下方)。
补救措施
我们已经暗示了有几种可能的补救措施,但我们将根据方案坚持使用一些简单的补救措施caption
。但首先,让我们看看为什么我认为现有的答案不令人满意。
其他答案的缺点
这接受的答案(截至撰写本文时,已获得 44 票!)一般而言是错误的,您可以轻松验证,例如在类的情况下amsart
,如果您将其应用于上面给出的示例源代码:
\documentclass[a4paper]{amsart}
%\documentclass[a4paper]{llncs}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
\usepackage{caption}
\usepackage{booktabs}
\usepackage{mwe}
\captionsetup[table]{skip=10pt plus 0.01pt}
%\captionsetup{tableposition=above}
\makeatletter
%\renewenvironment{table}
% {\@float{table}}
% {\end@float}
%\renewenvironment{table*}
% {\@dblfloat{table}}
% {\end@dblfloat}
\newcommand*\DrawHoriLine{%
\par
\dimen@ \prevdepth
\kern -.1\p@
\hrule \@height .1\p@ \@depth .1\p@
\kern -.1\p@
\prevdepth \z@
\null
\vskip -\baselineskip
\prevdepth \dimen@
}
\newcommand*\PrintSkips[1]{%
\typeout{In #1:}%
\typeout{\@spaces above: \the\abovecaptionskip}%
\typeout{\@spaces below: \the\belowcaptionskip}%
}
\makeatother
\begin{document}
In figure~\ref{fig:sample} the caption is below
and in table~\ref{tab:sample} it is above:
with the \textsf{amsart} class, they both look right.
\begin{figure}[tbp]
\DrawHoriLine
\centering
\PrintSkips{figure}
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\DrawHoriLine
\end{figure}
\begin{table}[tbp]
\DrawHoriLine
\centering
\PrintSkips{table}
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\DrawHoriLine
\end{table}
Table~\ref{tab:inverted} has the caption below:
with the \textsf{amsart} class, even if we apply @cmhughes' answer,
the spacing is wrong.
\begin{table}[tbp]
\DrawHoriLine
\centering
\PrintSkips{inverted table}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\DrawHoriLine
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
此代码发出一些诊断消息,帮助您验证我们将要提出的声明;出于同样的原因,它还绘制了标记浮动确切边界的细水平线,以使标题和浮动边缘之间不需要的垂直空间清晰可见。请注意,我们通过为其添加一些可拉伸性,使解决方案引入的垂直空间在诊断消息中清晰可辨。此外,您可以自己尝试不同的替代方案(例如llncs
通过适当取消注释某些代码行(同时注释掉其他代码行),测试该解决方案对类的有效性。
很明显,可接受的答案不适用于该amsart
类别;尽管如此,它可能会给人一种在该类别的情况下足够的印象llncs
,但事实并非如此;事实上:
它会在标题和浮动边缘之间引入虚假空间;
尽管
caption
软件包在编译过程中发出了警告,但标题的外观是已更改并且与文档类指定的不同。
请注意,第二点不适用于该类amsart
(因为它是包所知道的类之一)。llncs
但是,对于该类,标题的外观不保留这一事实可能是一个问题,因此,人们可能更愿意完全避免使用该包。实际上,在这种情况下,如果坚持“从下方”为表添加标题,最简单的做法可能是恢复类中给出的两个环境和caption
的原始定义(基于该类)。这可以通过取消注释以下行来实现table
table*
article
llncs
%\renewenvironment{table}
% {\@float{table}}
% {\end@float}
%\renewenvironment{table*}
% {\@dblfloat{table}}
% {\end@dblfloat}
在上面显示的代码中。由于我(再次)为 30000 个字符的限制而苦苦挣扎,我将让您自己检查我刚才所说的是否正确,以及尝试其他可能的替代方案,方法是适当地注释/取消注释该代码的各部分。毕竟,该类llncs
不是标准发行版的一部分,我希望这个答案专注于与标准类一起使用的解决方案。但是,让我提请大家注意已接受答案的另一个缺点:它将标题和表格之间的跳过设置为忽略文档类所作设置的值。例如,该类amsart
使用 12pt,而不是 10pt。
我发现@isk27 的回答和@agold 的有类似的缺陷:除了建议对每个浮动单独应用补丁外,它们还忽略了类预设的样式参数。center
特别是环境,会产生太多空间。
使用caption
包
实际上,只需加载该caption
包即可解决问题:请参阅其手册第 2.6 节,从第 13 页开始。请注意,命令
\captionsetup[table]{skip=10pt}
指定标题和标题对象之间的跳跃值,仅当您想要更改文档类预设的值时才应使用它,正如已经指出的那样,这并不总是一个好主意。应该说,
\captionsetup[table]{position=above}
但在简单情况下不需要这样做,因为默认设置position=auto
通常能够自动猜测标题出现的位置(浮动对象的上方或下方)。如果失败,除了上面显示的声明之外,您还可以在加载包时使用以下快捷方式,它也有效:
\usepackage[tableposition=above]{caption}
caption
有关该包如何与某些已知文档类交互的信息,请参阅包手册的第 5 节。完整的可编译示例说明了它与article
和amsart
类的行为,还展示了如何覆盖特定表的常规设置,请参阅下一节。
表格用于调整标题周围垂直空间的策略caption
有时会交换样式参数的含义\abovecaptionskip
;\belowcaptionskip
下一节中的所有示例都会打印这些参数的值,以帮助您了解包正在做什么。
article
文档类
回想一下,使用 时article
,如果你想要为表格添加标题“从多于”。
article
+ caption
,默认设置
在这种情况下,通常情况下,caption
包足够智能,可以在所有情况下插入正确的空格,即使您包含一个与其他表格标题不同的特殊表格也是如此;但是,这并不能保证在所有情况下都有效。
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
%
% The following should generally suffice...
\usepackage{caption}
%
% ... but if it doesn't, use, for example, the "tableposition" option:
% \usepackage[tableposition=above]{caption}
%
% Other packages follow:
\usepackage{booktabs}
\usepackage{mwe}
\makeatletter
\newcommand*\PrintSkips[1]{%
\typeout{In #1:}%
\typeout{\@spaces above: \the\abovecaptionskip}%
\typeout{\@spaces below: \the\belowcaptionskip}%
}
\makeatother
\begin{document}
Now figure~\ref{fig:sample} (with the caption below)
and table~\ref{tab:sample} (with the caption above)
both look correct. Remember that we are using the
\textsf{article} class.
\begin{figure}[tbp]
\centering
\PrintSkips{figure}
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\end{figure}
\begin{table}[tbp]
\centering
\PrintSkips{table}
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\end{table}
An exceptional table that, like table~\ref{tab:inverted},
has the caption below, is still handled correctly, if we
we do \emph{not} specify to the \textsf{caption} package
any positioning option for captions (the default
\texttt{position=auto} remains in force).
However, this is not guaranteed to work in all situations.
\begin{table}[tbp]
\centering
\PrintSkips{inverted table}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
输出:
article
+ caption
+tableposition=above
但是,如果你明确指定tableposition=above
,则会出现一个带有以下标题的例外表格不是得到正确处理。
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
%
% The following should generally suffice...
% \usepackage{caption}
%
% ... but if it doesn't, use, for example, the "tableposition" option:
\usepackage[tableposition=above]{caption}
%
% Other packages follow:
\usepackage{booktabs}
\usepackage{mwe}
\makeatletter
\newcommand*\PrintSkips[1]{%
\typeout{In #1:}%
\typeout{\@spaces above: \the\abovecaptionskip}%
\typeout{\@spaces below: \the\belowcaptionskip}%
}
\makeatother
\begin{document}
Now figure~\ref{fig:sample} (with the caption below)
and table~\ref{tab:sample} (with the caption above)
both look correct. Remember that we are always using
the \textsf{article} class.
\begin{figure}[tbp]
\centering
\PrintSkips{figure}
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\end{figure}
\begin{table}[tbp]
\centering
\PrintSkips{table}
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\end{table}
If the \textsf{caption} package is explicitly loaded
specifyig \texttt{tableposition=above}, though,
an exceptional table that, like table~\ref{tab:inverted},
has the caption below, will \emph{not} be handled correctly.
\begin{table}[tbp]
\centering
\PrintSkips{inverted table}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
输出:
\captionsetup
如此异常的表,仍然可以通过在浮动环境主体中发出命令的方式来应对。
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
%
% The following should generally suffice...
% \usepackage{caption}
%
% ... but if it doesn't, use, for example, the "tableposition" option:
\usepackage[tableposition=above]{caption}
%
% Other packages follow:
\usepackage{booktabs}
\usepackage{mwe}
\makeatletter
\newcommand*\PrintSkips[1]{%
\typeout{In #1:}%
\typeout{\@spaces above: \the\abovecaptionskip}%
\typeout{\@spaces below: \the\belowcaptionskip}%
}
\makeatother
\begin{document}
Now figure~\ref{fig:sample} (with the caption below)
and table~\ref{tab:sample} (with the caption above)
both look correct. We are always using the \textsf{article} class.
\begin{figure}[tbp]
\centering
\PrintSkips{figure}
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\end{figure}
\begin{table}[tbp]
\centering
\PrintSkips{table}
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\end{table}
Even when \texttt{tableposition=above} has been specified,
an exceptional table that, like table~\ref{tab:inverted},
has the caption below, can still be dealt with by means
of a \verb|\captionsetup| command issued in the body of
the floating environment. This is guaranteed to work.
\begin{table}[tbp]
\centering
\captionsetup{position=below}
\PrintSkips{inverted table}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
输出:
amsart
文档类
回想一下,使用 时amsart
,如果你想要为表格添加标题“从以下”。
amsart
+ caption
,默认设置
对于amsart
,默认设置意味着除figure
s 之外的所有浮点数(特别是所有table
s)都应在上方标注标题。不幸的是,对于amsart
包caption
,无法正确处理以相反方式标注标题的异常表格。
\documentclass[a4paper]{amsart}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
%
% The following should generally suffice...
\usepackage{caption}
%
% ... but if it doesn't, use, for example, the "tableposition" option:
% \usepackage[tableposition=above]{caption}
%
% Other packages follow:
\usepackage{booktabs}
\usepackage{mwe}
\makeatletter
\newcommand*\PrintSkips[1]{%
\typeout{In #1:}%
\typeout{\@spaces above: \the\abovecaptionskip}%
\typeout{\@spaces below: \the\belowcaptionskip}%
}
\makeatother
\begin{document}
Remember? With the \textsf{amsart} class,
figure~\ref{fig:sample} (with the caption below)
and table~\ref{tab:sample} (with the caption above)
both looked correct; the same happens if we load the
\textsf{caption} package, as we are doing now.
So, loading the package seems useless in this case,
but it does not harm.
\begin{figure}[tbp]
\centering
\PrintSkips{figure}
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\end{figure}
\begin{table}[tbp]
\centering
\PrintSkips{table}
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\end{table}
Unfortunately, with the \textsf{amsart} class,
an exceptional table that, like table~\ref{tab:inverted},
has the caption below, will \emph{not} be handled correctly,
if we load the \textsf{caption} package without specifying
any positioning option for captions (thereby leaving
the default \texttt{position=auto} in force).
\begin{table}[tbp]
\centering
\PrintSkips{inverted table}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
输出:
amsart
+ caption
+tableposition=above
\captionsetup
这与上一小节的情况相同。当然,您仍然可以通过在浮动环境的主体中发出命令来处理带有下方标题的异常表格。
\documentclass[a4paper]{amsart}
\usepackage[T1]{fontenc} % I’m accustomed to always loading it...
%
% The following should generally suffice...
% \usepackage{caption}
%
% ... but if it doesn't, use, for example, the "tableposition" option:
\usepackage[tableposition=above]{caption}
%
% Other packages follow:
\usepackage{booktabs}
\usepackage{mwe}
\makeatletter
\newcommand*\PrintSkips[1]{%
\typeout{In #1:}%
\typeout{\@spaces above: \the\abovecaptionskip}%
\typeout{\@spaces below: \the\belowcaptionskip}%
}
\makeatother
\begin{document}
Remember? With the \textsf{amsart} class,
figure~\ref{fig:sample} (with the caption below)
and table~\ref{tab:sample} (with the caption above)
both looked correct; the same happens if we load the
\textsf{caption} package, as we are doing now.
So, loading the package seems useless in this case,
but it does not harm.
\begin{figure}[tbp]
\centering
\PrintSkips{figure}
\includegraphics[scale=0.5]{image}
\caption{A sample image}
\label{fig:sample}
\end{figure}
\begin{table}[tbp]
\centering
\PrintSkips{table}
\caption{A sample table}
\label{tab:sample}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\end{table}
However, the \textsf{caption} package is still useful in that
an exceptional table that, like table~\ref{tab:inverted},
has the caption below, can correctly be dealt with,
even when the package is loaded with \texttt{tableposition=above},
by means of a \verb|\captionsetup| command issued in the body
of the floating environment.
\begin{table}[tbp]
\centering
\captionsetup{position=below}
\PrintSkips{inverted table}
\begin{tabular}{lcc}
\toprule
Titles & Data 1 & Data 2 \\
\midrule
This is the first row & 12 & 34 \\
This is the second row & 56 & 78 \\
This is---guess what?---the third row & 90 & ?? \\
\bottomrule
\end{tabular}
\caption{A table with a caption below}
\label{tab:inverted}
\end{table}
Random text follows, just to fill the page up.
\lipsum[1-2]
\end{document}
输出:
请注意,如果包是用默认设置加载的,也应该应用相同的解决方法caption
(尝试一下)。
答案3
在标题前尝试一下这个:
\bigskip
答案4
对我来说,最简单的解决方案是\vspace{...}
在标题前添加。它看起来是这样的:
\begin{table}[H]
\centering
\begin{tabular}{|l|l|l|r|r|}
\hline Monopole & No & \#1 & 5 & 482 106 m \\
\hline Yagi-Uda & Yes & \#1 & 4 & 86 903 m \\
\hline Yagi-Uda & Yes & \#2 & 4 & 108 642 m \\
\hline
\end{tabular}
\vspace{0.1cm}
\caption{Position accuracy of experiment 3}
\label{tab:exp3results}
\end{table}
谢谢isk27 的回答为我指明了正确的方向!我尝试在其他地方添加 vspace,但直到我看到这个建议才成功。