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
ltablexto try harder to find a good width for anXcolumn, 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 nestedxtabularenvironments withXcolumns? 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 marrieslongtableandtabularx, 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:





tabcolsepfrom left and right side of tables). – Zarko Sep 15 '19 at 11:41test & \innerTable{test2}{\lipsum*[1]} \\from 6 to 50 and compile, the formatting breaks in apparently the same way it does with theltablexpackage: https://i.imgur.com/vHg1XQk.png . (I'll update my question to reflect this additional information.) If you have any more insight/suggestions, I'd be grateful to receive them. Thanks again! – Travis Sep 15 '19 at 19:22landscapeoption when loading thegeometrypackage you'll find that the threshold beyond which the formatting issue occurs is closer to 20 rows, so the success of the code depends also in a substantial way on page geometry. – Travis Sep 15 '19 at 20:04\ltablexwith a three-column table in the first place.) In my case, the entries of the left cells of the nested tables have variable length, so any solution needs to support output like here: https://i.imgur.com/TP9hL4S.png (This particular example is ugly, but in my actual use case the left cells of the nested tables are short and have just a handful of symbols, and the right cells have rather less text.) – Travis Sep 16 '19 at 05:40