中心长列标题

中心长列标题

MWE我的桌子:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\begin{document}

\begin{table}
\hspace{-3cm}
\footnotesize{
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
 & \multicolumn{3}{p{3cm}}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{3cm}}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{3cm}|}{\centering Very long column title that needs to be centered 3} \\
\hline
 \multirow{2}{*}{Cooooooool1} & \multirow{2}{*}{Cooooool2} & ``Col &     \multirow{2}{*}{Cooooool1} &
\multirow{2}{*}{Cooooool2} & ``Coooooool & \multirow{2}{*}{Coooooool1} &     \multirow{2}{*}{Coooooool2} & ``Coooooool \\
 & & & 3'' & & & 3'' & & & 3'' \\
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\hline
\end{tabular}
}
\end{table}

\end{document}

这给了我:

桌子

您会注意到,列标题居中,但不沿着它们占据的三列。我怎样才能让它们居中?

答案1

TeX 在这方面确实不太擅长。如果你准备手动拆分标题,这是最简单的方法:

在此处输入图片描述

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\begin{document}

\begin{table}
\hspace{-3cm}
\footnotesize{
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
 & \multicolumn{3}{c|}{Very long column title}&
 \multicolumn{3}{c|}{Very long column title}&
 \multicolumn{3}{c|}{Very long column title}\\
 & \multicolumn{3}{c|}{that needs to be}&
 \multicolumn{3}{c|}{that needs to be}&
 \multicolumn{3}{c|}{that needs to be}\\
 & \multicolumn{3}{c|}{centered}&
 \multicolumn{3}{c|}{centered 2}&
 \multicolumn{3}{c|}{centered 3}\\
\hline
 \multirow{2}{*}{Cooooooool1} & \multirow{2}{*}{Cooooool2} & ``Col &     \multirow{2}{*}{Cooooool1} &
\multirow{2}{*}{Cooooool2} & ``Coooooool & \multirow{2}{*}{Coooooool1} &     \multirow{2}{*}{Coooooool2} & ``Coooooool& \\
 & & & 3'' & & & 3'' & & & 3'' \\
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\hline
\end{tabular}
}
\end{table}

\end{document}

答案2

1.自动确定列宽:

这是一个版本不是不需要多次传递,也不需要猜测宽度,但需要您知道每个子列的最宽元素是多少。使用子列的最宽元素,您可以计算出要用于列类型的正确宽度p{}。这会产生:

在此处输入图片描述

或者我建议使用包裹booktabs

在此处输入图片描述

笔记:

  • 通过自动确定宽度,您的标题可以容纳两行。
  • 尾部&缺少一行,已添加。
  • 其中一行似乎有重叠的数据,因此\multirow也对其进行了调整。
  • 即使\footnotesize您的桌子对于正常使用来说仍然太宽,所以除非您将它设置为横向放置以获得更宽的桌子,否则\paperwidth您应该重新考虑您对桌子的制作方式。

代码:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\usepackage{booktabs}

\newlength{\ColumnWidthBCD}
\newlength{\ColumnWidthEFG}
\newlength{\ColumnWidthHIJ}

% These should contain the widet elements of each column
\newcommand{\ColumnATitle}{Cooooooool1}
\newcommand{\ColumnBTitle}{Cooooool2}
\newcommand{\ColumnCTitle}{``Col}
\newcommand{\ColumnDTitle}{Cooooool1}
\newcommand{\ColumnETitle}{Cooooool2}
\newcommand{\ColumnFTitle}{``Coooooool}
\newcommand{\ColumnGTitle}{Coooooool1}
\newcommand{\ColumnHTitle}{Coooooool2}
\newcommand{\ColumnITitle}{``Coooooool}
\newcommand{\ColumnJTitle}{3''}

\begin{document}
\hspace{-3cm}
\footnotesize{
\settowidth{\ColumnWidthBCD}{\ColumnBTitle\ColumnCTitle\ColumnDTitle}%
\settowidth{\ColumnWidthEFG}{\ColumnETitle\ColumnFTitle\ColumnGTitle}%
\settowidth{\ColumnWidthHIJ}{\ColumnHTitle\ColumnITitle\ColumnJTitle}%
% Now adjust for the intercolumn spacing
\addtolength{\ColumnWidthBCD}{6\tabcolsep}
\addtolength{\ColumnWidthEFG}{6\tabcolsep}
\addtolength{\ColumnWidthHIJ}{6\tabcolsep}
\noindent
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
 & \multicolumn{3}{p{\ColumnWidthBCD} |}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{\ColumnWidthEFG} |}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{\ColumnWidthHIJ}|}{\centering Very long column title that needs to be centered 3} \\
\hline
 \multirow{2}{*}{\ColumnATitle} & \multirow{2}{*}{\ColumnBTitle} & \ColumnCTitle &     \multirow{2}{*}{\ColumnDTitle} &
