# Booktabs table with rotated and aligned header, multirows, and tabularx columns

I have a problem with the alignment in a bit complicated table setting (see MWE). I have a table normally stretching the complete width of the paper (for illustrating here the nr of columns is reduced and the width of the table is set to 9cm). I'm using the X option of the tabularx package to stretch all columns (except of the first two). The headers of the X columns are rotated. The content of the X columns is always two lines per cell.

What I would like to achieve is the following:

1. The text in the first column should be centred between the text of the second column (e.g. Test1 centred between XY and ZY)
2. The text in the second column should be centred between the two lines of the X columns (e.g. XY centred between the line beginning with 0,0+ and the line beginning with 1,0)
3. The headers should be aligned to the bottom so that all begin at the midrule line.
4. The headers should be centred in their column.

I think I achieved point 1 and 2. Maybe Test1 etc. could be slightly lower to be centred between the lines. But I couldn't figure out how to achieve point 3 and 4.

Would be nice if someone could help me?

MWE:

\documentclass{article}
\usepackage{tabularx,rotating,booktabs,multirow}
\usepackage[utf8]{inputenc}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcommand*\rot{\rotatebox{90}}

\newcommand{\forloop}[5][1]{
\setcounter{#2}{#3}%
\forloop[#1]{#2}{\value{#2}}{#4}{#5}}%
{}}

\newcounter{crcounter}

\newcommand{\compensaterule}[1]{%
\forloop{crcounter}{1}{\value{crcounter} < #1}%
{\vspace*{-\aboverulesep}\vspace*{-\belowrulesep}}}

\newcommand{\multirowbt}[3]{\multirow{#1}{#2}%
{\compensaterule{#1}#3}}

\begin{document}

\begin{table}[tbp]
\centering
\label{tab:Test}
\begin{tabularx}{9cm}{p{2.5cm} p{2.0cm} *{3}{>{\raggedright\arraybackslash}X}}
\textbf{Column1}                & \textbf{Column2}
& \rot{Long column name}
& \rot{Column4}
& \rot{Column5} \\
\midrule
\multirowbt{2}{*}{Test1}    & XY & $0,0 + 1,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\cmidrule(lr){3-5}
\multirowbt{2}{*}{Test2}    & XY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\cmidrule(lr){3-5}
\multirowbt{2}{*}{Test3}    & XY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\cmidrule(lr){3-5}
\multirowbt{2}{*}{Test4}    & XY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

• You could use some \multicolumns as in \multicolumn{1}{c}{\textbf{Column1}} & \multicolumn{1}{c}{\textbf{Column2}} & \multicolumn{1}{c}{\rot{Long column name}} & \multicolumn{1}{c}{\rot{Column4}} & \multicolumn{1}{c}{\rot{Column5}} – Gonzalo Medina Jul 21 '15 at 15:39
• @GonzaloMedina It shifts the rotated header a bit to the right, but also a lot to the top. So for question 4 I think it works. Do you also have an idea for question 3? – user2653422 Jul 21 '15 at 16:12
• At least with your example code in the question, my suggestion would also solve 3, wouldn't it? – Gonzalo Medina Jul 21 '15 at 16:17
• Should have tested it for all columns not just one. So yes, your answer is perfectly correct. Thank you very much! – user2653422 Jul 21 '15 at 16:27
• @GonzaloMedina Do you want to add an answer? – Johannes_B Oct 4 '15 at 14:26

Since the question was solved in comments, @GonzaloMedina is not much active anymore, and it was requested to add an answer, here it is:

Using \multicolumn{1}{c}{column text} for the column header, you supersede the original column settings for the column you're typesetting, while creating a centered column, thus here is Gonzalo's solution

\documentclass{article}
\usepackage{tabularx,rotating,booktabs,multirow}
\usepackage[utf8]{inputenc}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcommand*\rot{\rotatebox{90}}

\newcommand{\forloop}[5][1]{
\setcounter{#2}{#3}%
\forloop[#1]{#2}{\value{#2}}{#4}{#5}}%
{}}

\newcounter{crcounter}

\newcommand{\compensaterule}[1]{%
\forloop{crcounter}{1}{\value{crcounter} < #1}%
{\vspace*{-\aboverulesep}\vspace*{-\belowrulesep}}}

\newcommand{\multirowbt}[3]{\multirow{#1}{#2}%
{\compensaterule{#1}#3}}

\begin{document}

\begin{table}[tbp]
\centering
\label{tab:Test}
\begin{tabularx}{9cm}{p{2.5cm} p{2.0cm} *{3}{>{\raggedright\arraybackslash}X}}
\multicolumn{1}{c}{ \textbf{Column1}}                &\multicolumn{1}{c}{ \textbf{Column2}}
&\multicolumn{1}{c}{ \rot{Long column name}}
& \multicolumn{1}{c}{\rot{Column4}}
& \multicolumn{1}{c}{\rot{Column5}} \\
\midrule
\multirowbt{2}{*}{Test1}    & XY & $0,0 + 1,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\cmidrule(lr){3-5}
\multirowbt{2}{*}{Test2}    & XY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\cmidrule(lr){3-5}
\multirowbt{2}{*}{Test3}    & XY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\cmidrule(lr){3-5}
\multirowbt{2}{*}{Test4}    & XY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
& ZY & $0,0 + 0,0$ & $0,0 + 0,0$ & $0,0 + 0,0$          \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}


Alongside with its output: