33

The code below produces the following figure:

enter image description here

The issue I am having is that Sample is not vertically aligned, I get proper alignment if \cmidrule(l){2-3} \cmidrule(l){4-5} is removed.

How do I get proper alignment while maintaining the two horizontal lines?

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}

\begin{table}[h]\centering
\begin{tabular}{lcccc}
\toprule 
\multirow{2}{*}{Sample} & \multicolumn{2}{c}{I} & \multicolumn{2}{c}{II} \\
\cmidrule(l){2-3} \cmidrule(l){4-5}
 & A & B & C & D \\
\midrule
S1 & 5 & 8 & 12 & 2 \\
S2 & 6 & 9 & 2 & 6 \\
S3 & 7 & 9 & 5 & 8 \\
S4 & 8 & 9 & 8 & 2 \\
\bottomrule
\end{tabular}
\end{table}

\end{document}
2
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Jan 27, 2014 at 16:18
  • 5
    Off topic: I would use \cmidrule(lr) instead of \cmidrule(l).
    – Manuel
    Jan 27, 2014 at 16:36

4 Answers 4

35

Use the optional argument of \multirow.

Substituting

\multirow{2}{*}{Sample}

with

\multirow{2}[3]{*}{Sample}

does what you want.

MWE:

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}

\begin{table}[h]\centering
\begin{tabular}{lcccc}
\toprule
\multirow{2}[3]{*}{Sample} & \multicolumn{2}{c}{I} & \multicolumn{2}{c}{II} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
 & A & B & C & D \\
\midrule
S1 & 5 & 8 & 12 & 2 \\
S2 & 6 & 9 & 2 & 6 \\
S3 & 7 & 9 & 5 & 8 \\
S4 & 8 & 9 & 8 & 2 \\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

Output:

enter image description here

Note that I've also changed \cmidrule(l) to \cmidrule(lr) for a better looking table, as suggested by Manuel in the comments.

2
  • Why do you give 3 as the optional argument? Jun 3, 2016 at 15:11
  • 1
    @ondra.cifka In that optional argument you have to try and select the best number that fits... Jun 9, 2016 at 12:22
9

I had this same problem, and wasn't satisfied with having to guess at the bigstruts argument to \multirow, so I came up with this:

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}

\begin{table}[h]\centering
  \begin{tabular}{lcccc}
    \toprule
    \multirow{2}{*}[-0.5\dimexpr \aboverulesep + \belowrulesep + \cmidrulewidth]{Sample}
    & \multicolumn{2}{c}{I} & \multicolumn{2}{c}{II} \\
    \cmidrule(l){2-3} \cmidrule(l){4-5}
    & A & B & C & D \\
    \midrule
    S1 & 5 & 8 & 12 & 2 \\
    S2 & 6 & 9 & 2 & 6 \\
    S3 & 7 & 9 & 5 & 8 \\
    S4 & 8 & 9 & 8 & 2 \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document}

output of above code

I think this is correct because multirow is already trying to vertically center two rows' worth of height, but you have to account for the extra space added by \cmidrule, which adds \aboverulesep + \cmidrulewidth + \belowrulesep vertical space. To accomplish that, we tell \multirow to vertically adjust the box downward by half of that \cmidrule space.

If it looks a little off center, I think that's just an illusion because \aboverulesep is smaller than \belowrulesep. I contend that it's "centered" until someone proves me wrong, but you can try -0.5\dimexpr 2\aboverulesep + \cmidrulewidth instead and see if you like the look of that better.

While the above solution just fudges the box down, an alternative is probably to set \bigstrutjot to \dimexpr \aboverulesep + \belowrulesep + \cmidrulewidth and then use \multirow{2}[1]{*}{Sample}. If I understand multirow's documentation, this will actually make the box higher (two rows plus the space added by \cmidrule) rather than just moving the box down. However, in my testing, it yielded the exact same results as the above, and it seems less readable. (Readability being relative, mind you.)

1
  • just to generalize: <number-of-cmidrules / 2>\dimexpr \aboverulesep + \belowrulesep + \cmidrulewidth in case of multiple \cmidrules (+1, btw) Jun 9, 2020 at 10:26
2

An alternative solution with booktabs environment of tabularray package:

\documentclass{article}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{table}[h]\centering
\begin{booktabs}{
  colspec = {lcccc},
  cell{1}{1} = {r=2}{m}, % multirow
  cell{1}{2,4} = {c=2}{c}, % multicolumn
}
\toprule 
  Sample & I &   & II &   \\
\cmidrule[l]{2-3} \cmidrule[l]{4-5}
         & A & B & C  & D \\
\midrule
      S1 & 5 & 8 & 12 & 2 \\
      S2 & 6 & 9 &  2 & 6 \\
      S3 & 7 & 9 &  5 & 8 \\
      S4 & 8 & 9 &  8 & 2 \\
\bottomrule
\end{booktabs}
\end{table}

\end{document}

Note that you need to put trim option l, r or lr inside square brackets.

enter image description here

2
  • I think there is a mistake in the code: the environment {booktabs} should be an environment {tblr}. There is also a problem with the commands \cmidrule. Sep 5, 2021 at 9:38
  • 1
    The booktabs environment was recently added in version 2021N.
    – L.J.R.
    Sep 5, 2021 at 9:44
1

In the environment {NiceTabular} of nicematrix, you can merge cells both horizontally and vertically. Vertically, you specify the number of logical rows and not the number of physical lines as in \multirow. So the contents of the new (merged) cell is center mathematically.

\documentclass{article}

\usepackage{nicematrix,booktabs}

\begin{document}

\begin{table}[h]\centering
\begin{NiceTabular}{lcccc}
\toprule 
\Block{2-1}{Sample} & \Block{1-2}{I} & & \Block{1-2}{II} \\
\cmidrule(l){2-3} \cmidrule(l){4-5}
         & A & B & C  & D \\
\midrule
      S1 & 5 & 8 & 12 & 2 \\
      S2 & 6 & 9 &  2 & 6 \\
      S3 & 7 & 9 &  5 & 8 \\
      S4 & 8 & 9 &  8 & 2 \\
\bottomrule
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

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.