\multirow{2}{*}{\ColumnETitle} & \ColumnFTitle & \multirow{2}{*}{\ColumnGTitle} &     \multirow{2}{*}{\ColumnHTitle} & \ColumnITitle & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
  & & &     & & &     & & & 3'' \\% <--- changed this line as well, seemed wrong.
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\hline
\end{tabular}
}


\bigskip
\hspace{-3cm}
\footnotesize{
% This would not need to be recomputed here as we could use the above
% determined values. But to keep this second example useable standalong
% without the above example decided to leave these here as well
\settowidth{\ColumnWidthBCD}{\ColumnBTitle\ColumnCTitle\ColumnDTitle}%
\settowidth{\ColumnWidthEFG}{\ColumnETitle\ColumnFTitle\ColumnGTitle}%
\settowidth{\ColumnWidthHIJ}{\ColumnHTitle\ColumnITitle\ColumnJTitle}%
% Now adjust for the intercolumn spacing
\addtolength{\ColumnWidthBCD}{6\tabcolsep}
\addtolength{\ColumnWidthEFG}{6\tabcolsep}
\addtolength{\ColumnWidthHIJ}{6\tabcolsep}
\noindent
\begin{tabular}{cccccccccc} \toprule
 & \multicolumn{3}{p{\ColumnWidthBCD}}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{\ColumnWidthEFG}}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{\ColumnWidthHIJ}}{\centering Very long column title that needs to be centered 3} \\
\cmidrule(l{2pt}r{4pt}){1-1}
\cmidrule(l{4pt}r{4pt}){2-4}
\cmidrule(l{4pt}r{4pt}){5-7}
\cmidrule(l{4pt}r{2pt}){8-10}
 \multirow{2}{*}{\ColumnATitle} & \multirow{2}{*}{\ColumnBTitle} & \ColumnCTitle &     \multirow{2}{*}{\ColumnDTitle} &
\multirow{2}{*}{\ColumnETitle} & \ColumnFTitle & \multirow{2}{*}{\ColumnGTitle} &     \multirow{2}{*}{\ColumnHTitle} & \ColumnITitle & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
  & & &     & & &     & & & 3'' \\% <--- changed this line as well, seemed wrong.
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\bottomrule
\end{tabular}
}
\end{document}

2. 手动设置列宽

上面的版本可能看起来使用起来比较复杂,因为它会自动确定列宽,如果经常排版这种表格,这个功能就很有用。偶尔使用时,您可以手动调整列宽,直到获得所需的结果。

下面的 MWE 产生相同的结果,但需要您对列的宽度进行初步猜测,然后根据需要进行调整:

代码:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\hspace{-3cm}
\footnotesize{
\noindent
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
 & \multicolumn{3}{p{4.63cm}|}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{5.73cm}|}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{4.65cm}|}{\centering Very long column title that needs to be centered 3} \\
\hline
 \multirow{2}{*}{Cooooooool1} & \multirow{2}{*}{Cooooool2} & ``Col &     \multirow{2}{*}{Cooooool1} &
\multirow{2}{*}{Cooooool2} & ``Coooooool & \multirow{2}{*}{Coooooool1} &     \multirow{2}{*}{Coooooool2} & ``Coooooool & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
  & & &     & & &     & & & 3'' \\% <--- changed this line as well, seemed wrong.
\hline
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\hline
\end{tabular}
}


\bigskip
\hspace{-3cm}
\footnotesize{
\noindent
\begin{tabular}{cccccccccc} \toprule
 & \multicolumn{3}{p{4.63cm}}{\centering Very long column title that needs to be centered} &
\multicolumn{3}{p{5.73cm}}{\centering Very long column title that needs to be centered 2} &
\multicolumn{3}{p{4.65cm}}{\centering Very long column title that needs to be centered 3} \\
\cmidrule(l{2pt}r{4pt}){1-1}
\cmidrule(l{4pt}r{4pt}){2-4}
\cmidrule(l{4pt}r{4pt}){5-7}
\cmidrule(l{4pt}r{2pt}){8-10}
 \multirow{2}{*}{Cooooooool1} & \multirow{2}{*}{Cooooool2} & ``Col &     \multirow{2}{*}{Cooooool1} &
\multirow{2}{*}{Cooooool2} & ``Coooooool & \multirow{2}{*}{Coooooool1} &     \multirow{2}{*}{Coooooool2} & ``Coooooool & \\% <--- trailing "&" was missing.
% & & & 3'' & & & 3'' & & & 3'' \\
  & & &     & & &     & & & 3'' \\% <--- changed this line as well, seemed wrong.
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 \\ 
\bottomrule
\end{tabular}
}
\end{document}

相关内容