4

I have the following MWE, but I have a problem with the alignment of the last column (under the multicolumn)

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{calc}
\usepackage{array}
\usepackage{color}
%
\begin{document}
%
% table 1 
% evenly alined
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component sizes}\\
%\cmidrule(r){3-5} 
\cmidrule{3-5} 
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Information on component level degrees of freedom}
\label{tab:information-overview-academic-model}
\end{table}
% 
% table 2 
% not evenly alined
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component matrix sizes \textcolor{red}{test text added}}\\
%\cmidrule(r){3-5} 
\cmidrule{3-5} 
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Information on component level degrees of freedom}
\label{tab:information-overview-academic-model}
\end{table}
%
\end{document}

The result I am getting is that the columns under the multicolumns are not evenly separated. And when I increase the width of the text argument to multicolumn command it becomes even worse, first example seems to be fine but with the second there is definitely a problem. Here is the output of the above code:

alignment problem

Any ideas are appreciated to solve this issue. Many thanks in advance.

3

3 Answers 3

7

I suggest two kinds of solutions.

The first one with \makecell which puts the multicolumn header on many lines; the second one with tabularx which equally distributes the spaces among the last 3 columns, leaving the header in one line.

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{calc}
\usepackage{array}
\usepackage{color}
\usepackage{makecell}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
%
% table 1 
% evenly alined
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component sizes}\\
%\cmidrule(r){3-5} 
\cmidrule{3-5} 
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Information on component level degrees of freedom}
\label{tab:information-overview-academic-model}
\end{table}
% 
\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{\makecell{Reduced component\\ matrix sizes\\ \textcolor{red}{test text added}}}\\
%\cmidrule(r){3-5} 
\cmidrule{3-5} 
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{With \textbackslash\texttt{makecell}}
\label{tab:information-overview-academic-modelbis}
\end{table}
\begin{table}[htbp]
\centering
\begin{tabularx}{\linewidth}{ccCCC}
\toprule
& & \multicolumn{3}{c}{Reduced component matrix sizes \textcolor{red}{test text added}}\\
%\cmidrule(r){3-5} 
\cmidrule{3-5} 
% \multicolumn{2}{c}{Internal mode count, $m$} &10&20&30&10&20&30\\
%\midrule
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabularx}
\caption{With \texttt{tabularx}}
\label{tab:information-overview-academic-modelter}
\end{table}
\end{document}

enter image description here

2
  • Thanks, without reading the documentation for makecell, I tried to use makecell with one line without the linebreaks as you suggested and then the problem still persists. I guess it is better to use tabularx for this one.
    – Umut Tabak
    Apr 21, 2019 at 9:40
  • 1
    @UmutTabak \makecell is created to divide lines. If you don't add \\ within a \makecell it makes no sense to use it.
    – CarLaTeX
    Apr 21, 2019 at 9:46
3

Just for the sake of variety, here's a solution which (a) calculates the natural widths of columns 3, 4, and 5 and (b) uses the combined widths of the three columns (plus the intercolumn whitespace amount) to typeset the long header string in a way that allows for automatic line-breaking.

This approach keeps the tabular material nice and compact; this is generally considered to be a good thing.

enter image description here

Observe that I would place the headers "Component" and "Full size" at the top rather than at the bottom of the header material.

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor,booktabs,array,ragged2e}
%% Create a new column type:
\newcolumntype{C}[1]{>{\Centering\arraybackslash}p{#1}}

\begin{document}

\begin{table}[htbp]
\centering
\begin{tabular}{ccccc}
\toprule
& & \multicolumn{3}{c}{Reduced component sizes}\\
\cmidrule{3-5} 
Component & Full size & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{OP's original layout}
\label{tab:a}
\end{table}


%% perform a few length calculations 
\newlength\mylen
\newlength\mylena
\newlength\mylenb
\settowidth\mylena{$m=4$}
\settowidth\mylenb{$m=12$}
\setlength{\mylen}{\dimexpr2\mylena+\mylenb+4\tabcolsep\relax}

\begin{table}[htbp]
\centering
\begin{tabular}{@{}ccccc@{}}
\toprule
Component & Full size & 
\multicolumn{3}{C{\mylen}@{}}{Reduced component matrix sizes \textcolor{red}{test text added}}\\
\cmidrule(l){3-5} 
& & $m=4$ & $m=8$ & $m=12$\\
\midrule
1 & 257 & 24 & 28 & 32 \\
2 & 420 & 47 & 51 & 55 \\
3 & 177 & 24 & 28 & 32 \\
\bottomrule
\end{tabular}
\caption{Automatic line-breaking in cols 3 to 5}
\label{tab:b}
\end{table}

\end{document}
0

Instead of merging cells, you can try to split cells. In the following code, I have written a command \SplitCell which splits a cell in three sub-cells (by using \hfill).

\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{booktabs}

\begin{document}

\NewDocumentCommand{\SplitCell}{mmm}{\hfill #1\hfill #2\hfill #3\hfill}

\begin{table}[htbp]
\centering
\begin{tabular}{ccc}
\toprule
& & Reduced component matrix sizes \textcolor{red}{test text added} \\
\cmidrule{3-3} 
Component & Full size & \SplitCell{$m=4$}{$m=8$}{$m=12$} \\
\midrule
1 & 257 & \SplitCell{24}{28}{32} \\
2 & 420 & \SplitCell{47}{51}{55} \\
3 & 177 & \SplitCell{24}{28}{32} \\
\bottomrule
\end{tabular}
\caption{Information on component level degrees of freedom}
\label{tab:information-overview-academic-model}
\end{table}
%
\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .