40

I'd appreciate advise in centering a multirow text. In the MWE below the multirow text "spring" is NOT centered over the three rows that it spans.

\documentclass[10pt,a4paper,twoside,open=right,headinclude,footinclude]{scrreprt}
\usepackage{classicthesis-ldpkg}
\usepackage{multirow}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[htbf]
 \centering
    \begin{tabular}{|M{20mm}|M{20mm}|M{20mm}|} \hline
        Type & Specs & Uses\\ \hline
        -- & Model 1 & blah blah blah blah blah blah blah blah \\ \hline
        \multirow{3}*{spring} & Model 2 & blah blah blah blah blah blah blah blah \\ \cline{2-3}
        & Model 3 & blah blah blah blah blah blah blah blah \\ \cline{2-3}
        & Model 4 & blah blah blah blah blah blah blah blah \\ \hline
    \end{tabular}
\end{table}

\end{document} 

multirow text not centered

However, in the trivial case where each entry in the table contains only one line, the multirow text is correctly centered as the following MWE shows:

\documentclass[10pt,a4paper,twoside,open=right,headinclude,footinclude]{scrreprt}
\usepackage{classicthesis-ldpkg}
\usepackage{multirow}

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[htbf]
\centering
    \begin{tabular}{|M{20mm}|M{20mm}|M{20mm}|} \hline
        Type & Specs & Uses \\ \hline
        -- & Model 1 & blah \\ \hline
        \multirow{3}*{spring} & Model 2 & blah \\ \cline{2-3}
        & Model 3 & blah  \\ \cline{2-3}
        & Model 4 & blah \\ \hline
    \end{tabular}

\end{table}

\end{document}

multirow image centered

9 Answers 9

12

I suggest that you also load the array package, as it'll help center (vertically) the contents of a cell; in fact, if you load this package, you needn't bother with multirow at all.

Separately, and identical to the thrust of egreg's answer, I would strongly recommend you use the booktabs package to get (much!) better vertical spacing above and below "rules" (horizontal lines), resulting in a much more "professional" look of your tables. In the code and image below, observe the use of the command \cmidrule{1-3} to draw a line across the entire width of the table with a thickness of "cmidrule" rather than the thickness of "midrule". Finally, for the the layout of your tabular material, I'd recommend using the ordinary "p" column type for columns 1 and 2 to get their contents left-justified. :-)

\documentclass{scrreprt}
\usepackage{classicthesis}    % I don't have "classicthesis-ldpkg" on my system...
\usepackage{array,booktabs}
\newcommand{\bla}{blah blah blah blah blah blah blah blah}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{table}[ht]
  \centering
    \begin{tabular}{p{20mm}p{20mm}M{20mm}} \toprule
        Type   & Specs   & Uses \\ \midrule
        --     & Model 1 & \bla \\ \cmidrule{1-3}
               & Model 2 & \bla \\ \cmidrule(l){2-3}
        Spring & Model 3 & \bla \\ \cmidrule(l){2-3}
               & Model 4 & \bla \\ \bottomrule
    \end{tabular}
\end{table}
\end{document} 

enter image description here

5
  • 3
    This fix will not work if the row corresponding to Model-4 is not there.
    – Abhinav
    Commented Feb 16, 2017 at 2:34
  • @Abhinav - Indeed, if the question were different, the answer should differ as well, shouldn't it? Observe that this query was posted more than five years ago. If the OP had wanted to ask a follow-up question, e.g., about what to do if there was no "Model-4" block, I would have been happy to modify the answer as well.
    – Mico
    Commented Feb 16, 2017 at 6:35
  • 1
    @Mico What if the there are only two Model. Here in this example you placed 'Spring' word in 3rd row (middle one). So what if there is only two rows? how to centralize the word 'Spring'?
    – Hazem
    Commented Feb 1, 2018 at 11:56
  • @Hazem - Well, the entire table would look quite differently then, wouldn't it? Please post a new query to ask for guidance on how to format the tabular material you wish to typeset.
    – Mico
    Commented Feb 1, 2018 at 15:14
  • 1
    @Mico I posted this tex.stackexchange.com/questions/413229, please have a look.
    – Hazem
    Commented Feb 1, 2018 at 15:50
11

This is typical case where the \valign instead \halign primitive have to be used. The \valign is transposed primitive to the common used \halign. It means that data are inserted primary to the columns and separated by \cr, then these columns are composed alongside. Each data item becomes the maximal height of its row. The width of each data item is set as a constant in this example.

LaTeX table-like packages use \halign primitive. I don't know if the
\valign is used in some package because I don't know all the LaTeX
packages.

Nevertheless, I am able to show how the \valign works at the TeX primitive level. Sorry, it is not LaTeX.

\def\setrowparams{\hsize=25mm \leftskip=.5em \rightskip=\leftskip
   \emergencystretch=2em \noindent\hfil}

\def\blah{blah blah blah blah blah blah blah blah}
\def\blahh{\blah\ \blah\ \blah\  blah blah blah}

