2

I have a table with a horizontal title above the multi-columns. In some cases, the horizontal title is wider than the multi-columns. This result in non-equally distributed columns that does not look nice. I need to fix this issue by equally distribute the columns among the space. This is a sample script with afigure.

\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}

enter image description here

EDIT 2: I tried the solution provided by @Bernard. But, there is another problem. When I need to add long text in the first row, the whole table get distorted. Can you clarify how to avoid such problem. My text length varies. I have quite long text in the most left cell in each row. This is an example of script you can run to see the issue.

\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

You can make the natural width of the columns wider than the span. I also got rid of all the empty columns as they seemed confusing.

enter image description here

\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}
1

A solution with tabularx and makecell. I removed the vertical lines and the empty column. It the column head is two widen write it in a \thead command, which allow breaks.

\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} 

enter image description here

A variant, if cells in the first column contain longer text: the first column is a X type column, twice as wide as the 10 otherXcolumns in the cell. Also, I loaded thegeometry package, for more sensible horizontal margins if you don't have marginal notes:

\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} 

enter image description here

4
  • Can't you maintain the vertical lines as I originally posted? I tried, things got messy. – user6875880 Apr 17 '17 at 13:55
  • I can, but it makes the table preamble a little more complex. Please see my updated answer. – Bernard Apr 17 '17 at 14:16
  • Thanks a lot. But I added an update. Still can not get what must be. – user6875880 Apr 17 '17 at 21:23
  • @user6875880: I've added an answer to take your update into account. – Bernard Apr 17 '17 at 21:47
0

Slightly modified Bernard answer:

\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} 

Main differences:

  • for rotated headers is used \rothead from package makecell
  • defined new command \mcn{...}{...} as shortcut for \multicolumn{...}{c}{...}
  • deleted all surplus curly braces in empty cells

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.