我有一个表格,其中多列上方有一个水平标题。在某些情况下,水平标题比多列更宽。这导致列分布不均匀,看起来不太好看。我需要通过在空间中均匀分布列来解决这个问题。这是一个带有图形的示例脚本。
\documentclass[10pt]{article}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\newcommand*\rot{\rotatebox{90}}
\begin{document}
\begin{table}
\caption{MyTable}
\centering
\resizebox{\textwidth}{!} {
\begin{tabular} {@{} lc*{14}l @{}}
\hline
\\
\multicolumn{1}{c}{Column1}
&&\multicolumn{3}{c}{Column2}
&&\multicolumn{3}{c}{ColumnLongTitle}
&&\multicolumn{2}{c}{Column3}
&&\multicolumn{2}{c}{Column4}
\\
\cmidrule(lr){1-1}
\cmidrule(lr){3-5}
\cmidrule(lr){7-9}
\cmidrule(lr){11-12}
\cmidrule(lr){14-15}
\\
& \vline &\rot{Title1} & \rot{Title2} & \rot{Title3}
& \vline & \rot{Title1} & \rot{Title2} & \rot{Title3}
& \vline & \rot{Title1} & \rot{Title2}
& \vline & \rot{Title1} & \rot{Title2}
\\ \hline
Row1 & \vline & {} & \cmark & {} & \vline & \cmark & {} & {} & \vline & & \cmark & \vline & \cmark & {} & \\
\hline
Row2 & \vline & \cmark & {} & {} & \vline & \cmark & {} & {} & \vline & {} & \cmark & \vline & \cmark & {} & \\
\hline
Row3 & \vline & {} & \cmark & {} & \vline & \cmark & {} & {} & \vline & {} & \cmark & \vline & \cmark & {} & \\
\hline
\end{tabular}
}
\end{table}
\end{document}
编辑2:我尝试了@Bernard 提供的解决方案。但是,还有另一个问题。当我需要在第一行添加长文本时,整个表格都会扭曲。您能解释一下如何避免这样的问题吗?我的文本长度各不相同。我在每行最左边的单元格中都有相当长的文本。这是一个您可以运行以查看问题的脚本示例。
\documentclass[10pt]{article}
%\usepackage{showframe}
%\renewcommand\ShowFrameLinethickness{0.3pt}
\usepackage{graphicx}
\usepackage{booktabs, tabularx, caption, makecell}
\setcellgapes{3pt}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\newcommand*\rot{\rotatebox{90}}
\begin{document}
\begin{table}
\caption{MyTable}
\centering\makegapedcells
\begin{tabularx}{\linewidth}{@{} l|*{3}{>{\centering\arraybackslash}X} |*{3}{>{\centering\arraybackslash}X}| *{2}{>{\centering\arraybackslash}X}| *{2}{>{\centering\arraybackslash}X}|@{}}
\hline
\multicolumn{1}{c}{Column1}
&\multicolumn{3}{c}{Column2}
&\multicolumn{3}{c}{ColumnLongTitle}
&\multicolumn{2}{c}{Column3}
&\multicolumn{2}{c}{Column4}
\\
\cmidrule(lr){1-1}
\cmidrule(lr){2-4}
\cmidrule(lr){5-7}
\cmidrule(lr){8-9}
\cmidrule(lr){10-11}
\addlinespace
&\rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2}
& \rot{Title1} & \rot{Title2}
\\\hline
AAAAAAAAAAAAAAAAAAAAAAAA & {} & \cmark & {} & \cmark & {} & {} & & \cmark & \cmark & {} \\
\hline
Row2 & \cmark & {} & {} & \cmark & {} & {} & {} & \cmark & \cmark & {} \\
\hline
Row3 & {} & \cmark & {} & \cmark & {} & {} & {} & \cmark & \cmark & {} \\
\hline
\end{tabularx}
\end{table}
\end{document}
答案1
你可以让列的自然宽度大于跨度。我还删除了所有空列,因为它们看起来很混乱。
\documentclass[10pt]{article}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\newcommand*\rot[1]{\rotatebox{90}{#1 }}
\usepackage{array}
\begin{document}
\begin{table}
\caption{MyTable}
\setlength\extrarowheight{4pt}
\centering
%\resizebox{\textwidth}{!} {% scaling tables is evil but of you do do it,. you need a % here
\begin{tabular*}{\textwidth} {@{\extracolsep{\textwidth minus \textwidth}}
l|ccc|ccc|cc|cc@{}}
\hline
\multicolumn{1}{@{}c}{Column1}
&\multicolumn{3}{c}{Column2}
&\multicolumn{3}{c}{\makebox[0pt]{ColumnLongTitle}}
&\multicolumn{2}{c}{Column3}
&\multicolumn{2}{c@{}}{Column4}
\\
\cmidrule(lr){1-1}
\cmidrule(lr){2-4}
\cmidrule(lr){5-7}
\cmidrule(lr){8-9}
\cmidrule(lr){10-11}
\multicolumn{1}{@{}c}{}
&\rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2}
& \rot{Title1} & \rot{Title2}
\\ \hline
Row1 & & \cmark & & \cmark & & & & \cmark & \cmark & \\
\hline
Row2 & \cmark & & & \cmark & & & & \cmark & \cmark & \\
\hline
Row3 & & \cmark & & \cmark & & & &\cmark & \cmark & \\
\hline
\end{tabular*}
\end{table}
\end{document}
答案2
解决方案是tabularx
和makecell
。我删除了垂直线和空列。如果列头是两行,请将其写在命令中\thead
,这样可以允许换行。
\documentclass[10pt]{article}
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.3pt}
\usepackage{graphicx}
\usepackage{booktabs, tabularx, caption, makecell}
\setcellgapes{3pt}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\newcommand*\rot{\rotatebox{90}}
\begin{document}
\begin{table}
\caption{MyTable}
\centering\makegapedcells
\begin{tabularx}{\linewidth}{@{} l|*{3}{>{\centering\arraybackslash}X} |*{3}{>{\centering\arraybackslash}X}| *{2}{>{\centering\arraybackslash}X}| *{2}{>{\centering\arraybackslash}X}|@{}}
\hline
\multicolumn{1}{c}{Column1}
&\multicolumn{3}{c}{Column2}
&\multicolumn{3}{c}{ColumnLongTitle}
&\multicolumn{2}{c}{Column3}
&\multicolumn{2}{c}{Column4}
\\
\cmidrule(lr){1-1}
\cmidrule(lr){2-4}
\cmidrule(lr){5-7}
\cmidrule(lr){8-9}
\cmidrule(lr){10-11}
\addlinespace
&\rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2}
& \rot{Title1} & \rot{Title2}
\\\hline
Row1 & {} & \cmark & {} & \cmark & {} & {} & & \cmark & \cmark & {} \\
\hline
Row2 & \cmark & {} & {} & \cmark & {} & {} & {} & \cmark & \cmark & {} \\
\hline
Row3 & {} & \cmark & {} & \cmark & {} & {} & {} & \cmark & \cmark & {} \\
\hline
\end{tabularx}
\end{table}
\end{document}
如果第一列中的单元格包含较长的文本,则有一种变体:第一列是X
类型列,其宽度是10 other
Xcolumns in the cell. Also, I loaded the
几何包的两倍,如果没有边注,则可以获得更合理的水平边距:
\documentclass[10pt]{article}
\usepackage[showframe]{geometry}
\usepackage{graphicx}
\usepackage{booktabs, tabularx, caption, makecell}
\setcellgapes{3pt}
\newcolumntype{Y}{ >{\hsize=2\hsize\arraybackslash}X}
\newcolumntype{Z}{ >{\hsize=0.9\hsize\centering\arraybackslash}X}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\newcommand*\rot{\rotatebox{90}}
\begin{document}
\begin{table}
\caption{MyTable}
\centering\makegapedcells
\begin{tabularx}{\linewidth}{@{}Y|*{3}{Z} |*{3}{Z}| *{2}{Z}| *{2}{Z}|@{}}
\hline
\multicolumn{1}{c}{Column1}
&\multicolumn{3}{c}{Column2}
&\multicolumn{3}{c}{ColumnLongTitle}
&\multicolumn{2}{c}{Column3}
&\multicolumn{2}{c}{Column4}
\\
\cmidrule(lr){1-1}
\cmidrule(lr){2-4}
\cmidrule(lr){5-7}
\cmidrule(lr){8-9}
\cmidrule(lr){10-11}
\addlinespace
&\rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2} & \rot{Title3}
& \rot{Title1} & \rot{Title2}
& \rot{Title1} & \rot{Title2}
\\\hline
AA AAA AAA AA AA AAA AAA AAA AAA & {} & \cmark & {} & \cmark & {} & {} & & \cmark & \cmark & {} \\
\hline
Row2 & \cmark & {} & {} & \cmark & {} & {} & {} & \cmark & \cmark & {} \\
\hline
Row3 & {} & \cmark & {} & \cmark & {} & {} & {} & \cmark & \cmark & {} \\
\hline
\end{tabularx}
\end{table}
\end{document}
答案3
略有修改伯纳德回答:
\documentclass[10pt]{article}
\usepackage{geometry}
\usepackage{rotating}
\usepackage{booktabs, makecell, tabularx}
\setcellgapes{3pt}
\renewcommand\theadfont{\normalsize}
\newcolumntype{C}{>{\setlength\hsize{0.7\hsize}\centering \arraybackslash}X}
\newcolumntype{L}{>{\setlength\hsize{3.0\hsize}\raggedright\arraybackslash}X}
\newcommand\mcn[2]{\multicolumn{#1}{c}{#2}}
\usepackage{caption}
\usepackage{pifont}% http://ctan.org/pkg/pifont
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.3pt}
\renewcommand*\ShowFrameColor{\color{red}}
\begin{document}
\begin{table}
\caption{My Table}
\centering
\makegapedcells
\settowidth\rotheadsize{\theadfont Title 3}
\begin{tabularx}{\linewidth}{@{} L | *{3}{C} | *{3}{C} | *{2}{C} | *{2}{C} @{}}
\hline
\mcn{1}{Column1} & \mcn{3}{Column2} & \mcn{3}{ColumnLongTitle}
& \mcn{2}{Column3} & \mcn{2}{Column4} \\
\cmidrule(lr){1-1}\cmidrule(lr){2-4}\cmidrule(lr){5-7}
\cmidrule(lr){8-9}\cmidrule(lr){10-11}
& \rothead{Title1} & \rothead{Title2} & \rothead{Title3}
& \rothead{Title1} & \rothead{Title2} & \rothead{Title3}
& \rothead{Title1} & \rothead{Title2}
& \rothead{Title1} & \rothead{Title2} \\
\hline
AAAAAAAAAAAA AAAAAAAAAAAA
& & \cmark & & \cmark & & & & \cmark & \cmark & \\
\hline
Row 2 & \cmark & & & \cmark & & & & \cmark & \cmark & \\
\hline
Row 3 & & \cmark & & \cmark & & & & \cmark & \cmark & \\
\hline
\end{tabularx}
\end{table}
\end{document}
主要区别:
- 用于旋转标题
\rothead
的包makecell
- 将新命令定义
\mcn{...}{...}
为\multicolumn{...}{c}{...}
- 删除空单元格中所有多余的花括号