tables – Using Multirow Inside a Tabular Column Declaration in LaTeX

multirowtables

Is it possible to use \multirow command inside the column declaration of the tabular environment (provided by array package)?

I want to obtain the following result:

desired result

Which I can achieve by doing that:

\documentclass{article}
\usepackage{array,booktabs,multirow}
\begin{document}

\begin{tabular}{%
        >{\centering\arraybackslash}m{3cm}% 1st column
        m{2.5cm}% 2nd column
    }
    \toprule
    text & \multirow{2}{=}[-2.5pt]{\centering$a+b$} \\
    \cmidrule(r){1-1}
    text &  \\
    \midrule
    text & \multirow{2}{=}[-2.5pt]{\centering$x+y$} \\
    \cmidrule(r){1-1}
    text &  \\
    \bottomrule
\end{tabular}

\end{document}

However, I tried to clean things up a bit:

\documentclass{article}
\usepackage{array,booktabs,multirow}
\begin{document}

\begin{tabular}{%
        >{\centering\arraybackslash}m{3cm}%
        >{\multirow{2}{=}[-2.5pt]{\centering\arraybackslash\(}m{2.5cm}<{\)\}}% <--- added '\multirow' here
    }
    \toprule
    text & a+b \\      % <--- Removed '\multirow' from here
    \cmidrule(r){1-1}
    text &  \\
    \midrule
    text & x+y \\      % <--- Removed '\multirow' from here
    \cmidrule(r){1-1}
    text &  \\
    \bottomrule
\end{tabular}

\end{document}

This second approach obviously won't work, since neither <{\)\}} nor <{\)}} works.

I guess \} should print the brace, while } just closes everything as <{\)} and leaves another } remaining.

So, is it possible to close this brace somehow? That is, to make >{\multirow{2}{=}[-2.5pt]{\centering\arraybackslash\(}m{2.5cm}<{\)}} works.


Note:

I'm aware of the possibility of declaring a command such as

\newcommand{\MR}[1]{\multirow{2}{=}[-2.5pt]{\centering\ensuremath{#1}}}

to use like \MR{a+b}.

Best Answer

Life is easier if you use my new LaTeX3 package tabularray to make multirow or multicolumn cells:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}

\begin{tblr}{
  colspec = {Q[c,m,3cm]m{2.5cm}}, 
  cell{odd}{2} = {r=2}{c}, % multirow
}
  \toprule
    text & $a+b$ \\
  \cmidrule[r]{1-1}
    text &       \\
  \midrule
    text & $x+y$ \\
  \cmidrule[r]{1-1}
    text &       \\
  \bottomrule
\end{tblr}

\end{document}

enter image description here

Updated: From version 2021N, tabularray supports trim options l, r and lr, but you need to put them inside square brackets.