1

I’m quite new to LaTeX and I’m having a real hard time writing a table that contains some large text in LaTeX.

My goal is to draw a 7 columns by 17 rows table. Each row is subdivided in a 2-row multirow spanning columns 2 to 4. Some cells in columns 5, 6 & 7 span across two rows to reduce repetitive text (see the image below).

Since the table is too large to fit into one page, I'm using the longtable package, and to tweak the look of the table I'm additionally also using the lscape (landscape), array and multirow packages to tweak the look of the table and for the arrangement of some multi-column cells.

The thing is that some cells containing large text is overflowing to the cells below instead of stretching the height of its row. This problem does not seem to happen when not using the multirow{}{}{} command.

I've searched far and wide on this and other forums for the solution, and have also skimmed the aforementioned package manuals (plus tabular etc.), but after opening more tabs than I care to admit, I give up. A suggestion might be to change the overall design of the table, or even break it in smaller column chunks, but I don't have a choice. The table looks OK-ish in PDF after exporting it from Excel, so I could use \includepdf from package pdfpages, but this would be a last resort route.

The image below is a sample of what the first few rows of my table would look like in excel:

sample of what the first few rows of my table would look like

The MWE below uses package lipsum intermittently to generate some dummy placeholder text (instead of the original):

\documentclass{article}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage[left=2.00cm, right=2.00cm, top=2.00cm, bottom=2.00cm]{geometry}
\usepackage{lscape}
\usepackage{lipsum} % For dummy text
\begin{document}
\newcommand\columnA{0.2\textwidth}
\newcommand\columnB{0.11\textwidth}
\newcommand\columnC{0.13\textwidth}
\newcommand\columnD{0.13\textwidth}
\newcommand\columnE{0.13\textwidth}
\newcommand\columnF{0.3\textwidth}
\newcommand\columnG{0.13\textwidth}
\begin{landscape}
\begin{small}
\begin{longtable}[c]{@{\extracolsep{\fill}}
    >{\raggedright\arraybackslash}m{\columnA}
    >{\centering\arraybackslash}m{\columnB}
    >{\centering\arraybackslash}m{\columnC}
    >{\centering\arraybackslash}m{\columnD}
    >{\centering\arraybackslash}m{\columnE}
    >{\centering\arraybackslash}m{\columnF}
    >{\centering\arraybackslash}m{\columnG}
    @{}}
\caption{A very big ugly table.\label{table1}}\\

\multirow{2}{\columnA}{\textbf{Study design}} & \multicolumn{3}{c}{\textbf{Participants}} & \multirow{2}{\columnE}{\textbf{Device name (Control options)}} & \multirow{2}{\columnF}{\textbf{User-environment interaction}} & \multirow{2}{\columnG}{\textbf{Type of outcomes assessed}} \\ \cline{2-4}
& \textbf{N} & \textbf{Description*} & \textbf{Diagnosis} & & & \\ \hline
\multicolumn{7}{c}{\textbf{Some category here}} \\ \hline
\endfirsthead
% additional pages' table head rows go here
\endhead
% additional pages' table foot rows go here...
\endfoot
\hline
\multicolumn{7}{l}{Table foot row 1}\\
\multicolumn{7}{l}{* Another foot row 2 \lipsum[10][2] }\\
\endlastfoot

\multirow{2}{\columnA}{Row 1 \lipsum[1][1]} & \textbf{Cases: 4} & \lipsum[1][2] & \lipsum[1][3] & \multirow{2}{\columnE}{\lipsum[1][4]} & \multirow{2}{\columnF}{\lipsum[1][5]} & \multirow{2}{\columnG}{\lipsum[1][6]} \\
& \textbf{Controls: 4} & Not reported & Just three words & & & \\
\multirow{2}{0.2\textwidth}{Row 2 \lipsum[2][1-2]} & \textbf{Cases: 10} & \lipsum[2][2] & \lipsum[2][3] & \multirow{4}{\columnE}{Quite long multirow tex:t \lipsum[2][4]} & \multirow{4}{\columnF}{Super long text \lipsum[2][1-15]} & \multirow{2}{\columnG}{\lipsum[2][6]} \\
& \textbf{Controls: -} & - & - & & & \\
\multirow{2}{\columnA}{Row 3 \lipsum[3][1]} & \textbf{Cases: 1} & two words & some more & & & \multirow{2}{\columnE}{Not that many words here either} \\
& \textbf{Controls: -} & - & - & & & \\
\multirow{2}{\columnA}{Row 4 \lipsum[4][1]} & \textbf{Cases: 9} & \lipsum[4][5] & Good & \multirow{2}{\columnE}{Couple of words} & \multirow{4}{\columnF}{Another short phrase with few words} & \multirow{2}{\columnG}{Yet another short text \& Only two words} \\
& \textbf{Controls: 20} & \lipsum[4][7] & Two words again & & & \\
\multirow{2}{\columnA}{Row 5 \lipsum[5][1-3]} & \textbf{Cases: 4} & something short & Bad & \multirow{2}{\columnE}{Something relatively short but not tiny} & & \multirow{2}{\columnG}{Again, something not that long} \\
& \textbf{Controls: -} & - & - & & & \\
\multirow{2}{\columnA}{Row 6 \lipsum[6][1]} & \textbf{Cases: 3} & \lipsum[6][2] & \lipsum[6][3] & \multirow{2}{\columnE}{\lipsum[6][4]} & \multirow{2}{\columnF}{\lipsum[6][5-10]} & \multirow{2}{\columnG}{Learning skills} \\
& \textbf{Controls: 3} & Short text & A few words & & & \\
\end{longtable}
\end{small}
\end{landscape}
\end{document}

And this is the output: enter image description here

I tried the solutions from these and many other similar questions but I could not reproduce any working solutions and or I reached a point were I found compiling errors and could not get any further down the rabbit hole:

multirow with word wrapped columns in longtable

how to implement a table that has some cells with long texts?

Center multirow text and place it on each page in longtabu when page breaks

I'm new here, so any criticism to the way I wrote the post or the question itself is welcome, and I'll gladly re-edit it if required. Thanks in advance!

1 Answer 1

0
  • proposed solution mimic excel image (in this, complex structured dtable this has some sense)
  • instead of m{...} column type I suggest to rather use p{...} type
  • since in some cells are rather long , multi line text, it is sensible to use smaller font size, for example \small
  • for horizontal rules are used Xhline and Xcline rules defined in the makecell package
  • for more vertical space around cell contents is employed macro \makegapedcells defined in the makecell package
  • instead defined commands for column type and width is defined new column type which enable simple prescribed share of table width (in landscape orientation it is assumed that is equal to \textheight)
  • in empty cell is inserted rule which define its height (see MWE below)
  • for multirow cells are considered sinax \multirow[t]{<number of spanned lines>}{=}{<content>}
  • for cell \multicolumn{7}{|c|}{\textbf{Some category}} is not clear, if it is part of table headers with fixed case in each header or can be inserted anywhere in table with appropriate text (in MWE is considered former case)

MWE:

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{ragged2e}
\usepackage{array, longtable, 
            makecell,   % new
            multirow}
\renewcommand\theadfont{\footnotesize\bfseries}
\renewcommand\theadgape{}
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}p{\dimexpr#1\linewidth-2\tabcolsep-\arrayrulewidth}}


\usepackage{pdflscape}
\usepackage{lipsum} % For dummy text

\begin{document}
    \begin{landscape}
    \vfil
\setcellgapes{3pt}
\makegapedcells
\small\linespread{0.84}\selectfont  % <---
\setlength\tabcolsep{2pt}
\begin{longtable}[c]{| 
                     L{0.13}| L{0.09} | L{0.09}| L{0.08}|  L{0.12}| L{0.36}| L{0.13}|
                     }
\caption{A very big ugly table.}
\label{table1}                                                                      \\
    \Xhline{1.2pt}
\multirow{2.8}{=}{\thead{Study\\ design}}
    &   \multicolumn{3}{c|}{\thead{Participants}}
                &   \multirow{2.8}{=}{\thead{Device name\\ (Control options)}}
                    &   \multirow{2.8}{=}{\thead{User-environment\\ interaction}}
                        &   \multirow{2.8}{=}{\thead{Type of outcomes\\ assessed}}  \\
    \Xcline{2-4}{0.6pt}
    &   \thead{N} 
        &   \thead{Description*} 
            &   \thead{Diagnosis} 
                &   &   &                                                           \\ 
    \Xhline{0.6pt}
\multicolumn{7}{c}{\textbf{Some category}}                                          \\
    \Xhline{0.6pt}
\endfirsthead
\caption[]{A very big ugly table.}                                                  \\
    \Xhline{1.2pt}
\multirow{2.8}{=}{\thead{Study\\ design}}
    &   \multicolumn{3}{c|}{\thead{Participants}}
                &   \multirow{2.8}{=}{\thead{Device name\\ (Control options)}}
                    &   \multirow{2.8}{=}{\thead{User-environment\\ interaction}}
                        &   \multirow{2.8}{=}{\thead{Type of outcomes\\ assessed}}  \\
    \Xcline{2-4}{0.6pt}
    &   \thead{N}
        &   \thead{Description*}
            &   \thead{Diagnosis}
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multicolumn{7}{c}{\textbf{Some category}}                                          \\
    \Xhline{0.6pt}
\endhead
    \multicolumn{7}{r}{\footnotesize\itshape{Continue is on the next page}}         %?
\endfoot
    \Xhline{1.2pt}
\endlastfoot
% table body
\multirow[t]{5}{=}{Row 1 \lipsum[1][1]} 
    & \textbf{Cases: 4}
        &   \lipsum[1][2] 
            &   \lipsum[1][3] 
                &   \lipsum[1][4] 
                    & \lipsum[1][5] 
                        &   \multirow[t]{5}{=}{\lipsum[1][6]}                       \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: 4} 
        &   Not reported 
            &   Just three words 
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow[t]{8}{=}{Row 2 \lipsum[2][1-2]} 
    &   \textbf{Cases: 10}
        &   \lipsum[2][2]
            &   \lipsum[2][3]
                &   \multirow[t]{22}{=}{Quite long multi line text \lipsum[2][4]}
                    &   \multirow[t]{11}{=}{Super long text \lipsum*[2][1-15]}
                        &   \multirow[t]{7}{=}{\lipsum[2][6]}                       \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: -}
        &   \hfil ---
            &   \hfil \centering ---
                &   &   &                                                           \\                                                         
    \Xcline{1-4}{0.6pt}
    \Xcline{7-7}{0.6pt}
\multirow[t]{2}{=}{Row 3 \lipsum[3][1]}
    & \textbf{Cases: 1}
        & two words
            & some more
                &   &   &   \multirow[t]{2}{=}{Not that many words here either}     \\
    \Xcline{2-4}{0.6pt}
    & \textbf{Controls: -}
        & \hfil ---
            & \hfil ---
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow[t]{3}{=}{Row 4 \lipsum[4][1]}
    &   \textbf{Cases: 9}
        &   \lipsum[4][5]
            &   Good
                &   Couple of words
                    &   Another short phrase with few words
                        &   \multirow[t]{3}{=}{Yet another short
                                               text \& Only two words}               \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: 20}
        &   \lipsum[4][7]
            &   Two words again
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow[t]{8}{=}{Row 5 \lipsum[5][1-3]}
    &   \multirow{2}{=}{\textbf{Cases: 4}}
        &   \multirow{2}{=}{something short}
            &   \multirow{2}{=}{Bad}
                &   \multirow{2}{=}{Something relatively short but not tiny}
                    &   \rule[-5.5\baselineskip]{0pt}{-0pt}   % <--- define empty cell height
                        & \multirow{2}{=}{Again, something not that long}           \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: -}
        & - & - &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow[t]{7}{=}{Row 6 \lipsum[6][1]}
    &   \textbf{Cases: 3}
        &   \lipsum[6][2]
            &   \lipsum[6][3]
                &   \multirow[t]{7}{=}{\lipsum[6][4]}
                    &   \multirow[t]{7}{=}{\lipsum*[6][5-10]}
                        & \multirow[t]{7}{=}{Learning skills}                          \\
     \Xcline{2-4}{0.6pt}
    &   {\textbf{Controls: 3}}
        &   {Short text }
            &   {A few words }
                &   &   &                                                           \\
\end{longtable}
    \end{landscape}
\end{document}

enter image description here

For final form of table you need to compile MWE due to features of longtable at least twice.

Addendum:

  • Version with use of the m{...} columns type. It still use makgapedcells macro, but is added Ulrike Fisher ( patch ), which enable it works at m columns type.

  • Due to reduced interline space this solution requires considerable manual tuning:

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{ragged2e}
\usepackage{array, longtable,
            makecell,   % new
            multirow}
\renewcommand\theadfont{\footnotesize\bfseries}
\makeatletter
\renewcommand\mcell@classz{\@classx
   \@tempcnta \count@
   \prepnext@tok
   \@addtopreamble{%\mcell@mstyle
      \ifcase\@chnum
         \hfil
         \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
         \hskip1sp
         \mcell@agape{\d@llarbegin\insert@column\d@llarend}\hfil \or
         \hfil\hskip1sp
         \mcell@agape{\d@llarbegin \insert@column\d@llarend}\or
         \mcell@agape{$\vcenter
         \@startpbox{\@nextchar}\insert@column\@endpbox$}\or
         \mcell@agape{\vtop
         \@startpbox{\@nextchar}\insert@column\@endpbox}\or
         \mcell@agape{\vbox
        \@startpbox{\@nextchar}\insert@column\@endpbox}%
      \fi
      \global\let\mcell@left\relax\global\let\mcell@right\relax
    }\prepnext@tok}
\makeatother
\renewcommand\theadgape{}

\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}m{\dimexpr#1\linewidth-2\tabcolsep-\arrayrulewidth}}


\usepackage{pdflscape}
\usepackage{lipsum} % For dummy text

\begin{document}
    \begin{landscape}
    \vfil
\setcellgapes{3pt}
\makegapedcells
\small\linespread{0.84}\selectfont  % <---
\setlength\tabcolsep{2pt}
\begin{longtable}[c]{|
                     L{0.13}| L{0.09} | L{0.09}| L{0.08}|  L{0.12}| L{0.36}| L{0.13}|
                     }
\caption{A very big ugly table.}
\label{table1}                                                                      \\
    \Xhline{1.2pt}
\multirow{2.8}{=}{\thead{Study\\ design}}
    &   \multicolumn{3}{c|}{\thead{Participants}}
                &   \multirow{2.8}{=}{\thead{Device name\\ (Control options)}}
                    &   \multirow{2.8}{=}{\thead{User-environment\\ interaction}}
                        &   \multirow{2.8}{=}{\thead{Type of outcomes\\ assessed}}  \\
    \Xcline{2-4}{0.6pt}
    &   \thead{N}
        &   \thead{Description*}
            &   \thead{Diagnosis}
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multicolumn{7}{c}{\textbf{Some category}}                                          \\
    \Xhline{0.6pt}
\endfirsthead
\caption[]{A very big ugly table.}                                                  \\
    \Xhline{1.2pt}
\multirow{2.8}{=}{\thead{Study\\ design}}
    &   \multicolumn{3}{c|}{\thead{Participants}}
                &   \multirow{2.8}{=}{\thead{Device name\\ (Control options)}}
                    &   \multirow{2.8}{=}{\thead{User-environment\\ interaction}}
                        &   \multirow{2.8}{=}{\thead{Type of outcomes\\ assessed}}  \\
    \Xcline{2-4}{0.6pt}
    &   \thead{N}
        &   \thead{Description*}
            &   \thead{Diagnosis}
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multicolumn{7}{c}{\textbf{Some category}}                                          \\
    \Xhline{0.6pt}
\endhead
    \multicolumn{7}{r}{\footnotesize\itshape{Continue is on the next page}}         %?
\endfoot
    \Xhline{1.2pt}
\endlastfoot
% table body
\multirow{4}{=}{Row 1 \lipsum[1][1]}
    & \textbf{Cases: 4}
        &   \lipsum[1][2]
            &   \lipsum[1][3]
                &   \multirow{4}{=}[1ex]{\lipsum[1][4]}
                    & \multirow{4}{=}{\lipsum[1][5]}
                        &   \multirow{-2}{=}{\lipsum[1][6]}                       \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: 4}
        &   Not reported
            &   Just three words
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow{-2}{=}[2ex]{Row 2 \lipsum[2][1-2]}
    &   \textbf{Cases: 10}
        &   \lipsum[2][2]
            &   \lipsum[2][3]
                &   \multirow{-2.4}{=}{Quite long multi line text \lipsum[2][4]}
                    &   \multirow{-3.3}{=}{Super long text \lipsum*[2][1-15]}
                        &   \multirow{3.3}{=}{\lipsum[2][6]}                       \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: -}
        &   \hfil ---
            &   \hfil \centering ---
                &   &   &                                                           \\
    \Xcline{1-4}{0.6pt}
    \Xcline{7-7}{0.6pt}
