如何使表格标题适合表格的宽度

如何使表格标题适合表格的宽度

我有以下表格,其标题比表格材料的宽度更宽。如何让标题适合表格的宽度?

\documentclass{article}
\usepackage{caption,booktabs,array}
\usepackage[flushleft]{threeparttable}
\newcommand{\rowgroup}[1]{\hspace{-1em}#1}
\usepackage{geometry}
\geometry{
    a4paper,
    total={170mm,257mm},
    left=20mm,
    top=20mm,
}

\usepackage[width=.75\textwidth]{caption}
\begin{document}
\pagenumbering{gobble}
\begin{table}
\caption{Quite a long title for the table that tends to be wider than the table}
\centering
\begin{threeparttable}
\begin{tabular}{>{\quad}ll}
% \toprule
%& \multicolumn{1}{c}{} \\
\midrule
\rowgroup{Tests} \\
test1 &0.2\\
test2&0.2\\
test3&3.03\\
test3&0.006\\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}

X

答案1

您没有threeparttable正确使用环境:您应该将\caption语句放在环境内部而不是外部threeparttable。这样,标题的宽度就会受到限制自动地与环境的关系tabular

更一般地,环境的三个正式部分threeparttable\caption指令、tabular环境(也可能是:tabular*tabularx)和tablenotes环境。通过将标题和表格注释材料放在环境内部而不是外部threeparttable,它们的宽度将自动调整,以不超过表格材料的宽度。

在此处输入图片描述

\documentclass{article}
\usepackage{caption,booktabs,array,ragged2e}
\usepackage[flushleft]{threeparttable}
\newcommand{\rowgroup}[1]{\hspace{-1em}#1}
\usepackage{geometry}
\geometry{a4paper,total={170mm,257mm},left=20mm,top=20mm}

\usepackage[width=.75\textwidth]{caption}
\begin{document}
\begin{table}
%\captionsetup{justification=RaggedRight} % optional
\centering
\begin{threeparttable} % note that this comes before "\caption"
\caption{Quite a long title that's no longer wider than the subsequent tabular environment}
\begin{tabular}{>{\quad}ll}
\midrule
\rowgroup{Tests} \\
test1 &0.2\\
test2&0.2\\
test3&3.03\\
test3&0.006\\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}

答案2

一个简单的解决方案就是添加minipage到您的标签中,例如,

\documentclass{article}
\usepackage{caption,booktabs,array}
\usepackage[flushleft]{threeparttable}
\newcommand{\rowgroup}[1]{\hspace{-1em}#1}
\usepackage{geometry}
\geometry{
    a4paper,
    total={170mm,257mm},
    left=20mm,
    top=20mm,
}


\begin{document}
\pagenumbering{gobble}
\begin{table}
\begin{minipage}{80pt}
    \caption{Quite a long title for the table that tends to be wider than the table}
\centering
\begin{threeparttable}
\begin{tabular}{>{\quad}ll}
% \toprule
%& \multicolumn{1}{c}{} \\
\midrule
\rowgroup{Tests} \\
test1 &0.2\\
test2&0.2\\
test3&3.03\\
test3&0.006\\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{minipage}
\end{table}
\end{document}

也许一些专家会为此提供自动适应选项......

答案3

您可以\\在标题中添加反斜杠:

\documentclass{article}
    \usepackage{caption,booktabs,array}
    \usepackage[flushleft]{threeparttable}

\newcommand{\rowgroup}[1]{\hspace{-1em}#1}

\usepackage{geometry}

\geometry{
        a4paper,
        total={170mm,257mm},
        left=20mm,
        top=20mm,
    }

\usepackage[width=.75\textwidth]{caption}

\begin{document}

\pagenumbering{gobble}

\begin{table}
        \caption{Quite a long title \\ for the table that tends \\ to be wider than
 the table}
    \centering
    \begin{threeparttable}
    \begin{tabular}{>{\quad}ll}
    % \toprule
    %& \multicolumn{1}{c}{} \\
    \midrule
    \rowgroup{Tests} \\
    test1 &0.2\\
    test2&0.2\\
    test3&3.03\\
    test3&0.006\\
    \bottomrule
    \end{tabular}
    \end{threeparttable}
    \end{table}

\end{document}

您可以通过控制反斜杠的位置来控制标题的宽度

相关内容