如何設定 longtable 單元格中的多列寬度?如果您運行我的 MWE,您會發現我的長表的第一行超出了頁數限制。
\documentclass[11pt,a4paper,oneside]{report}
\usepackage{setspace}
\usepackage{array}
\usepackage{covington}
\usepackage[danish,english]{babel}
\usepackage{longtable}
\usepackage{caption}
\usepackage{multirow}
\begin{document}
\begin{longtable}{Rp{9cm}}
\caption{Example}\\
\endfirsthead
\caption[]{(continued)}\\
\endhead
\endlastfoot
\multicolumn{2}{l}{Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.}\\
\\
\textsc{Lorem}&"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."\\
{\dag} \textsc{Ipsum}&"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."\\
\end{longtable}
\end{document}
答案1
你很幸運,因為包longtable
記錄了文件中列的寬度.aux
。這可以用來計算前兩列的寬度。
宏\GetLongtableWidths
提取下一個 的寬度longtable
。它是在之前調用的\begin{longtable}
。裡面使用的是p
規範。\wdcal
第一個參數是最小寬度,如果檔案中尚不存在寬度,則使用該最小寬度.aux
。該寬度應小於或至多等於最終寬度。的第二個參數\wdcal
採用一個表達式。\wdcol<colnum>
被替換為列的寬度<colnum>
。由於p{...}
的列規範\multicolumn
不抑制\tabcolsep
(在列的左側和右側),因此\tabcolsep
需要減去 2。
\documentclass[11pt,a4paper,oneside]{report}
\usepackage{setspace}
\usepackage{array}
\usepackage{covington}
\usepackage[danish,english]{babel}
\usepackage{longtable}
\usepackage{caption}
\usepackage{multirow}
\makeatletter
\newcommand*{\GetLongtableWidths}{%
\begingroup
\count@=\value{LT@tables}%
\advance\count@\@ne
\edef\tmp{LT@\romannumeral\count@}%
\@ifundefined{\tmp}{%
}{%
\count@=\z@
\def\LT@entry##1##2{%
\advance\count@\@ne
\expandafter
\gdef\csname @wdcol\the\count@\endcsname{##2}%
}%
\csname\tmp\endcsname
}%
\endgroup
}
\newcommand*{\wdcol}[1]{%
\@ifundefined{@wdcol\number#1}%
{0pt}%
{\csname @wdcol\number#1\endcsname}%
}
\newcommand*{\wdcal}[2]{%
\ifdim\dimexpr(#2)\relax<0pt %
\dimexpr(#1)\relax
\else
\dimexpr(#2)\relax
\fi
}
\makeatother
\begin{document}
\GetLongtableWidths
\begin{longtable}{rp{9cm}}
\caption{Example}\\
\endfirsthead
\caption[]{(continued)}\\
\endhead
\endlastfoot
\multicolumn{2}{p{\wdcal{9cm}{\wdcol{1}+\wdcol{2}-2\tabcolsep}}}{Lorem ipsum dolor s
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.}\\
\\
\textsc{Lorem}&"Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum."\\
{\dag} \textsc{Ipsum}&"Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum."\\
\end{longtable}
\end{document}