Longtable 和 tabularx 以及自动换行问题

Longtable 和 tabularx 以及自动换行问题

我有一个大于单页的表格 (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}

在此处输入图片描述

相关内容