# Tabularx: Center a vertical table heading (rotated, multicolumn)

I want to get my vertical table heading ("Rows") to be centered.

The table looks like this

and it is created with that code:

\documentclass{scrreprt}

\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{rotating}
\usepackage{makecell}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{table}[ht]
\centering
\begin{tabularx}{.3\textwidth}{@{}Y|*{4}{Y}@{}}
\toprule
\multicolumn{2}{c}{} & \multicolumn{3}{c@{}}{\thead{Columns}} \\
\cmidrule(l){3-5}
\multicolumn{2}{c}{} & \thead{X} & \thead{Y} & \thead{Z} \\
\multirow{4}{*}{\rotatebox[origin=c]{90}{\thead{Rows}}}
& \thead{A} & ax & ay & az \\
& \thead{B} & bx & by & bz \\
& \thead{C} & cx & cy & cz \\
& \thead{D} & dx & dy & dz \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}


I know that \multirow has the option [<vmove>] and with that I could try and play around with some random values to get it looking almost centered. For example with this line \multirow{4}{*}[-.81em]{\rotatebox[origin=c]{90}{\thead{Rows}}}.

But this will not be exact and always when I add or delete a row, I have to find a new value for the vertical alignment. That can't be the right solution.

What is the proper way to center a multirow table heading? Thank you in advance!

## 2 Answers

With nicematrix:

\documentclass{scrreprt}

\usepackage{booktabs}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\usepackage{nicematrix}

\begin{document}

\begin{table}[ht]
\centering
\begin{NiceTabular}{@{}wc{\dimexpr 0.0666\textwidth-2\tabcolsep} !{\vrule}*{4}{wc{\dimexpr 0.0666\textwidth-2\tabcolsep}}@{}}
\toprule
\multicolumn{2}{c}{} & \Block{1-3}{\thead{Columns}} \\
\cmidrule(l){3-5}
\multicolumn{2}{c}{} & \thead{X} & \thead{Y} & \thead{Z} \\
\Block{4-1}{\rotate\thead{Rows}}& \thead{A} & ax & ay & az \\
& \thead{B} & bx & by & bz \\
& \thead{C} & cx & cy & cz \\

& \thead{D} & dx & dy & dz \\
\bottomrule
\end{NiceTabular}
\end{table}

\end{document}

• Not bad :) Thank you! But I do not like that the vertical line is now touching the bottom rule. That does not look as elegant as the tabularx version imo. Is there a way to change this? Jan 22 at 22:27
• @Max16hr: I have updated my answer accordingly. (Replaced | with !{\vrule} according to "5.1.1The vertical rules" from the nicematrix package manual.) Jan 22 at 22:33
• That is nice! But well ... it is still not perfect^^ Because now I realize that it is not centered properly. To see this, please replace the word Rows by the string ---------.---------. That should be placed perfectly next to the vrule if it would be centered. But it doesn't, i.e. it is a bit too low. Jan 24 at 17:06

Maybe simpler, you can use a non-integer number of rows. Here is possible code, in which I inderted \multirow in the last row, with a negative number of rows:

\documentclass{scrreprt}

\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{rotating}
\usepackage{makecell}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{table}[ht]
\centering
\begin{tabularx}{.3\textwidth}{@{}Y|*{4}{Y}@{}}
\toprule
\multicolumn{2}{c}{} & \multicolumn{3}{c@{}}{\thead{Columns}} \\
\cmidrule(l){3-5}
\multicolumn{2}{c}{} & \thead{X} & \thead{Y} & \thead{Z} \\
& \thead{A} & ax & ay & az \\
& \thead{B} & bx & by & bz \\
& \thead{C} & cx & cy & cz \\
\multirow{-5.45}{*}{\rotatebox[origin=c]{90}{\thead{Rows}}}
& \thead{D} & dx & dy & dz \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}


• Well, this looks good, but I think, there are the same problems. The number of -5.45 rows is also arbitrary and not exact. (Why not -5.6 or -5.39?) And always when I change my table and I have not 4 but 3, 6 or 14 rows, I have to figure out a new number of rows to make it looking almost centered. This is not generic, unfortunately. Jan 21 at 15:58
• The calue was determined by trial and error. The number of rows is exact only for one-lined row, without any vertical padding of the rules, and with arraystretch=1. For 14 rules, I would try first little more than 15, and would adjust the value. Jan 21 at 16:03
• Yes, trial and error is what I did as well as a workaround. But I am not satisfied with this^^ LaTeX is such a nice tool to make things perfect and not just approximate. So I hope there is a better solution :) Jan 21 at 16:11
• I'm afraid there is none, because, as I said, multirow is designed for standard i=one-lined rows. Ma y be with latex3, we'll have some package that makes it automatically… Jan 21 at 17:33