![Longtable 和 tabularx 以及自动换行问题](https://linux22.com/image/352445/Longtable%20%E5%92%8C%20tabularx%20%E4%BB%A5%E5%8F%8A%E8%87%AA%E5%8A%A8%E6%8D%A2%E8%A1%8C%E9%97%AE%E9%A2%98.png)
我有一个大于单页的表格 (tabularx),因此我尝试使用 longtable 包。Mytable 包含较长的文本,当我尝试将 tabularx-table 放入“longtable 环境”时,该文本超出了页边距 - 我认为是因为选项“X”不再受支持。因此我寻找其他包并找到了 tabu/longtabu ... 但说实话,情况变得更糟了。有人能帮我吗?
\documentclass[a4paper,bibliography=totoc,toc=listof,chapterentrydots=on,captions=tableheading,headings=small]{scrbook}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{pdflscape}
\usepackage{lscape}
\usepackage{longtable}
\usepackage[figuresright]{rotating}
\usepackage[babel]{csquotes}
\usepackage[language=autobib,
backend=biber,
citetracker=true,
autolang=other,
style=authoryear-comp,
maxcitenames=2,maxbibnames=99,
uniquename=false,uniquelist=true,
isbn=false,doi=false,
]{biblatex}
\usepackage{booktabs}
\newcommand{\tabitem}{~~\llap{\textbullet}~~}
\usepackage{pdflscape}
\usepackage{lscape}
\usepackage{longtable}
\usepackage{lipsum}
\usepackage{tabularx}
\begin{document}
\begin{table}[hbt]
\footnotesize
\renewcommand{\arraystretch}{1.5}
\caption[title]{\textbf{title}.}
\label{title}
\begin{tabularx}{\linewidth}{p{0.12\linewidth}p{0.15\linewidth}p{0.65\linewidth}}
\toprule
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\textbf{principle x} & \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{\textbf{This is one of many principles}}\\
& \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X}{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle: & \tabitem subprinciple I \par \tabitem subprinciple II \par \tabitem subprinciple III\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
也可以插入这些信息吗?
\endfirsthead
\multicolumn{3}{l}{{continued from previous page}} \\
\toprule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\endfoot
\bottomrule
\endlastfoot
答案1
- 由于您的表格位于浮动环境 (
table
) 中,该环境仅限于一页,因此包含\endfirsthead
毫无\endhead
意义。如果这是需求,则应使用longtable
,请参阅下面的第二个示例。
第一个例子:table
环境
- 我建议使用
tabular*
而不是tabularx
从你的 MWE 我制作了真正的最小 MWE :) 并定义\newcommand\mc[1]{...}
为\multicolumn
:
\documentclass[a4paper,captions=tableheading,headings=small]{scrbook}
\usepackage{booktabs, longtable, tabularx}
\newcommand{\tabitem}{~~\llap{\textbullet}~~}
\newcommand\mc[1]{\multicolumn{2}{p{\dimexpr0.85\linewidth-3\tabcolsep\relax}}{#1}}
\begin{document}
\begin{table}[hbt]
\footnotesize
\renewcommand{\arraystretch}{1.5}
\caption[title]{\textbf{title}.}
\label{title}
\begin{tabular*}{\linewidth}{>{\bfseries}p{0.15\dimexpr\linewidth-2\tabcolsep\relax}
p{0.15\dimexpr\linewidth-2\tabcolsep\relax}
p{0.70\dimexpr\linewidth-2\tabcolsep\relax}
}
\toprule
principle x & \mc{\textbf{This is one of many principles}}\\
& \mc{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle:
& \tabitem subprinciple I \par
\tabitem subprinciple II \par
\tabitem subprinciple III \\
principle x & \mc{\textbf{This is one of many principles}}\\
& \mc{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle:
& \tabitem subprinciple I \par
\tabitem subprinciple II \par
\tabitem subprinciple III \\
principle x & \mc{\textbf{This is one of many principles}}\\
& \mc{This is a very long text that belongs to the principle mentioned above. I hope that the automatic word wrap is working. } \\
& sub-principle:
& \tabitem subprinciple I \par
\tabitem subprinciple II \par
\tabitem subprinciple III \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
第二个示例:longtable
环境
\documentclass[a4paper,captions=tableheading,headings=small]{scrbook}
\usepackage{booktabs, longtable, tabularx}
\newcommand{\tabitem}{~~\llap{\textbullet}~~}
\newcommand\mc[1]{\multicolumn{2}{p{\dimexpr0.85\linewidth-3\tabcolsep\relax}}{#1}}
\usepackage{lipsum} % for dummy text in table
\usepackage{showframe} % for show table layout
\begin{document}
{
\footnotesize
\renewcommand{\arraystretch}{1.5}
\label{title}
\begin{longtable}{>{\bfseries}p{\dimexpr0.15\linewidth-2\tabcolsep\relax}
p{\dimexpr0.15\linewidth-2\tabcolsep\relax}
p{\dimexpr0.70\linewidth-2\tabcolsep\relax}
}
\caption[title]{\textbf{title}.}
\label{title} \\
\toprule
\endfirsthead
\caption[title]{continued from previous page} \\
\toprule
\endhead
%
\midrule
\multicolumn{3}{r}{Continued on next page} \\
\endfoot
\bottomrule
\endlastfoot
%
principle x & \mc{\textbf{This is one of many principles}} \\
& \mc{\lipsum*[1]} \\
& sub-principle:
& \tabitem subprinciple I \par
\tabitem subprinciple II \par
\tabitem subprinciple III \\
principle x & \mc{\textbf{This is one of many principles}} \\
& \mc{\lipsum*[1]} \\
& sub-principle:
& \tabitem subprinciple I \par
\tabitem subprinciple II \par
\tabitem subprinciple III \\
principle x & \mc{\textbf{This is one of many principles}} \\
& \mc{\lipsum*[1]} \\
& sub-principle:
& \tabitem subprinciple I \par
\tabitem subprinciple II \par
\tabitem subprinciple III \\
principle x & \mc{\textbf{This is one of many principles}} \\
& \mc{\lipsum*[1]} \\
& sub-principle:
& \tabitem subprinciple I \par
\tabitem subprinciple II \par
\tabitem subprinciple III \\
\end{longtable}
}
\end{document}