![长表中的多列宽度](https://linux22.com/image/254280/%E9%95%BF%E8%A1%A8%E4%B8%AD%E7%9A%84%E5%A4%9A%E5%88%97%E5%AE%BD%E5%BA%A6.png)
如何设置 longtable 单元格中的多列宽度?如果您运行我的 MWE,您将看到 longtable 的第一行超出了页面限制。
\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
\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}