\multirow{2}{=}{Row 3 \lipsum[3][1]}
    & \textbf{Cases: 1}
        & two words
            & some more
                &   &   &   \multirow{2}{=}{Not that many words here either}     \\
    \Xcline{2-4}{0.6pt}
    & \textbf{Controls: -}
        & \hfil ---
            & \hfil ---
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow{3}{=}{Row 4 \lipsum[4][1]}
    &   \textbf{Cases: 9}
        &   \lipsum[4][5]
            &   Good
                &   \multirow{4}{=}{Couple of words}
                    &   \multirow{4}{=}{Another short phrase with few words}
                        &   \multirow{4}{=}{Yet another short
                                               text \& Only two words}               \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: 20}
        &   \lipsum[4][7]
            &   Two words again
                &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow{-3.4}{=}{Row 5 \lipsum[5][1-3]}
    &   \multirow{2}{=}{\textbf{Cases: 4}}
        &   \multirow{2}{=}{something short}
            &   \multirow{2}{=}{Bad}
                &   \multirow{2}{=}{Something relatively short but not tiny}
                    &   \rule[-5.5\baselineskip]{0pt}{-0pt}   % <--- define empty cell height
                        & \multirow{3}{=}{Again, something not that long}           \\
    \Xcline{2-4}{0.6pt}
    &   \textbf{Controls: -}
        & - & - &   &   &                                                           \\
    \Xhline{0.6pt}
\multirow{4}{=}{Row 6 \lipsum[6][1]}
    &   \textbf{Cases: 3}
        &   \lipsum[6][2]
            &   \lipsum[6][3]
                &   \multirow{4}{=}{\lipsum[6][4]}
                    &   \multirow{-2}{=}{\lipsum*[6][5-10]}
                        & \multirow{3}{=}{Learning skills}                          \\
     \Xcline{2-4}{0.6pt}
    &   {\textbf{Controls: 3}}
        &   {Short text }
            &   {A few words }
                &   &   &                                                           \\
\end{longtable}
    \end{landscape}
\end{document}

enter image description here

5
  • This is a huge help, thank you, but I wonder how can the text within each cell be vertically aligned if not using m{...} column type? I don't mean horizontally centered as in the original question, but rather, that the empty space be distributed above and below the text of a cell.
    – XabiGarde
    Dec 11, 2020 at 18:34
  • @XabiGarde, if you prefer vertical centering of text, than use m column type, but in this case makegapedcells will not work. If you like to have more vertical space, than you should see cellspace packages. Anyway, I wouldn't use m columns. As is table shown now, is more clear and aesthetic, of course to my opinion.
    – Zarko
    Dec 11, 2020 at 18:48
  • Thank for the hints. I fully agree that your approach looks more clear and aesthetic. However, some scientific journals comply with style-guidelines that tend to avoid vertical and horizontal lines as much as possible, such as the APA Style (American Psycological Association). I'll give some thought to consider working around it, and leave it as is, or finally resorting to the excel-to-PDF sub-optimal approach. In any case, your answer best fit my question, so I'll mark it as accepted. Thank you!
    – XabiGarde
    Dec 11, 2020 at 19:19
  • @XabiGarde, see new example oin addendum. It use m column type.
    – Zarko
    Dec 11, 2020 at 19:59
  • This was a huge help! I have adapted your new addendum to my real table and after some manual adjustments, it looks pretty good. Thank you!
    – XabiGarde
    Dec 15, 2020 at 7:50

You must log in to answer this question.

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