2

I have tables where I am trying to use two features together:

  1. multirowcell (from the makecell package) for cells that span multiple rows with new lines inserted into the text in specific locations, and
  2. specifying a fixed width for one or more other columns where I want the text to wrap (and to better control the overall table width).

When using these features together, I would like the text in the multirowcells to be vertically centered.

When using default table column types, makecell will vertically center the multirowcell text automatically. Table 1 in the MWE illustrates this behavior.

If a column type that allows for specifying the width of a column is used, even for a column in the table that does not use multirowcell, Table 2 in the MWE shows that makecell won't successfully vertically center the text in the multirowcells. In this example I've used a custom column type, but the same problem occurs when using, e.g., p{}.

Is there a way to help makecell vertically center the text when another column in the table has a specified width that causes the text in that column to wrap?

I tried using \Gape[][]{}, but even a large negative value for the depth correction will not vertically center the text. Inserting a \vspace{} above the text in the affected cells does work, but it requires guess-and-check approximation of the correct adjustment length for each affected cell. A solution that doesn't require specifying absolute length corrections would be ideal since I have several tables with different configurations that are affected.

\documentclass{article}
\usepackage{makecell}
\usepackage{multirow}

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

Table 1

\begin{tabular}{|c|c|c|}
    \hline
    Column A & Column B & Column C \\
    \hline
    \multirowcell{4}{Some Somewhat\\Lengthy Text\\in a multirowcell} & Table Text & lorem ipsum dolor sit amet consectetur \\
    \cline{2-3}
    & \multirowcell{2}{A multirowcell\\More text} & adipiscing elit sed do elusmod tempur \\
    \cline{3-3}
    & & incididunt ut labore et dolore magna aliqua \\
    \cline{2-3}
    & Something & Whatever \\
    \hline
\end{tabular}

Table 2

\begin{tabular}{|c|c|C{1.2in}|}
    \hline
    Column A & Column B & Column C \\
    \hline
    \multirowcell{4}{Some Somewhat\\Lengthy Text\\in a multirowcell} & Table Text & lorem ipsum dolor sit amet consectetur \\
    \cline{2-3}
    & \multirowcell{2}{A multirowcell\\More text} & adipiscing elit sed do elusmod tempur \\
    \cline{3-3}
    & & incididunt ut labore et dolore magna aliqua \\
    \cline{2-3}
    & Something & Whatever \\
    \hline
\end{tabular}

\end{document}

Illustration of tables without and with fixed column widths illustrating that multirowcell will not vertically center text in those cells with fixed column widths for some columns.

2 Answers 2

2

\multirow is designed for one-line cells, so you have to count the total number of lines, not of rows. In addition, adding vertical padding of cells may change the equivalent number of lines. Also, you can adjust the number of equivalent rows using a decimal number.

Here is a code for this case:

\begin{tabular}{|c|c|C{1.2in}|}
    \hline
    Column A & Column B & Column C \\
    \hline
    \multirowcell{7}{Some Somewhat\\Lengthy Text\\in a multirowcell} & Table Text & lorem ipsum dolor sit amet consectetur \\
    \cline{2-3}
    & \multirowcell{2}{A multirowcell\\More text} & adipiscing elit sed do elusmod tempur \\
    \cline{3-3}
    & & incididunt ut labore et dolore magna aliqua \\
    \cline{2-3}
    & Something & Whatever \\
    \hline
\end{tabular}

enter image description here

2
  • How does one calculate the correct number of rows to use? Based on the multirow documentation, it seems like the correct count is 9 for the multirowcell in Column A (counting rows in Column C), but on testing, 9 shifts that text too low in the cell. Is there a way to calculate the length programmatically with, e.g., the calc package?
    – trynthink
    Mar 3, 2020 at 18:30
  • 1
    Programmatically, I don't know. When I have to use multirow, I always start from the number of lines, and adjust this value if necessary.
    – Bernard
    Mar 3, 2020 at 18:42
1

Here is what you can do with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\NiceMatrixOptions{cell-space-limits=1mm}


\begin{NiceTabular}{ccC{1.2in}}[hvlines]
    Column A & Column B & Column C \\
    \Block{4-1}{Some Somewhat\\Lengthy Text\\ in a multirowcell} & Table Text & lorem ipsum dolor sit amet consectetur \\
    & \Block{2-1}{A multirowcell\\More text} & adipiscing elit sed do elusmod tempur \\
    & & incididunt ut labore et dolore magna aliqua \\
    & Something & Whatever \\
\end{NiceTabular}
\end{document}
  • In {NiceTabular}, you have a command \Block for merged cells. You specifiy the number of rows and the number of columns.

  • The key hvlines draws all the rules excepted in the blocks.

Output of the previous code

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.