\def\multirow#1#2{\multispan{#1}\hrule\vfil\vbox{\setrowparams\topstrut#2\botstrut}\vfil}

\def\topstrut{\vbox to3ex{}} \def\botstrut{\vtop to2ex{}}

\vbox{\hbox{\vrule
   \valign{&\hrule\vfil\vbox{\setrowparams\topstrut#\unskip\botstrut}\vfil\cr
   Type   & & \multirow3{spring} \cr \noalign{\vrule}
   Specs  & Model 1 & Model 2 & Model 3 & Model 4 \cr \noalign{\vrule}
   Uses   & \hfilneg\blah & \hfilneg\blahh  & \hfilneg\blah &
   \hfilneg\blah \cr 
  \noalign{\vrule}   
}}\hrule}

\end

Now the word "spring" is verticaly centered at every circumstances of the filling the last colum by blah blah...

valign output

How can be this done by LaTeX? The answer "surround my code by LaTeX preamble and postamble" is not accepted.

1
  • 1
    good answer, I think this is the only answer I found which can align the text in a merge cell(multiply rows) correctly, but are there any Latex alternatives, you code is hard to understand and reused, thanks.
    – ollydbg23
    Commented Aug 25, 2015 at 11:50
8

As a person encountering a similar problem years after this question was last active, it seems that most of the answers here are suggesting things that are helpful in this case but not in general.

After searching through many other questions, I found this one [Vertical Alignment in multirow using cells with >1 lines ] to be the most helpful. Unfortunately the answer is basically "multirow just doesn't do this on its own, but you can tweak it case-by-case."

My own observations from playing with this are that multirow won't account for extra space between lines due to \addlinespace (I believe that booktabs adds extra vertical space around \midrule and \cmidrule, so multirow would be thrown off by these) and behaves as though all of the rows that it spans (except the first) are a single line (i.e. if the second or later row spans more than one line, the vertical spacing will be messed up).

3

This isn't an answer about \multirow, but rather a suggestion to use a different approach without vertical rules:

\usepackage{array,booktabs}

\begin{tabular}{ll>{\raggedright\arraybackslash}p{20mm}}
\toprule
Type & Specs & Uses \\
\midrule
       & Model 1 & \blah \\
\midrule
spring & Model 2 & \blah \\
\cmidrule{2-3}
       & Model 3 & \blah \\
\cmidrule{2-3}
       & Model 4 & \blah \\
\bottomrule
\end{tabular}

The interrupted horizontal rules make clear that "spring" applies to all entries in the same block delimited by full horizontal rules.

enter image description here

2
  • 1
    I don't think you've defined the macro blah explicitly in your MWE... :-)
    – Mico
    Commented Oct 5, 2011 at 14:02
  • 1
    \newcommand{\blah}{blah blah blah blah blah blah blah blah}
    – egreg
    Commented Oct 5, 2011 at 14:34
2

Vertical alignment is possible using makecell. It's contents looks like a mini-tabular with \\ to break lines as necessary. And, it "plays nice" with booktabs:

\documentclass{article}
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{makecell}% http://ctan.org/pkg/makecell
\newcommand{\bla}{%
  \makecell[r]{blah blah\\blah blah blah\\\midrule blah blah\\blah blah}%
}
\begin{document}
\begin{table}[ht]
  \centering
  \begin{tabular}{p{20mm}p{20mm}c} \toprule
    Type   & Specs   & Uses \\ \midrule
    --     & Model 1 & \bla \\ \cmidrule{1-3}
           & Model 2 & \bla \\ \cmidrule{2-3}
    Spring & Model 3 & \bla \\ \cmidrule{2-3}
           & Model 4 & \bla \\ \bottomrule
  \end{tabular}
\end{table}
\end{document} 

makecell

3
  • Werner -- booktabs and makecell might not be playing so nice with each other after all: The four midrules (inside the "blah" macro texts) appear to be "trimmed" both left and right even though you did not specify this behavior in the code. One might be able to address this issue, in the short run at least, by inserting @{} after c in the tabular specification.
    – Mico
    Commented Oct 5, 2011 at 16:18
  • @Mico: This is the expected behaviour in my opinion. I was merely illustrating the vertical alignment with respect to the other tabular entries. Throwing in the \midline was to show the tabular-ish \makecell interaction.
    – Werner
    Commented Oct 5, 2011 at 16:20
  • I didn't claim this behavior was an outright bug. My point is, rather, that if the horizontal lines don't all terminate at the same "meridian" (vertical line...), the table's appearance may be unnecessarily rough.
    – Mico
    Commented Oct 5, 2011 at 16:32
2

For me it was possible to with the following:

\usepackage{multirow}

\begin{table}
\caption{cascsac asdsad asd asd asd asss.}
\label{tab:FMEASA}
% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{tabular}{|c|p{2.5cm}|p{3cm}|p{2cm}|p{0.5cm}|p{3cm}|}
\hline
\multirow{4}[50]{1.5cm}{Up / Down blabla} & Interruption or cable asdfasdf & No sdf sadfsadf sdfsdaf & fhfgh check during fdhfgh  fgdhfdgh & Yes   & 1. fhfghby fdhfghfgh fghdfghv 2. fhdfghfhg of Automatic dgdsgdfg3. error dsgsdfg to dgdgdfg\\
\cline{2-6}      & sdfsdfsdf GND & No asdfsafd steering sfsadfs & As above & Yes   & As above \\
\cline{2-6}      & asdfsdfsd +12V & asdfsdfsd in the asdfsdf position; sadfsadf steering sdfasdf & As above & No    & As above \\
\cline{2-6}      & Mechanical break sdfsdf asdfsadf and sadfsdf & No asfsadfsd asdfasdf possible & By the user & Yes   & As above  \\
\hline
\end{tabular}%
\end{table}

where \multirow{4}[50]{1.5cm}{Up / Down blabla} where the 50 seems to operate as a percentage in the cases I have tested.

3
  • The optional [...] to multirow does the trick.
    – user9424
    Commented Dec 3, 2018 at 12:14
  • 1
    This does not work for me. It places the text under the table. The documentation says the optional parameter is for bigstruts.
    – Tvde1
    Commented Mar 4, 2019 at 10:41
  • @Tvde1 is correct. But you can do a final tweak according to the docs. Use the '<vmove>' parameter. For example (1ex): latex \multirow[c]{3}{*}[1ex]{1} & 2 & 3 & 4 & 5 \\ See the section on fine tuning. Commented Jun 19, 2023 at 11:44
2

It’s easy peasy with tblr environment of my new LaTeX3 package tabularray:

\documentclass[10pt,a4paper]{scrreprt}
\usepackage{tabularray}
\begin{document}

\begin{table}[ht]
 \centering
    \begin{tblr}{|Q[m,c,20mm]|Q[m,c,20mm]|Q[m,c,20mm]|} \hline
        Type & Specs & Uses\\ \hline
        -- & Model 1 & blah blah blah blah blah blah blah blah \\ \hline
        \SetCell[r=3]{m} Spring & Model 2 & blah blah blah blah blah blah blah blah \\ \cline{2-3}
        & Model 3 & blah blah blah blah blah blah blah blah \\ \cline{2-3}
        & Model 4 & blah blah blah blah blah blah blah blah \\ \hline
    \end{tblr}
\end{table}

\end{document}

enter image description here

2

It's easy with {NiceTabular} of nicematrix.

\documentclass[10pt,a4paper]{scrreprt}
\usepackage{nicematrix}
\begin{document}

\begin{table}[ht]
 \centering
    \begin{NiceTabular}[hvlines]{ m[c]{20mm} m[c]{20mm} m[c]{20mm} } 
        Type & Specs & Uses\\ 
        -- & Model 1 & blah blah blah blah blah blah blah blah \\ 
        \Block{3-1}{Spring} & Model 2 & blah blah blah blah blah blah blah blah \\ 
        & Model 3 & blah blah blah blah blah blah blah blah \\ 
        & Model 4 & blah blah blah blah blah blah blah blah \\ 
    \end{NiceTabular}
\end{table}

\end{document}

Output of the above code

0

Example 1

With cals, this type of tabulars are extremely easy to build:

\documentclass{article}
\usepackage{cals, calc}
\newcommand{\bla}{blah blah blah blah blah blah blah blah}

\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\newcommand{\col}{\dimexpr(20mm)+12.4pt\relax} % Calculate column width 12.4pt is two columnsep + vertical rule

\begin{document}

\begin{calstable}

% Defining 3 column 20mm+12.4pt to compensate for tabcolsep and arrayrulewith
\colwidths{{\col}{\col}{\col}}


% Set up the tabular
\makeatletter
\def\cals@framers@width{0.8pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.8pt}
\def\cals@bodyrs@width{0.4pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{}

% H1 Header
\thead{%
\bfseries
\brow
    \alignC\cell{Type} % alignment works to it is changed
    \cell{Specs}
    \cell{Uses}
\erow
\mdseries
}
% R1 Body
\brow
    \cell{}
    \cell{\vfil Model 1} % vfil centres vertically
    \cell{\bla}
\erow
% R2 Body
\brow
    \nc{lrt}
    \cell{\vfil Model 2}
    \cell{\bla}
\erow
% R3 Body
\brow
    \nc{lr}
    \cell{\vfil Model 3}
    \cell{\bla}
\erow
% R4 Body
\brow
    \nc{lrb}\sc{\vfil Spring}
    \cell{\vfil Model 4}
    \cell{\bla}
\erow
\makeatletter
\end{calstable}\par % \par needed to align the tabular

\end{document}

enter image description here

Example 2

Simulating booktabs requires to remove vertical and part of the horizontal rules. Since the spanning removes the horizontal rules between cells, we need only to change the code two places. In more complicated tabulars, we normally have to do more:

\def\cals@framecs@width{0pt}      % Turn off outside vertical rules
\def\cals@cs@width{0pt}           % Turn off inside vertical rules

The result with those two modifications:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .