我希望标题的宽度与表格的宽度相同,并适当环绕。所以我创建了一个新命令来实现这一点。MWE 如下。问题是,虽然表格和标题在右侧齐平(这是我想要的),但标题在左侧略微突出于表格(这不是我想要的)。我怎样才能让表格和标题在左右两侧齐平?谢谢,Roger
\documentclass{article}
%
\newcommand{\tab}[4]{
\setbox0=\hbox{#1}
\begin{table}[htbp]
\begin{center}
\begin{minipage}{\the\wd0}
\caption[#2]{#3\label{#4}}
\box0
\end{minipage}
\end{center}
\end{table}}
%
\begin{document}
%
\tableofcontents
\listoftables
%
\tab{
\begin{tabular}{|c|c|c|}
\hline
one & two & three\\
four & five & six\\
seven & eight & nine\\
\hline
\end{tabular}}
{Small Table in Minipage}
{A small table in a minipage with a caption that wraps
but is slightly to the left of the table in vertical
alignment.}
{tab:inminipage}
%
\end{document}
答案1
您的代码在错误的位置引入了一个空格,就在表格之前。因此,打印出来时,您的表格会向右缩进一个空格的宽度:
\tab{
应该
\tab{% %% <- remove the space produces by the linebreak
这是您的 MWE:
\documentclass{article}
%
\newcommand{\tab}[4]{
\setbox0=\hbox{#1}
\begin{table}[htbp]
\begin{center}
\begin{minipage}{\the\wd0}
\caption[#2]{#3\label{#4}}
\box0
\end{minipage}
\end{center}
\end{table}}
%
\begin{document}
%
\tableofcontents
\listoftables
%
\tab{% %%% <- this % makes the difference
\begin{tabular}{|c|c|c|}
\hline
one & two & three\\
four & five & six\\
seven & eight & nine\\
\hline
\end{tabular}}
{Small Table in Minipage}
{A small table in a minipage with a caption that wraps
but is slightly to the left of the table in vertical
alignment.}
{tab:inminipage}
%
\end{document}
答案2
虽然这不能直接回答您的问题,但我建议threeparttable
使用已经提供此功能的软件包:
\documentclass{article}
\usepackage{threeparttable}
\begin{document}
\begin{table}
\centering
\begin{threeparttable}
\caption{A quite long caption.}
\begin{tabular}{|c|c|c|}
\hline
one & two & three\\
four & five & six\\
seven & eight & nine\\
\hline
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}