使用 ltablex 包嵌套表格

使用 ltablex 包嵌套表格

我正在创建一个大表格,其中某些单元格包含较小的表格。我可以使用 tabularx 包轻松完成此操作,该包允许我自动调整列大小和换行。但是,我的表格将跨越多页,我希望自动分页。

我尝试了 ltablex 包,它可以很好地将我的表格分成多个页面,但是仅当单元格内没有嵌套表格时。我尝试在单元格内使用 tabular 表格(而不是 tabularx),效果很好。但是,我还需要 tabularx 包的文本换行功能来​​处理嵌套表格。

以下是一些示例代码,在注释掉 ltablex 包的情况下运行良好(下图第一张),但超出了我的页面范围。同样,如果我注释掉第 3 行的嵌套表(下图第二张),ltablex 也可以正常工作。我希望有人能提出一种简单的方法,以兼顾两方面的优势。

\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}

使用 tabularx 嵌套表格,无需分页: 在此处输入图片描述

使用 ltablex 表,没有嵌套表:

在此处输入图片描述

谢谢你!

答案1

我会用longtable在外面的桌子。

在此处输入图片描述

\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}

相关内容