[Tex/LaTex] Vertical centering in multirow table cells

multirowtablesvertical alignment

The documentation for the multirow package implies that the content of a \multirow will be vertically centered by default. In this example (machine-generated from a Word document, hence all the poodlefaking):

  • in the first row, the first cell spans 3 rows, and the other cells are just cells;
  • in the second row, the first cell continues its spanning, and the other cells all span 2 rows;
  • the author has unwittingly added a bogus third row, but it shouldn't matter, as it has to be there to be the recipient of the spans from rows 1 and 2.

output from code below
(Ignore the extra rules: I'm still working on the code to get that right)

The problem is that the text in col 1 is not vertically-centred with respect to the 3 rows that it spans. Cells 2/3/4 on row 2 are correctly vertically-centred in themselves. I can try to add the fixup optional argument to \multirow, but this is within an automated process, so working out how much to add is problematic, as for the general case, there is no easy way to calculate the amount.

\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{array,multirow}
\pagestyle{empty}
\begin{document}
% code generated from Word OOXML via XSLT2
\begin{tabular}{@{}
    |>{\raggedright}p{3cm}
    |>{\raggedright}p{3cm}
    |>{\raggedright}p{3cm}
    |>{\raggedright}p{3cm}
    |@{}}\hline
% first row
\multirow{3}{3cm}[-2\baselineskip]{Tests:}
&\multicolumn{1}{|>{\raggedright}m{3cm}|}{Vortest}
&\multicolumn{1}{|>{\raggedright}m{3cm}|}{1. Nachtest}
&\multicolumn{1}{|>{\raggedright}m{3cm}|}{2. Nachtest}\\
% second row
&\multirow{2}{3cm}{Juni vor dem TSC}
&\multirow{2}{3cm}{September nach dem TSC}
&\multirow{2}{3cm}{Mai/Juni im Jahr nach dem TSC}
\\\cline{1-1} % this is line 24
% bogus third row
&&&\\\cline{1-1}\cline{2-2}\cline{3-3}\cline{4-4}
\end{tabular}
\end{document}

The other problem is that processing this document gets me the error:

! Misplaced \omit.
\@cline #1-#2\@nil ->\omit 
                       \@multicnt #1\advance \@multispan \m@ne \ifnum \@...
l.24     \\\cline{1-1}

! Leaders not followed by proper glue.
<to be read again> 
               \hfill 
l.24     \\\cline{1-1}

Best Answer

By default, multirow will center your text vertically, however your compiling error is what is stopping your code working. You must add \arraybackslash after your \raggedright in the column definition. Once that's done, you can remove your base line adjustment.

Also to simplify your code, you could define a couple of new column types:

\documentclass[preview,border=1px]{standalone}
\usepackage{array,multirow}
\pagestyle{empty}

\newcolumntype{P}{>{\raggedright\arraybackslash}p{3cm}}
\newcolumntype{M}{>{\raggedright\arraybackslash}m{3cm}}

\begin{document}
% code generated from Word OOXML via XSLT2
\begin{tabular}{@{}|P|P|P|P|@{}}\hline
% first row
\multirow{3}{3cm}{Tests:}
&\multicolumn{1}{M|}{Vortest}
&\multicolumn{1}{M|}{1. Nachtest}
&\multicolumn{1}{M|}{2. Nachtest}\\
% second row
&\multirow{2}{3cm}{Juni vor dem TSC}
&\multirow{2}{3cm}{September nach dem TSC}
&\multirow{2}{3cm}{Mai/Juni im Jahr nach dem TSC}
\\\cline{1-1} % this is line 24
% bogus third row
&&&\\\cline{1-1}\cline{2-2}\cline{3-3}\cline{4-4}
\end{tabular}
\end{document}

Fixing the multiple vertical lines is also done here by only adding the line after the column definition instead of both before and after:

enter image description here

However, I am not entirely sure what you are trying to achieve and the multirow in the second line are to me a bit confusing. Could you clarify what you are trying to achieve exactly?

Related Question