4

When I type more than 3 lines of text in the table, the table collapses, could anyone help?

Here is the code:

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} % Required for inserting images
\usepackage{setspace}% for\doublespacing
\usepackage{indentfirst}% for indent the first line
\usepackage{longtable}
\usepackage{textalpha}
\usepackage{ragged2e}
 \usepackage[T1]{fontenc} % <-- new
\usepackage{xltabular} 
\usepackage{amsmath}
\usepackage{caption}

\begin{document}

\newcolumntype{L}{>{\RaggedRight}X} % suspend full justification % special new column type for header row: \newcolumntype{C}{>{\centering\arraybackslash}% p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth\relax}}

\begin{xltabular}{\textwidth}{ | l | L | c | } % <-- new

%% headers and footers \caption{DUSP genes in normal and pathological brain function\label{long4}}\ \hline \multicolumn{3}{ | C | }{DUSP genes in normal and pathological brain function}\ \hline Isoforms & Function in Normal Brain & Function in Pathological Brain \ \hline \endfirsthead

\hline \multicolumn{3}{|c|}{Continuation of Table \ref{long4}}\ \hline Isoforms & Function in Normal Brain& Function in Pathological Brain \ \hline \endhead

\hline \endfoot

\hline \multicolumn{3}{| c |}{End of Table}\ \hline \hline \endlastfoot

SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \

         \end{xltabular}

% This is footnote \begin{minipage}{10cm} \small AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4. \end{minipage}

\end{document}

This is ideal form.

enter image description here

The code gives collapsed table:

enter image description here

Annie
  • 93

4 Answers4

6

You need to allow automatic line breaking in both columns 2 and 3. I therefore suggest you replace

\begin{xltabular}{\textwidth}{ | l | L | c | }

with

\begin{xltabular}{\textwidth}{ | l | L | L | }

If, for some reason, you want the contents of the cells in column 3 to be centered instead of left-aligned, I suggest you execute

\newcolumntype{Z}{>{\Centering}X} 

in the preamble and use

\begin{xltabular}{\textwidth}{ | l | L | Z | }

instead of \begin{xltabular}{\textwidth}{ | l | L | L | }


enter image description here

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} % Required for inserting images
\usepackage{setspace}% for\doublespacing
\usepackage{indentfirst}% for indent the first line
\usepackage{ragged2e}
\usepackage[T1]{fontenc}
\usepackage{xltabular} 
\usepackage{amsmath}
\usepackage{caption}

\newcolumntype{L}{>{\RaggedRight}X} % suspend full justification % special new column type for header row: \newcolumntype{C}{>{\centering\arraybackslash}% p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth\relax}}

\begin{document}

\begin{xltabular}{\textwidth}{ | l | L | L | } % <-- new

%% headers and footers \caption{DUSP genes in normal and pathological brain function\label{long4}}\ \hline \multicolumn{3}{ | C | }{DUSP genes in normal and pathological brain function}\ \hline Isoforms & Function in Normal Brain & Function in Pathological Brain \ \hline \endfirsthead

\hline \multicolumn{3}{|c|}{Continuation of Table \ref{long4}}\ \hline Isoforms & Function in Normal Brain& Function in Pathological Brain \ \hline \endhead

\hline \endfoot

\hline \multicolumn{3}{| C |}{End of Table}\ \hline \endlastfoot

SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \

\end{xltabular}

\noindent \begin{minipage}{\textwidth} \small AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4. \end{minipage}

\end{document}

Mico
  • 506,678
1

Slightly expanding on Mico's answer. The reason is two columns in your screenshot are not of the same width. It's possible to achieve this effect with X-type columns if you specify explicit proportions. The format is

\newcolumntype{Y}{>{\hsize=<factor>\hsize\linewidth=\hsize}X}
\newcolumntype{Z}{>{\hsize=<2-factor>\hsize\linewidth=\hsize}X}

Note that factors should add up to a number of columns of type X, which is 2 in the example. It's explained in documentation section 4.3. In the example below, I choose relative widths of 0.8 and 1.2 (note that, as required, 0.8+1.2=2), which implies that the usable width of the second X-type column is 50% larger than that of the first.

I would make annotation a part of the table to avoid unexpected separation on page breaks. I would also stretch the table a bit to add more space to rows.

Here's the final code

\documentclass{report}
\usepackage{graphicx} % Required for inserting images
\usepackage{setspace}% for\doublespacing
\usepackage{indentfirst}% for indent the first line
\usepackage{textalpha}
\usepackage{ragged2e}
\usepackage[T1]{fontenc} % <-- new
\usepackage{xltabular} 
\usepackage{amsmath}
\usepackage{caption}

\newlength\tabwidth \newcolumntype{Y}{>{\RaggedRight\hsize=.8\hsize\linewidth=\hsize}X} \newcolumntype{Z}{>{\RaggedRight\hsize=1.2\hsize\linewidth=\hsize}X} \newcolumntype{C}{>{\centering\arraybackslash}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}}

\begin{document} \begingroup \renewcommand{\arraystretch}{1.25} \begin{xltabular}{\textwidth}{|l|Y|Z|} % <-- new %% headers and footers \caption{DUSP genes in normal and pathological brain function\label{long4}}\ \hline \multicolumn{3}{|C|}{DUSP genes in normal and pathological brain function}\ \hline Isoforms & Function in Normal Brain & Function in Pathological Brain \ \hline \endfirsthead \hline \multicolumn{3}{|C|}{Continuation of Table \ref{long4}}\ \hline Isoforms & Function in Normal Brain & Function in Pathological Brain \ \hline \endhead \hline \endfoot \hline \multicolumn{3}{|C|}{End of Table}\ \hline \hline \multicolumn{3}{@{}p{\linewidth}@{}}{% \rule{0pt}{0.7\normalbaselineskip}% \footnotesize AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.} \endlastfoot SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ \end{xltabular} \endgroup \end{document}

enter image description here

Mico
  • 506,678
Celdor
  • 9,058
  • 1
    Are you sure about \hsize=<factor>\hsize and \hsize=<1-factor>\hsize? Your own practical example uses 0.8 and 1.2, not 0.8 and 0.2. – Mico Aug 03 '23 at 12:34
  • 1
    @Mico The numbers were incorrect in the answer. It should be 0.8 and 1.2. Thanks for the comment. – Celdor Aug 03 '23 at 12:52
  • 1
    +1. Thanks. I've taken the liberty of editing your answer to provide some intuition for what relative widths of 0.8 and 1.2 may mean in practice. (I know that you fully well know this; however, newcomers to the world of tabularx columns may not, right?) Feel free to revert. – Mico Aug 03 '23 at 14:21
0

Your table you can also write by use longtblr of tabularray package. Code is shorter, but compilation time is a bit longer:

\documentclass{report}
\usepackage[T1]{fontenc}

%\usepackage{graphicx} % Required for inserting images %\usepackage{indentfirst}% for indent the first line \usepackage{ragged2e} \usepackage{tabularray} \UseTblrLibrary{amsmath, booktabs} \SetTblrStyle{contfoot}{font=\footnotesize\itshape} \SetTblrStyle{note}{font=\small}

\begin{document} \begin{longtblr}[ caption = {DUSP genes in normal and pathological brain function}, label = {long4}, note{} = {AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.}, ]{vlines, colspec = { l *{2}{X[cmd=\RaggedRight]} }, cell{1,Z}{1} = {c=3}{c}, row{3-Y} = {rowsep=3pt}, row{2} = {c}, rowhead = 2 } % column headers \toprule DUSP genes in normal and pathological brain function & & \ \midrule Isoforms & Function in Normal Brain & Function in Pathological Brain \ \midrule % table body SSH1 & long text long text long text long text long text long text
& long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \ SSH1 & long text long text long text long text long text long text
& long textlong text long text long text long text long text long text long text \ % table body SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text long text long text long text long text \ % table body SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text long text long text long text long text \ % table body SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text long text long text long text long text \ % table body SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text long text long text long text long text \ % table body SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text long text long text long text long text \ \midrule End of Table & & \ \bottomrule \end{longtblr} \end{document}

enter image description here

enter image description here

enter image description here

As you can see, in above MWE in comparison to your MWE are done the following changes:

  • for horizontal table lines are used rules defined in the booktabs package.
  • for text aligned in cells is used ragged2e package

In the case, that you like to have different width od the second an third column, you can get this pa scaling X specifier, for example:

colspec      = { l X[cmd=\RaggedRight]
                   X[1.3, cmd=\RaggedRight]},

where the third column is 30% wider than second.

Zarko
  • 296,517
0

Another answer based on the tabularray package.

\documentclass{report}
\usepackage{tabularray}
% Header for pages other than the 1st one
\DefTblrTemplate{conthead-text}{MyContTxt}{
    \begin{tblr}{
        colspec={lX[l]X[l]},
        vlines,
    }
        \hline
        \SetCell[c=3]{c}Continuation of Table &amp; &amp; \\
        \hline
        Isoforms &amp; Function in Normal Brain &amp; Function in Pathological Brain\\
    \end{tblr}
}
\DefTblrTemplate{capcont}{MyTmp}{%
    \UseTblrTemplate{conthead-text}{MyContTxt}
}

% Remove the &quot;Continued on next page&quot; text from footer
\DefTblrTemplate{firstfoot, middlefoot}{MyFoot}{%
    %
}

\NewTblrTheme{MyTheme}{
    \SetTblrTemplate{capcont}{MyTmp}%
    \SetTblrTemplate{firstfoot, middlefoot}{MyFoot}%
}

\begin{document}

\begin{longtblr}[ theme = MyTheme, caption = {DUSP genes in normal and pathological brain function}, label = {long4}, headsep= -1pt, note{} = {\small AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.}, ]{ colspec = {lX[l]X[l]}, width = \textwidth, vlines, } \hline \SetCell[c=3]{c}DUSP genes in normal and pathological brain function & &\ \hline Isoforms & Function in Normal Brain & Function in Pathological Brain \ \hline SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ SSH1 & long text long text long text long text long text long text & long textlong text long text long text \ SSH2 & long text long text long text long text & long text long text long text long text \
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \ \hline \SetCell[c=3]{c} End of Table & &\ \hline \end{longtblr}

\end{document}

1st page

2nd page

3rd page

Courtesy: This answer was followed to add a different header in the pages other than the first one.

Imran
  • 3,096