multirow (2) vertical center text

MWE:

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

\begin{tabular}{@{}l|llll@{}}
\toprule
\multicolumn{1}{c|}{\multirow{2}{*}{Test}} & \multicolumn{4}{c}{A} \\ \cmidrule(l){2-5}
\multicolumn{1}{c|}{}                      & 1   & 2   & 3   & 4   \\ \midrule
&     &     &     &     \\
&     &     &     &     \\ \bottomrule
\end{tabular}

\end{document}


Output:

Saw this, this. Somewhere, I have to use makecell or m, but not able to grasp. Any help in making the "Test" vertically align.

• Since rules from booktabs package add some vertical white space above and below them, hence the incompatibility with vertical lines (please keep that in mind) and the gaps around the intersections, \multirow{2} will not result in the expected output. You could try with something like \multirow{2.4} or other non-integer values. Aug 15, 2020 at 16:58
• Side note on your MWE: Compiling results in a bunch of error messages since you can't have a float like table inside of the standalone class. Either keep the table and switch to article or a different standard class or stick with standalone and remove the table. Aug 15, 2020 at 17:00
• Depending on the actual contents of your table, it might be better to not vertically center the contents, but leave them top aligned as they usually are. If you with to keep the booktabs rules, stay away from vertical lines entirely. Aug 15, 2020 at 17:02
• See also: Proper centering with cmidrule and multi- row and column, Vertical alignment using multirow and booktabs and Vertically centering of text in multirow in table when using \cmidrule for more related questions that address vertically centered contents of \multirow next to a \cmidrule. Aug 15, 2020 at 17:06
• @leandriis, Thanks for the link, 1st link indeed solved my problem. changed \multirow{2}{*}{Test} to \multirow{2}[3]{*}{Test}. However, did not understand what [N] does. Can you explain it? Aug 15, 2020 at 17:52

Here is what you can do with {NiceTabular} of nicematrix (with the latest version: 5.4 of 2020-10-06).

\documentclass{article}
\usepackage{booktabs}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{@{}l|llll@{}}
\toprule
\Block{2-1}{Test} & \Block{1-4}{A} \\ \cmidrule(l){2-5}
& 1   & 2   & 3   & 4   \\ \midrule
&     &     &     &     \\
&     &     &     &     \\ \bottomrule
\end{NiceTabular}

\end{document}

• In {NiceTabular}, you use \Block to merge cells both vertically and horizontally.

• The content of the block is composed at the mathemtatical center of the rectangle of the merged cells (not as with \multirow).

• The vertical rules are not broken and thus, are compatible with booktabs (but you must be aware that the use of vertical rules is not at all in the spirit of booktabs).

• You need several compilations (because nicematrix uses PGF/Tikz nodes).

• Good to know about nicematrix, thanks. Aug 15, 2020 at 17:54

Avoid verticals for a better impact and troubles with gaps

The \multirow option can be changed to decimal values for finer up-down adjustment/centering -- here changed to value of 2.4

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

\begin{document}
\begin{table}[]
\begin{tabular}{@{}lllll@{}}                                      \toprule
\multirow{2.4}{*}{Test}         & \multicolumn{4}{c}{A}                 \\ \cmidrule(l){2-5}
& 1                     & 2   & 3   & 4 \\ \midrule
& X                     & Y   & Z   & A \\
& P                     & Q   & R   & S \\ \bottomrule
\end{tabular}
\end{table}

\end{document}