2

I am creating a large table which contains smaller tables in some cells. I was able to do this simply with the tabularx package, which allowed me to automatically size columns and wrap text. However, my table will span multiple pages, and I'd like automatic page breaks.

I tried ltablex package, which nicely breaks my table over multiple pages but only when there are no tables nested within cells. I tried using a tabular table (instead of tabularx) inside of a cell, and that works fine. However, I need the text-wrapping capabilities of the tabularx package for my nested table as well.

Here's some example code which works fine with the ltablex package commented out (first image below) but goes over the edge of my page. Similarly, ltablex works fine if I comment out the nested table in row 3 (second image below). I'm hoping someone can suggest a simple way to have the best of both worlds.

\documentclass{article}
\usepackage[utf8]{inputenc}
%\usepackage{ltablex}
\usepackage{tabularx}

\makeatletter
\newcommand{\cellwidth}{\TX@col@width}  
\makeatother

\begin{document}
\begin{tabularx}{\linewidth}{|l|X|X|X|}
\hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline

&{\begin{tabularx}{\cellwidth}{|X|X|}
\hline
col 1 & col 2 \\ \hline
col 1 & col 2 \\ \hline
\hline
\end{tabularx}}&& \\ \hline

Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline

\end{tabularx}
\end{document}

With tabularx nested tables, no page breaking: enter image description here

With ltablex table, no nested table:

enter image description here

Thank you!

  • I would not use ltablex and just use a longtable for the outer table and tabularx or tabular etc for the inner ones. – David Carlisle Jul 14 '17 at 13:43
1

I would use longtable for the outer table.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{tabularx}


\begin{document}
\begin{longtable}{|l|*3{>{\raggedright\arraybackslash}p{.24\textwidth}|}}
\hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline

&{\begin{tabularx}{\linewidth}{|>{\raggedright\arraybackslash}X|>{\raggedright\arraybackslash}X|}
\hline
col 1 & col 2 \\ \hline
col 1 & col 2 \\ \hline
\hline
\end{tabularx}}&& \\ \hline

Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline
Column 1 & Ideally, this text will wrap automatically inside the cells. The table should contain other tables in some cells. The table should be able to break across pages automatically. & Column 3 & Column 4 \\ \hline

\end{longtable}
\end{document}
  • While that worked well for the example above, I have a follow-up question. Is it possible to nest a longtable within another longtable so that the interior tables also break across pages as needed? – hcummin Jul 14 '17 at 19:40
  • @hcummin no longtable only break between rows. – David Carlisle Jul 14 '17 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.