I'm preparing a long table (> 1 page, so I'm using the longtable
package) that is set to take up the full \textwidth of a page. I aim to produce both letter- and A4-sized versions of the document, but as these standards have different sizes, the table with have different widths in each of the documents.
I expect to expand the table some in the future, and I'd like to avoid manually adjusting the relevant length parameters whenever I switch back and forth between the letter and A4 formats. Thus, I am using the tabularx
package and (for the rightmost column) the X
column type it furnishes that expands so that the table has the designated width. In order to use simultaneously the features of longtable
and tabularx
I am using the ltablex
package, which is designed for this purpose.
On top of this, the rightmost cell of each row of the tabular environment contains another table. This inner table again has one column of type X
. The ltablex
package redefines the tabularx
environment, a consequence of which is that attempting to nest one tabularx
environment in another produces an error:
! Extra alignment tab has been changed to \cr.
<template> \endtemplate
In an answer to a related question David Carlisle gave a workaround for this issue, defining a command restoretx
that restores the ability to nest tabularx
environments.
This allows, for example, one to create nested tables, so that, e.g., this m.w.e. compiles and produces the expected results:
\documentclass{article}
\usepackage{blindtext}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{tabularx}
%This block is from David Carlisle's code in the linked question
\makeatletter
\let\tx@\TX@endtabularx
\def\restoretx{\let\TX@endtabularx\tx@}
\makeatother
\usepackage{ltablex}
\newcommand{\innerTable}[2]{
{
\restoretx
\begin{tabularx}{\linewidth}{cX}
#1 & #2 \\
\end{tabularx}
}
}
\setlength{\parindent}{0cm}
\begin{document}
\begin{tabularx}{\linewidth}{cX}
test & \innerTable{test2}{\blindtext[1]} \\
test & \innerTable{test2}{\blindtext[1]} \\
[...about 40 copies of this row in total...]
test & \innerTable{test2}{\blindtext[1]} \\
test & \innerTable{test2}{\blindtext[1]} \\
\end{tabularx}
\end{document}
The number of rows in the above example is not accidental: If we add a small number of additional copies of the row test & \innerTable{test2}{\blindtext[1]} \\
then the file will still compile, but the formatting breaks badly. In this case a typical page now looks like
Worse, this behavior is inconsistent across runs: Sometimes a file with 40 copies of the line will compile correctly, other times not.
Consulting the logfile shows the following warning:
Package tabularx Warning: X Columns too narrow (table too wide)
(tabularx) on input line 79.
I know that the tabularx
package (and so probably the ltablex
package) tries to compute for each column of type X
an appropriate width by some iterative process, so I suppose this warning means that the package gave up on trying to find a good width.
Is there a way to compel
ltablex
to try harder to find a good width for anX
column, albeit at the cost of time/memory? (The table I'm actually working with is much longer than the one above, ~170 rows.) Or is there another workaround that still uses nestedxtabular
environments withX
columns? If this is hopeless (or even if it's not), what other methods can be used to achieve the same end?
Some possible solutions---and the reasons I initially rejected them---which I might have might have to settle for:
Another package,Edit I attempted this, and the compilation fails altogether, reporting a memory issue:ltxtable
, also marrieslongtable
andtabularx
, and its behavior is somewhat different fromltablex
. But (both the installation and use of) this package seems idiosyncratic, including that by default the table code is required to be in a separate file, which I'd like to avoid it possible. (I'd expect this particular limitation can be circumvented by using yet another package.)! TeX capacity exceeded, sorry [text input levels=15].
Probably I could just measure and hardcode the relevant lengths for both of the two page sizes and write a switch that checks the page size to determine which sizes to use---but this is kludgy and would require actually measuring column sizes (this is feasible, though).
This question is similar, by the way, but the OP's aim there is evidently somewhat different from mine, since the accepted answer thereto (also by David Carlisle, incidentally) uses columns of manually defined widths, which is exactly what I aim to avoid.
Edit A posted answer suggests using the xltabular
package instead of the ltablex
package. This package has the apparent advantage that one doesn't need to define a command like \restoretx
in the m.w.e. above, but using the package it does not resolve the issue in the question. In fact, it produces output whose formatting is broken in essentially the same way: