0

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}.

2

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.

6
  • +1. In this particular case the advantage of tblr comes especially to the fore.
    – Zarko
    Aug 17 '21 at 0:45
  • Definitely a cleaner code! Is it possible to use that with longtable? Nota: Eu uso o overleaf e por algum motivo ele reclama sobre não encontrar o tabularray.sty. Eu precisei colocar esse arquivo manualmente nele para que tudo funcionasse, você sabe algo sobre isso?
    – Leone
    Aug 17 '21 at 1:05
  • Yes, there is a longtblr environment. Please read the manual.
    – L.J.R.
    Aug 17 '21 at 1:07
  • I can not read your words in other language. I can only guess: tabularray is a new package and not in overleaf yet. You can download tabularray.sty and ninecolors.sty from CTAN and upload them to your TeX project folder in overleaf.
    – L.J.R.
    Aug 17 '21 at 1:12
  • 1
    @Leone With version 2021N of tabularray, trim options l, r and lr are supported. I have updated my answer accordingly.
    – L.J.R.
    Sep 2 '21 at 7:40
1

One way to simplify your first MWE is to use \multirowsetup and change its default settings as follows:

\renewcommand\multirowsetup{\centering}

Using it, your first MWE become:

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

\renewcommand\multirowsetup{\centering}
\begin{document}

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

\end{document}

and compilation result of it is:

enter image description here

Edit: If you like to make some shortcut for this particular multirow cell, you can define it as:

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

And than this MWE write as:

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

\renewcommand\multirowsetup{\centering}
\newcommand{\MR}[1]{\multirow{2}{=}[-2.5pt]{$#1$}}
\begin{document}

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

\end{document}

Result is the same as before.

4
  • I was about to say that \multirowsetup was kind of already covered by declaring a new command (see my note in the question) and you included it in your answer :D. Well, I will let this question opened for a while, but probably \newcommand is the best way out.
    – Leone
    Aug 17 '21 at 0:03
  • @Leone, to my opinion, it is not :-(. It hasn't any flexibility. For example, what you will do, if multirow should span more than two text lines? Or of the distance around horizontal lines is different as in its definition is anticipated? etc ... well, if your definition for MR solve your problem, what is the question?
    – Zarko
    Aug 17 '21 at 0:21
  • Now I would say that my question is a matter of curiosity. I would like to know about the possibility of doing something like my second approach (to open and close commands in column declaration). The truth is: I simplified my problem. While I was implementing a MWE to post this question here, I didn't realized that the lack of flexibility would be a problem to me. At first I "solved" my problem by declaring MR, then I came up with the idea of setting the column up at the column declaration field (to avoid having to put MR in every cell). >>>
    – Leone
    Aug 17 '21 at 0:49
  • >>> Only after that I faced one problem that you listed: the number of lines in which multirow spans. So... my second approach, even if it somehow work, will never fit my current needs (I will have to declare a MR command with some options, no problem), but if it did, would it be possible to do?
    – Leone
    Aug 17 '21 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.