4

I'm unable to center a table's multirow vertically (it works only horizontally):

\def\arraystretch{1.5}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{table}[t]
    \caption{Caption.}
    \label{table:label}

    \begin{tabularx}{\columnwidth}{| Y | Y | X | X |}
        \hline
         & OA & Acc & Time [minutes] \\
        \hline 

        \multirow{2}{*}{\hfil A} & \multirow{2}{*}{100\%} & 100\% & x \\
        \cline{3-4}
         & & 100\% & y \\
        \hline

        \multirow{2}{*}{B} & & 100\% & 100\% \\
        \cline{3-3}
         & - & Urban areas: 100\% & \\
        \hline
    \end{tabularx}
\end{table}

2 Answers 2

4

What is important for multirow is not the number of rows it has to encompass, but the number n of lines. These may be different for cells in paragraph mode like p,m, b or X. Changing the value of \arraystretch also makes the computation of equivalent lines more complex. Since version 2.0, if I remember well, multirow accepts a number of lines with decimal values, which makes it easier to fine-tune the vertical position of the contents. Here, I should have taken n=3, but n=2.8 yields a better result.

Unrelated: I replaced the hyphen in the last \multirow, which is irrelevant here, with a more correct endash. Also I loaded caption to have a decent spacing between caption and table.

    \documentclass{article}
    \usepackage{ragged2e}
    \usepackage{multirow, tabularx, caption}
    \captionsetup{skip=6pt}
    \newcolumntype{Y}{>{\centering\arraybackslash}X}

    \begin{document}

    \def\arraystretch{1.5}
    \begin{table}[t]
    \caption{Caption.}
    \label{table:label}
    \begin{tabularx}{\columnwidth}{| Y | Y | *{2}{ >{\RaggedRight\arraybackslash}X |}}
        \hline
         & OA & Acc & Time [minutes] \\
        \hline
        \multirow{2}{*}{A} & \multirow{2}{*}{100\,\%} & 100\,\% & x \\
        \cline{3-4}
         & & 100\% & y \\
        \hline
        \multirow{2.8}{*}{ B} &\multirow{2.8}{*}{–} & 100\,\% & 100\,\% \\
        \cline{3-3}
         & & Urban areas: 100\,\% & \\
        \hline
    \end{tabularx}
    \end{table}

    \end{document} 

enter image description here

4

multirow cannot predict the height correctly when one changes the default line spacing via redefining \arraystretch. In this case you need to manually adjust the vertical position of a multirow cell contents by passing the option [-6pt], for example, to the multirow command.

\renewcommand\arraystretch{1.5}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{table}[t]
    \caption{Caption.}
    \label{table:label}

    \begin{tabularx}{\columnwidth}{| Y | Y | X | X |}
        \hline
         & OA & Acc & Time [minutes] \\
        \hline 

        \multirow{2}{*}{A} & \multirow{2}{*}{100\%} & 100\% & x \\
        \cline{3-4}
         & & 100\% & y \\
        \hline

        \multirow{2}{*}[-6pt]{B} & & 100\% & 100\% \\ % ---> [-6pt]
        \cline{3-3}
         & - & Urban areas: 100\% & \\
        \hline
    \end{tabularx}
\end{table}
2
  • what about the dash sign (-) in the last row of the table, how can it be centered vertically?
    – Hakim
    Aug 13, 2018 at 22:12
  • You do the same thing as you did for B, that's the second row before last one reads: \multirow{2}{*}[-6pt]{B} & \multirow{2}{*}[-6pt]{-} & 100\% & 100\% \\. This will put {-} at the same level as B.
    – AboAmmar
    Aug 13, 2018 at 23:10

You must log in to answer this question.

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