2

I am having an issue that I cannot resolve, even after searching online, I apologize as I'm fairly new to Latex and this is basically my first table (which needs to span over multiple pages).

As you can see, Group 1 and all Zones are not vertically centered. Moreover, the "End of Table" remark is also not horizontally centered properly. I tried applying solutions I found only, but none worked - I believe I'm doing something wrong here...

enter image description here

The code:

\documentclass{article}

\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pdflscape}
 
\begin{document}
\begin{landscape}
\begin{longtable}{p{5cm} @{}l p{10cm}}
\toprule
Risk Category & Zone & Description \\
\midrule
\endfirsthead

% Longtable rules for subsequent pages
\multicolumn{3}{c}%
{{\tablename\ \thetable{} -- Continued from previous page}} \\
\toprule 
Risk Category & Zone & Description \\
\midrule
\endhead

% Footer for all pages except last page.
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot

% Footer forthe  last page
\bottomrule
\multicolumn{3}{r}{{End of table.}} \\
\endlastfoot

\multirow{2}{*}{Group 1} & Zone A of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\cmidrule{2-3}

& Zone B of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule

& Zone C & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\ 
\midrule

& Zone D & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\ 


\end{longtable}
\end{landscape}
\end{document}
4
  • Your example needs external files we do not have. So it cannot be used to reproduce the problem without changing it and therefore without guessing. Please always try to show a minimal working example, that does not need any changes to reproduce the problem. In your case you also should eliminate the usage of subfiles.
    – cabohah
    Feb 14 at 11:02
  • @cabohah thank you! I added the content of the subfile, which primarily contained my packages.
    – Mr42
    Feb 14 at 11:04
  • @cabohah thank you for pointing out! Also minimized the preamble.
    – Mr42
    Feb 14 at 11:09
  • @cabohah I noticed that the centering works when adjacent columns contain only a single line, multirow fails when there are multiple lines in other columns. Moreover, using fixup \multirow{2}{*}[-1.4em]{Group 1} as shown here works - is there a way to automatically adjust this height, instead of using fixed values like -1.4 in my case?
    – Mr42
    Feb 14 at 11:41

2 Answers 2

2

As supplement to to nice @cabohah answer (+1), but with use of tabularray package. Using its longtblr table the code is more clear and concise (shorter). Table has (to my opinion) more appealing looks:

enter image description here

Syntax of tabularray package a little bit differ from other (classic) tables. for example instead of \multirow and multicolumn environments it define \SetCell[r=<num of rows>]{<style} <content> and \SetCell[c=<num of columns>]{<style} <content>. For more details you need to read package documentation. It contain many examples.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\usepackage{pdflscape}

\begin{document}
    \begin{landscape}
\begin{longtblr}[
caption = {does your table has a caption?},
  label = {tab:long}
                ]{colspec = {*{2}{Q[l]} X[l,m]},
                  cell{2}{1} = {r=2}{},  
                  rowhead = 1,
                  }
\toprule
Risk Category   & Zone      & Description \\
\midrule
\SetCell[r=2]{}     Group 1
                & Zone A of Group 1 
                            & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule
                & Zone B of Group 1 
                            & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule
                & Zone C    & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule
                & Zone D    & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\midrule[\heavyrulewidth]
\SetCell[c=3]{r}    End of table.           \\
    \end{longtblr}
\end{landscape}
\end{document}
0
2

The first mandatory argument of \multirow is the number of rows. Your table has multi-line table rows and also uses booktab lines. So the value 2 is to low. Increasing the number can help to improve the vertical centering.

To vertically center the “Zone” cells, I would instead suggest to use m-columns instead of p-columns. These are vertically centered to the baseline of the row. m-columns need package array.

Example:

\documentclass{article}

\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pdflscape}
 
\begin{document}
\begin{landscape}
\begin{longtable}{m{5cm} @{}l m{10cm}}
\toprule
Risk Category & Zone & Description \\
\midrule
\endfirsthead

% Longtable rules for subsequent pages
\multicolumn{3}{c}%
{{\tablename\ \thetable{} -- Continued from previous page}} \\
\toprule 
Risk Category & Zone & Description \\
\midrule
\endhead

% Footer for all pages except last page.
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot

% Footer forthe  last page
\midrule[\heavyrulewidth]
\multicolumn{3}{r}{{End of table.}} \\
\endlastfoot

\multirow[c]{3}{*}{Group 1} & Zone A of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\cmidrule{2-3}

& Zone B of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule

& Zone C & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\ 
\midrule

& Zone D & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\ 


\end{longtable}
\end{landscape}
\end{document}

enter image description here

BTW: For the first column, IMHO l would be better. And, e.g., with package xltabular the width of the last column can be detected automatically:

\documentclass{article}

\usepackage{xltabular}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pdflscape}
 
\begin{document}
\begin{landscape}
  \renewcommand*{\tabularxcolumn}[1]{m{#1}}
  \begin{xltabular}{\linewidth}{llX}
\toprule
Risk Category & Zone & Description \\
\midrule
\endfirsthead

% Longtable rules for subsequent pages
\multicolumn{3}{c}%
{{\tablename\ \thetable{} -- Continued from previous page}} \\
\toprule 
Risk Category & Zone & Description \\
\midrule
\endhead

% Footer for all pages except last page.
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot

% Footer forthe  last page
\midrule[\heavyrulewidth]
\multicolumn{3}{r}{{End of table.}} \\
\endlastfoot

\multirow[c]{3}{*}{Group 1} & Zone A of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\
\cmidrule{2-3}

& Zone B of Group 1 & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column.\\
\midrule

& Zone C & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\ 
\midrule

& Zone D & This is a fairly long sentence that will cause multiple line breaks in the document therefore creating many rows in this column. \\ 


\end{xltabular}
\end{landscape}
\end{document}

enter image description here

Note: I've not horizontally centered “End of table”, because it seems you've already found out, that you have to use \multicolumn{3} to span all three columns. However, if you don't want the text right aligned by centered, you would need \multicolumn{3}{c}{{End of table.}}.

2
  • Thank you so much! This works for the demonstrated solution. However, when the text "Group 1" is replaced with a longer text, the respective cell will overflow into the following cell (e.g., Zone C, or even D - going over the midrule). The rowheight is somewhat not updated if the text for Group 1 changes. Maybe you know of a way to fix the row overflow? An image. To replicate the issue, I changed the column style from {l l X} to {X l X} and \multirow[c]{3}{*}{Group 1} to \multirow[c]{3}{=}{Group 1} + lengthened text of Group 1.
    – Mr42
    Feb 14 at 15:01
  • Spanning rows with text, that is longer than the text in the other rows is problematic using multirow. If you need this, I would recommend to use tabularray. But note: tabularray is slow. So if time is critical, e.g., using overleaf free, this could be a problem.
    – cabohah
    Feb 15 at 7:47

You must log in to answer this question.

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