latex3 / latex2e

The LaTeX2e kernel
https://www.latex-project.org/
LaTeX Project Public License v1.3c
1.87k stars 262 forks source link

Rendering of tabularx with multicolumn break after a certain number of table lines with multicolumn. #630

Open weetmuts opened 3 years ago

weetmuts commented 3 years ago

Brief outline of the bug

When using tabularx and having many lines with \multicolumn eventually the rendering breaks down and the cell widths become tiny.

Minimal example showing the bug

\RequirePackage{latexbug}
\documentclass[10pt,a4paper]{article}
\usepackage{tabularx}

\overfullrule=10pt

\begin{document}
\raggedright
\tiny

\newcommand{\foo}{%                                                                                                                                                                                                                                                                                                                                                            
a & z z z z & w w w w \\ \hline
b & \multicolumn{2}{X|}{x x x x} \\ \hline
}

% This table always works.
\begin{tabularx}{\linewidth}{|l|X|X|} \hline
  \foo
  \foo
  \foo
  end & end & end \\ \hline
\end{tabularx}

\pagebreak

% This table breaks. Fixed by removing a single foo.
\begin{tabularx}{\linewidth}{|l|X|X|} \hline
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo % Remove this line and table magically works again.                                                                                                                                                                                                                                                                                                                     
end & end & end \\ \hline
\end{tabularx}

\end{document}

Log file (required) and possibly PDF file

Attached files showing the bug and when the final foo is removed, working log and expected pdf output.

WC.show_bug.log WC.show_bug.pdf WC.working.log WC.working.pdf

davidcarlisle commented 3 years ago

Thanks for your report, that's ... unexpected. tabularx doesn't really inspect each row at all: it just inspects the column widths chosen by the primitive \halign for the entire table, and then re-runs if needed. So it's rather surprising that adding a row here makes any difference.

However I can confirm your report I'll need to trace what's happening but I'm currently travelling so may be a few days.

FrankMittelbach commented 3 years ago

I think it is just unsupported input and not a bug :-) even if tabularx doesn't say so. The problem seems to me that

\multicolumn{2}{X}{...}

makes little sense. As all "X" columns are the same width this makes 2 columns of width X into one column width X. If if puts enough material into "..." you see that it ends up being as wide as the 2 col and the third is empty.

Not tabuluarx sees 48 X initially and it makes some guesses about their width and complains about reaching \maxdimen (there it goes wrong because it doesn't expect the X in the \multicolumn, but as I said I don't think the input makes a lot of sense this way. I would need a different specifier for \multicolumn that kind of says "make me as wide as whatever the spanned columns are and then use a p-column of that width" but not make me an X-column spanning 2 X-columns)

but too late tonight ot think about this further, but in my feeling it is not a bug as such.

davidcarlisle commented 3 years ago

After @FrankMittelbach ;s comment you can use

\newcommand{\foo}{% 
a & z z z z & w w w w \\ \hline
b & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}X|}{x x x x} \\ \hline
}

to make a cell that spans the two X columns. But it still fails at the same point. Too late to look further now,

weetmuts commented 3 years ago

Thanks David and Frank! Actually I do know and use the \hsize setting fix, I just simplified the example as much as possible. I found the \hsize setting here: https://tex.stackexchange.com/questions/236155/tabularx-and-multicolumn

As you say, it feels a bit odd to specify an X inside a multicolumn, what happens if it somehow contributes to the calculation of the X column widths.... perhaps there is a way to state the calculated width of the box that will contain the word wrapped cell texts, without using an X?

Here is a more detailed example of what I actually do.

\documentclass[10pt,a4paper]{article}
\usepackage{parskip}
\usepackage{xltabular}

\overfullrule=10pt

\newcolumntype{R}{>{\raggedright\arraybackslash}X}

\newcommand*{\spantwo}[1]{\multicolumn{2}{>{\hsize=\dimexpr 2\hsize+2\tabcolsep\relax\raggedright\arraybackslash}X|}{#1}}
\newcommand*{\spanthree}[1]{\multicolumn{3}{|>{\hsize=0.95\textwidth\relax\raggedright\arraybackslash}X|}{#1}}

\newcommand{\foo}{a & $x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0$ & A comment comment comment comment comment. \\ \hline
b & \spantwo{$x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0 \land x>0$} \\ \hline
\spanthree{A comment comment comment comment comment comment comment comment comment comment comment comment.} \\ \hline
}

\begin{document}
\raggedright

\begin{xltabular}{\linewidth}{|l|R|R|} \hline
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
  \foo
%  \foo % Uncomment this line to break the table.                                                                                                                                                                                    
\end{xltabular}

\end{document}
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.

weetmuts commented 2 years ago

@FrankMittelbach @davidcarlisle Any luck?

davidcarlisle commented 2 years ago

@weetmuts sorry keeping this open haven't had time to analyse this further but there is definitely an issue (I thought the analyse further label stopped the stale bot marking it as stale but apparently not)

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.

weetmuts commented 2 years ago

@davidcarlisle Thanks! I am still interested in a solution.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.

Grosskopf commented 1 year ago

I have the same issue when using tabularx in tabularx too many times

\documentclass{book}
\usepackage{tabularx}
\RequirePackage{tabularx}
\begin{document}

\begin{center}

    \begin{tabularx}{\textwidth}{p{3.5cm}m{1cm}|X|X|X}
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
        % uncommenting the following line breaks the table
        % a & b & {\begin{tabularx}{0.99\linewidth}[t]{XX} c & d \end{tabularx}} & e & f\\
    \end{tabularx}
\end{center}
\end{document}

Interestingly enough, using tabular inside tabularx instead of tabularx inside tabularx results in it working, but then I have problems with alignment

github-actions[bot] commented 8 months ago

This issue has been automatically marked as stale because it has not had recent activity.