0

I'm making a table over different pages with the longtable package:

\begin{landscape}
\begin{longtable}{|c|c|c|c|c|c|c|}

\caption{Table noMCI.} \label{tab:long} \\
\hline 
\multicolumn{1}{|c|}{\textbf{Study}} & 
\multicolumn{1}{ c|}{\textbf{Participants}} & 
\multicolumn{1}{ c|}{\textbf{Inclusion Criteria}} & 
\multicolumn{1}{ c|}{\textbf{Study Design}} & 
\multicolumn{1}{ c|}{\textbf{Intervention}} & 
\multicolumn{1}{ c|}{\textbf{Primary Outcome}} & 
\multicolumn{1}{ c|}{\textbf{Cognitive Function}}\\ 
\hline 
\endfirsthead

\hline 
\multicolumn{1}{|c|}{\textbf{Study}} & 
\multicolumn{1}{ c|}{\textbf{Participants}} & 
\multicolumn{1}{ c|}{\textbf{Inclusion Criteria}} & 
\multicolumn{1}{ c|}{\textbf{Study Design}} & 
\multicolumn{1}{ c|}{\textbf{Intervention}} & 
\multicolumn{1}{ c|}{\textbf{Primary Outcome}} & 
\multicolumn{1}{ c|}{\textbf{Cognitive Function}}\\  
\hline 
\endhead

\hline
\endfoot

\hline
\endlastfoot

One & abcdef ghjijklmn & 123.456778 & x & x & x & x \\

\end{longtable}
\end{landscape}

I want to split columns 'Participants', 'Intervention' and 'Cognitive Function' in 2, 5 and 6 subcolumns respectively. But when i change the code in this way:

\hline 
\multicolumn{1}{|c|}{\textbf{Study}} & 
\multicolumn{2}{ c|}{\textbf{Participants}} & 
\multicolumn{1}{ c|}{\textbf{Inclusion Criteria}} & 
\multicolumn{1}{ c|}{\textbf{Study Design}} & 
\multicolumn{5}{ c|}{\textbf{Intervention}} & 
\multicolumn{1}{ c|}{\textbf{Primary Outcome}} & 
\multicolumn{6}{ c|}{\textbf{Cognitive Function}}\\

I get this error "Extra alignment tab has been changed to \cr" and the output table results in:

enter image description here.

How to fix it?

EDIT: how to set vertical lines between the columns?

1
  • 1
    Welcome to TeX.SE. LaTeX has no real concept of "subcolumns" -- the vertically-oriented structural elements of a tabular, longtable, and similar environment are just columns, not columns and subcolumns. You therefore need to start by defining the longtable environment as having 17, not 7, columns. E.g., instead of \begin{longtable}{|*{7}{c|}}, you would write \begin{longtable}{|*{17}{c|}}. Better still, just get rid of all vertical rules -- trust me, they're not needed -- and simply write \begin{longtable}{ *{17}{c} }.
    – Mico
    Commented Sep 20, 2023 at 17:35

1 Answer 1

1

First, you have to specify a total number of columns including those which forms "subcolumns" and then use \multicolumn to merge columns in particular rows, for instance headings.

The table will contain a large number of columns. I would suggest to shorten heading columns or at least add line breaks; you will find makecell package very useful with following macros: \thead and \makecell because they let you add line breaks \\ in the argument. The other advantage is to add formatting in one place, e.g. heading columns in bold as shown in the example (see the line of \theadfont).

You may also consider adding caption package in order to format captions. In the example I changed a distance between table and the caption.

IMO you should remove vertical bars. Particularly in that table you would have more flexibility to decrease space between column and add more room for the content. This can be done by changing \tabcolsep, which affects the whole table (commented line in the example inside longtable env.). Alternatively, distance between columns can be set locally in the preamble of the table: just insert @{<macro>} between columns; the empty argument @{} completely remove spacing, whereas <macro> should be any command that inserts extra space, e.g. \hspace{3pt}, \quad, \; etc.

See the example below

\documentclass{article}
\usepackage{pdflscape}
\usepackage{longtable}
\usepackage{caption}      % To format captions
\usepackage{makecell}     % For \thead and \makecell
\usepackage{booktabs}     % For \toprule, \midrule and \bottomrule
\usepackage{kantlipsum}   % For a dummy text, only for example

\captionsetup[table]{position=top,skip=3pt}

\renewcommand*\theadfont{\small\bfseries}
\renewcommand*\cellset{\small}
\renewcommand*\theadgape{\Gape[0pt]}


\begin{document}
\kant[1]

\begin{landscape}
  \small
  % \setlength\tabcolsep{3pt}
  \begin{longtable}{@{} *{17}{c} @{}}
    \caption{Table noMCI.}\label{tab:long} \\
    \toprule
    \thead{Study}
    & \multicolumn{2}{c}{\thead{Parti-\\cipants}}
    & \thead{Inclusion\\Criteria}
    & \thead{Study\\Design}
    & \multicolumn{5}{c}{\thead{Intervention}}
    & \thead{Primary\\Outcome}
    & \multicolumn{6}{c}{\thead{Cognitive Function}} \\
    \midrule\endfirsthead
    \toprule
    \thead{Study}
    & \multicolumn{2}{c}{\thead{Parti-\\cipants}}
    & \thead{Inclusion\\Criteria}
    & \thead{Study\\Design}
    & \multicolumn{5}{c}{\thead{Intervention}}
    & \thead{Primary\\Outcome}
    & \multicolumn{6}{c}{\thead{Cognitive Function}} \\
    \midrule\endhead
    \bottomrule\endfoot
    \bottomrule\endlastfoot
    One
    & A1 & A2
    & 123.4
    & B
    & C1 & C2 & C3 & C4 & C5
    & E
    & F1 & F2 & F3 & F4 & F5 & F6 \\
    Two
    & G1 & G2
    & 56.78
    & H
    & I1 & I2 & I3 & I4 & I5
    & J
    & K1 & K2 & K3 & K4 & K5 & K6 \\
    Three
    & A1 & A2
    & 90.12
    & B
    & C1 & C2 & C3 & C4 & C5
    & E
    & F1 & F2 & F3 & F4 & F5 & F6 \\
    Four
    & G1 & G2
    & 3.456
    & H
    & I1 & I2 & I3 & I4 & I5
    & J
    & K1 & K2 & K3 & K4 & K5 & K6 \\
  \end{longtable}
\end{landscape}

\kant[2]
\end{document}

enter image description here


EDIT

You really need to watch those headings. You are making them too long!

If you want to add extra heading, just append extra row. In the previous example I have used c-column types but since your table contains texts, I would change columns to p{<column-width>}. It forms paragraphs in cells. One consequence is texts receive natural line breaks at column boundaries. Otherwise, long texts are typeset in one line. p{} keeps paragraph justification with texts starting from the left. For a proper left alignment, an extra option is required: >{\raggedright\arraybackslash}, which you insert in front of the column definition

... >{\raggedright\arraybackslash}p{1.5cm} ...

Alternatively, \centering or \raggedleft can be used instead. If you use extra alignment, \arraybackslash is mandatory in the last column definition because these macros redefine \\. It's worth noting, the package ragged2e provides much improved counterparts, s.a. \Centering, \RaggedRight or \RaggedLeft which do not require \arraybackslash.

Another point, your table is really large and consists of 17 columns! It's very easy to make the table too wide. For this reason, I'd suggest an alternative package xltabular. The package provides X-column type. X is very convenient addition because it splits remaining space among all such columns. Then, all columns defined as X gain the same width. By default it also creates paragraphs with standard justification. You could insert the above expression to get a different alignment. There is also a way to do so globally if you are interested (see page 3 of the documentation).

One arrangement is to set all columns containing numbers as X and use fixed widths for other columns containing text because you may need different widths for diferent texts. Alternatively, you could define regular r columns for numbers and X for texts. Just make sure all columns containing texts form paragraphs: use either X or p{}. However, you will still need to watch it out to void making the table too wide.

One more suggestion, consider using geometry package to decrease margins. This will add more space for the table. You can use \newgeometry{} before the table and \restoregeometry afterwards to change page layout just for the table. Keep in mind both macros insert pages breaks. You can use them right before and after \begin{landscape}...\end{landscape}.

Lastly, if you are going to shorten columns, you might need to append annotations. I put two example of those (see the macro \tnote). The \rlap{} is a convenient macro as any content inside does not take extra space. It's not affecting alignments and is for keeping presentation tidy.

\documentclass{article}
\usepackage[pass]{geometry}   % [pass] 
\usepackage{pdflscape}
\usepackage{xltabular}
\usepackage{ragged2e,microtype}
\usepackage{caption}          % To format captions
\usepackage{makecell}         % For \thead and \makecell
\usepackage{booktabs}         % For \toprule, \midrule and \bottomrule
\usepackage[nopar]{kantlipsum}   % For a dummy text, only for example

\captionsetup[table]{position=top,skip=3pt}

\renewcommand*\theadfont{\small\bfseries}
\renewcommand*\cellset{\small}
\renewcommand*\theadgape{\Gape[0pt]}
\renewcommand*{\theadalign}{ct}

