The problem is in understanding precisely how tabular
works.
Each cell has a reference point of reference and all the reference points are eventually put on the same horizontal line. For l
, c
, and r
cells the reference point is on the baseline; for a p
cell the reference point is on the first line's baseline; for m
cells the reference point is midway from the top and the bottom of the text; for b
cells it's on the last line's baseline.
Thus a tabular
specification such as
{l m{3cm} p{3cm}}
will not place the middle cells of a row mid aligned with respect to the last cell, but rather its content will stick above the last column by (slightly less than) half its vertical size.
The kind of alignment you're trying to get can't be obtained without measuring the cells' contents, as Stefan Kottwitz's answer about \tabbox
shows: that 3.7\baselineskip
was good for that example, not in general.
Here's a way out, in your specific case (note the use of \adjustbox
for the picture):
\documentclass{article}
\usepackage{tikz}
\usepackage{array}
\usepackage{booktabs,adjustbox}
\newcommand{\finalcells}[2]{%
\begingroup\sbox0{\begin{minipage}{3cm}\raggedright#1\end{minipage}}%
\sbox2{\begin{minipage}{3cm}\raggedright#2\end{minipage}}%
\xdef\finalheight{\the\dimexpr\ht0+\dp0+\smallskipamount\relax}%
\xdef\finalheightB{\the\dimexpr\ht2+\dp2+\smallskipamount\relax}%
\ifdim\finalheightB>\finalheight
\global\let\finalheight\finalheightB
\fi\endgroup
\begin{minipage}[t][\finalheight][t]{3cm}\raggedright#1\end{minipage}&
\begin{minipage}[t][\finalheight][t]{3cm}\raggedright#2\end{minipage}}
\begin{document}
\begin{tabular}{c m{2cm} m{3cm} m{3cm} m{3cm}}
\toprule
\multicolumn{1}{c}{Header 1} &
\multicolumn{1}{c}{Header 2} &
\multicolumn{1}{c}{Header 3} &
\multicolumn{1}{c}{Header 4} &
\multicolumn{1}{c}{Header 5} \\
\midrule
Short Text &
Slightly Longer Text &
\adjustbox{valign=c}{%
\begin{tikzpicture}
% Draw beam and end lines
\draw (-1.5,-0.75) rectangle (1.5,0.75) ;
\end{tikzpicture}%
}
&
\finalcells{Some text that extends onto more than one line as there's plenty of it.}
{Some text that extends onto more than one line as there's plenty of it.
Some text that extends onto more than one line as there's plenty of it.} \\
\bottomrule
\end{tabular}
\end{document}
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:
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?
Best Answer
Use
\multirow
with the appropriate value in the first argument:Avoiding vertical rules in a table is a good practice; in this regard, the booktabs package would be of interest.