如何在多列之間均勻分配空間

如何在多列之間均勻分配空間

我有一個表格,在多列上方有一個水平標題。在某些情況下,水平標題比多列更寬。這會導致列分佈不均勻,看起來不太好。我需要透過在空間中均勻分佈列來解決這個問題。這是一個帶有圖形的範例腳本。

\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 otherXcolumns 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}{...}
  • 刪除空白儲存格中所有多餘的大括號

在此輸入影像描述

相關內容