\newcolumntype{L}[1]{>{\RaggedRight}p{#1}}
\newcolumntype{C}[1]{>{\Centering}p{#1}}
\newcolumntype{R}[1]{>{\RaggedLeft}p{#1}}
\newcolumntype{Y}{>{\Centering}X}

\newcommand\tnote[1]{\rlap{\,\textsuperscript{#1}}}   % For annotations


\begin{document}
\kant[1]

\newgeometry{margin=1cm}
\begin{landscape}
  \small\sloppy
  \setlength\tabcolsep{3pt}
  \setlength\cmidrulekern{\tabcolsep}
  \begin{xltabular}{\linewidth}{@{}
    p{1.5cm}
    *2{Y}
    L{1.75cm}
    L{1.75cm}
    L{1.75cm} *4{Y}
    C{1.75cm}
    *6{Y}
    @{}}
    \caption{Table noMCI.}\label{tab:long} \\
    \toprule
    % Heading - first row
    \thead[l]{Study}
    & \multicolumn{2}{c}{\thead{Parti-\\cipants}}
    & \thead{Inclusion\\Criteria}
    & \thead{Study\\Design}
    & \multicolumn{5}{c}{\thead{Intervention}}
    & \thead{Primary\\Outcome}
    & \multicolumn{6}{c}{\thead{Cognitive Function}} \\
    \cmidrule(lr){2-3}\cmidrule(lr){6-10}\cmidrule(lr){12-17}
    % Heading - second row
    & \thead{N} & \thead{Age}
    & &
    & \thead[l]{Training} & \thead{Ses.\\(n)} & \thead{Len.\tnote{2}\\(min)} & \thead{Freq.\\(n/w)\tnote{1}} & \thead{Total\\(n)}
    & & \thead{WM} & \thead{PS} & \thead{Att.} & \thead{EF} & \thead{Vis.} & \thead{WM} \\
    \midrule\endfirsthead
    \toprule
    % Heading - first row
    \thead[l]{Study}
    & \multicolumn{2}{c}{\thead{Parti-\\cipants}}
    & \thead{Inclusion\\Criteria}
    & \thead{Study\\Design}
    & \multicolumn{5}{c}{\thead{Intervention}}
    & \thead{Primary\\Outcome}
    & \multicolumn{6}{c}{\thead{Cognitive Function}} \\
    \cmidrule(lr){2-3}\cmidrule(lr){6-10}\cmidrule(lr){12-17}
    % Heading - second row
    & \thead{N} & \thead{Age}
    & &
    & \thead[l]{Training} & \thead{Ses.\\(n)} & \thead{Len.\\(min)} & \thead{Freq.\\(n/w)\tnote{1}} & \thead{Total\\(n)}
    & & \thead{WM} & \thead{PS} & \thead{Att.} & \thead{EF} & \thead{Vis.} & \thead{WM} \\
    \midrule\endhead
    \bottomrule\endfoot
    \bottomrule\endlastfoot
    % Table body
    One
    & 123.4 & 56.78
    & \kant[2][2]
    & Sample sample sample
    & \kant[2][1] & 40 & 60 & 5 & 40
    & E
    & F1 & F2 & F3 & F4 & F5 & F6 \\
  \end{xltabular}
  \vspace{-0.75\baselineskip}%  
  \textsuperscript{1} Measured in a number of something per week\par
  \textsuperscript{2} Length of something etc.
\end{landscape}
\restoregeometry

\kant[2]
\end{document}

enter image description here


EDIT 2

In order to add vertical lines, just add | in column definitions. IMO your table contains lots of columns and will not look great.

Booktabs macros no longer work as every one of each adds a gap when rules and bars cross. If you want, you can simply use \hline or use a trick I suggested (see code).

You probably need to stretch that table--see the line of \arraystretch and change its factor to your preference.

I realised page numbers on pages with the table are partially cut due to small margins. I corrected that setting a custom footskip option.

\documentclass{article}
\usepackage[pass]{geometry}   % [pass] 
\usepackage{pdflscape}
\usepackage{xltabular}
\usepackage{ragged2e,microtype}
\usepackage{caption}          % To format captions
\usepackage{makecell}         % For \thead and \makecell
\usepackage{booktabs}         % For \toprule, \midrule and \bottomrule
\usepackage[nopar]{kantlipsum}   % For a dummy text, only for example

\captionsetup[table]{position=top,skip=3pt}

\renewcommand*\theadfont{\small\bfseries}
\renewcommand*\cellset{\small}
\renewcommand*\theadgape{\Gape[0pt]}
\renewcommand*{\theadalign}{ct}

\newcolumntype{L}[1]{>{\RaggedRight}p{#1}}
\newcolumntype{C}[1]{>{\Centering}p{#1}}
\newcolumntype{R}[1]{>{\RaggedLeft}p{#1}}
\newcolumntype{Y}{>{\Centering}X}

\NewDocumentCommand\tnote{sm}{%
  \rlap{\IfBooleanF{#1}{\,}\textsuperscript{#2}}}   % For annotations


\begin{document}
\kant[1]

\newgeometry{margin=1cm,footskip=24pt}
\begin{landscape}
  \renewcommand*\arraystretch{1.25}
  \small\sloppy
  \setlength\parindent{0pt}
  \setlength\tabcolsep{3pt}
  \setlength\cmidrulekern{\tabcolsep}
  \begin{xltabular}{\linewidth}{|
    p{1.5cm} |
    *2{Y|}
    L{1.75cm} |
    L{1.75cm} |
    L{1.75cm} | *4{Y|}
    C{1.75cm} |
    *6{Y|}
    @{}}
    \caption{Table noMCI.}\label{tab:long} \\
    %\toprule
    \noalign{\hrule height \heavyrulewidth}
    %%% Heading - first row
    \thead[l]{Study}
    & \multicolumn{2}{c|}{\thead{Parti-\\cipants}}
    & \thead{Inclusion\\Criteria}
    & \thead{Study\\Design}
    & \multicolumn{5}{c|}{\thead{Intervention}}
    & \thead{Primary\\Outcome}
    & \multicolumn{6}{c|}{\thead{Cognitive Function}} \\
    % \cmidrule(lr){2-3}\cmidrule(lr){6-10}\cmidrule(lr){12-17}
    \cline{2-3}\cline{6-10}\cline{12-17}
    % Heading - second row
    & \thead{N} & \thead{Age}
    & &
    & \thead[l]{Training} & \thead{Ses.\\(n)} & \thead{Len.\tnote{2}\\(min)} & \thead{Freq.\\(n/w)\tnote*{1}} & \thead{Total\\(n)}
    & & \thead{WM} & \thead{PS} & \thead{Att.} & \thead{EF} & \thead{Vis.} & \thead{WM} \\
    % \midrule\endfirsthead
    \noalign{\hrule height \lightrulewidth}\endfirsthead
    %%% Heading - consequtive rows
    % \toprule
    \noalign{\hrule height \heavyrulewidth}
    \thead[l]{Study}
    & \multicolumn{2}{c|}{\thead{Parti-\\cipants}}
    & \thead{Inclusion\\Criteria}
    & \thead{Study\\Design}
    & \multicolumn{5}{c|}{\thead{Intervention}}
    & \thead{Primary\\Outcome}
    & \multicolumn{6}{c|}{\thead{Cognitive Function}} \\
    % \cmidrule(lr){2-3}\cmidrule(lr){6-10}\cmidrule(lr){12-17}
    \cline{2-3}\cline{6-10}\cline{12-17}
    % Heading - second row
    & \thead{N} & \thead{Age}
    & &
    & \thead[l]{Training} & \thead{Ses.\\(n)} & \thead{Len.\\(min)} & \thead{Freq.\\(n/w)\tnote*{1}} & \thead{Total\\(n)}
    & & \thead{WM} & \thead{PS} & \thead{Att.} & \thead{EF} & \thead{Vis.} & \thead{WM} \\
    % \midrule\endhead
    % \bottomrule\endfoot
    % \bottomrule\endlastfoot
    \noalign{\hrule height \lightrulewidth}\endhead
    \noalign{\hrule height \heavyrulewidth}\endfoot
    \noalign{\hrule height \heavyrulewidth}\endlastfoot
    % Table body
    One
    & 123.4 & 56.78
    & \kant[2][2]
    & Sample sample sample
    & \kant[2][1] & 40 & 60 & 5 & 40
    & E
    & F1 & F2 & F3 & F4 & F5 & F6 \\
  \end{xltabular}
  \vspace{-\baselineskip}%  
  \textsuperscript{1} Measured in a number of something per week\par
  \textsuperscript{2} Length of something etc.
\end{landscape}
\restoregeometry

\kant[2]
\end{document}

enter image description here

5
  • Nice example, thank you very much. One more question, how to add titles to those "subcolumns"? For instance, in participants I want to name the subcolumns N and Age respectively. Basically, I have to reproduce an excel table also containing a lot of text. I posted it as an answer to this question in order to see the image. Commented Sep 21, 2023 at 9:24
  • If you think you need to add more information, just edit your question and label the appended text to inform that you did so, for instance add EDIT or More details as a heading.
    – Celdor
    Commented Sep 21, 2023 at 10:50
  • I have added more suggestions for you with another example. See my updated answer.
    – Celdor
    Commented Sep 21, 2023 at 11:48
  • How to set vertical lines between columns in this last example? Thank you so much. Commented Oct 23, 2023 at 10:43
  • This answer is getting too long. I'll edit it one more time because it needs correction due to page numbers but consider asking another question next time you need more help.
    – Celdor
    Commented Oct 23, 2023 at 12:46

You must log in to answer this question.

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