TransWikia.com

Vertical centering of multirowcell text when specifying other column widths

TeX - LaTeX Asked by trynthink on August 3, 2020

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]{>{centeringarraybackslash}m{#1}}

begin{document}

Table 1

begin{tabular}{|c|c|c|}
    hline
    Column A & Column B & Column C 
    hline
    multirowcell{4}{Some SomewhatLengthy Textin a multirowcell} & Table Text & lorem ipsum dolor sit amet consectetur 
    cline{2-3}
    & multirowcell{2}{A multirowcellMore 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 SomewhatLengthy Textin a multirowcell} & Table Text & lorem ipsum dolor sit amet consectetur 
    cline{2-3}
    & multirowcell{2}{A multirowcellMore 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

multirow is designed for on-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 SomewhatLengthy Textin a multirowcell} & Table Text & lorem ipsum dolor sit amet consectetur 
    cline{2-3}
    & multirowcell{2}{A multirowcellMore 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

Answered by Bernard on August 3, 2020

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

documentclass{article}
usepackage{makecell}
usepackage{nicematrix}
usepackage{array}
newcolumntype{C}[1]{>{centeringarraybackslash}m{#1}}

begin{document}

NiceMatrixOptions{cell-space-top-limit=1mm,cell-space-bottom-limit=1mm}


begin{NiceTabular}{wc{1in}wc{1in}C{1.2in}}[hvlines]
    Column A & Column B & Column C 
    Block{4-1}{makecell{Some SomewhatLengthy Text in a multirowcell}} & Table Text & lorem ipsum dolor sit amet consectetur 
    & Block{2-1}{makecell{A multirowcellMore 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.

  • However, the command Block don't create space and that's why I have specified the width of the first and the second column.

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

Output of the previous code

Answered by F. Pantigny on August 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP