我正在尝试使用多行元素(全部在一个环境中)设置一个长表标题landscape
,以便将单位放置在列标签下方,尽管我尝试了所有方法,但我无法使其工作。
最初,我在这里找到了一个问题,它为我提供了一个模板(我现在无论如何也找不到它),但它无法正常工作。我查找了软件包文档,认为问题出在选项上*
,它提供了以下见解:
text
是构造的实际文本。如果明确设置了宽度,文本将设置在该宽度的 \parbox 中;您可以使用 \\ 强制在所需的位置进行换行。
该文档似乎还表明,其中=
的选项\multirow
也设置了\parbox
(因此允许在文本主体中使用\\
,但它无法改变错误消息的状态。
以下是 MWE:
\documentclass[10pt,titlepage]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{xstring}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{lscape}
\usepackage{multirow}
\begin{document}
\begin{landscape}
\begin{longtable}[c]{lllrrrrrrrrr}
% Set up caption and label
\caption{An example of badness}
\label{tab:table}
% First page head
\hline
\multirow{2}{=}{\textbf{A}} & \multirow{2}{=}{\textbf{B}} & \multirow{2}{=}{\textbf{C}}
& \multirow{2}{=}{\textbf{D} \\ [kg]} & \multirow{2}{=}{\textbf{E} \\ [kg]} & \multirow{2}{=}{\textbf{F} \\ [kg]}
& \multirow{2}{=}{\textbf{G} \\ [kg]} & \multirow{2}{=}{\textbf{H} \\ [kg]} & \multirow{2}{=}{$\mathbf{\alpha}$ \\ [\%]}
& \multirow{2}{=}{$\mathbf{\beta}$ \\ [\%]} & \multirow{2}{=}{$\mathbf{\gamma}$ \\ [s]} & \multirow{2}{=}{$\mathbf{\Delta}$ \\ [m/s]} \\
\hline
\endfirsthead
\hline
\multirow{2}{=}{\textbf{A}} & \multirow{2}{=}{\textbf{B}} & \multirow{2}{=}{\textbf{C}}
& \multirow{2}{=}{\textbf{D} \\ [kg]} & \multirow{2}{=}{\textbf{E} \\ [kg]} & \multirow{2}{=}{\textbf{F} \\ [kg]}
& \multirow{2}{=}{\textbf{G} \\ [kg]} & \multirow{2}{=}{\textbf{H} \\ [kg]} & \multirow{2}{=}{$\mathbf{\alpha}$ \\ [\%]}
& \multirow{2}{=}{$\mathbf{\beta}$ \\ [\%]} & \multirow{2}{=}{$\mathbf{\gamma}$ \\ [s]} & \multirow{2}{=}{$\mathbf{\Delta}$ \\ [m/s]} \\
\hline
\endhead
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
\hline
\end{longtable}
\end{landscape}
\end{document}
它基本上会导致 LaTeX 发出大量错误消息,如下所示。虽然我对 LaTeX 的许多“程序化”方面基本一无所知,但我可以看出这些错误中的许多似乎都是通用的。
Misplaced \noalign. \hline
Misplaced \omit. \multirow
Extra alignment tab has been changed to \cr. \multirow
Missing number, treated as zero. & \multirow{2}{=}{\textbf{D} \\ [kg]}
Illegal unit of measure (pt inserted). & \multirow{2}{=}{\textbf{D} \\ [kg]}
Missing number, treated as zero. ... & \multirow{2}{=}{\textbf{E} \\ [kg]}
Illegal unit of measure (pt inserted). ... & \multirow{2}{=}{\textbf{E} \\ [kg]}
Missing number, treated as zero. ... & \multirow{2}{=}{\textbf{F} \\ [kg]}
Illegal unit of measure (pt inserted). ... & \multirow{2}{=}{\textbf{F} \\ [kg]}
Missing number, treated as zero. & \multirow{2}{=}{\textbf{G} \\ [kg]}
Illegal unit of measure (pt inserted). & \multirow{2}{=}{\textbf{G} \\ [kg]}
Missing number, treated as zero. ... & \multirow{2}{=}{\textbf{H} \\ [kg]}
Illegal unit of measure (pt inserted). ... & \multirow{2}{=}{\textbf{H} \\ [kg]}
Illegal unit of measure (pt inserted). ...\multirow{2}{=}{$\mathbf{\alpha}$ \\ [\%]}
Illegal unit of measure (pt inserted). ... \multirow{2}{=}{$\mathbf{\beta}$ \\ [\%]}
Missing number, treated as zero. ... \multirow{2}{=}{$\mathbf{\gamma}$ \\ [s]}
Illegal unit of measure (pt inserted). ... \multirow{2}{=}{$\mathbf{\gamma}$ \\ [s]}
Missing number, treated as zero. ...multirow{2}{=}{$\mathbf{\Delta}$ \\ [m/s]}
Illegal unit of measure (pt inserted). ...multirow{2}{=}{$\mathbf{\Delta}$ \\ [m/s]}
Missing number, treated as zero. & \multirow{2}{=}{\textbf{D} \\ [kg]}
Illegal unit of measure (pt inserted). & \multirow{2}{=}{\textbf{D} \\ [kg]}
Missing number, treated as zero. ... & \multirow{2}{=}{\textbf{E} \\ [kg]}
Illegal unit of measure (pt inserted). ... & \multirow{2}{=}{\textbf{E} \\ [kg]}
Missing number, treated as zero. ... & \multirow{2}{=}{\textbf{F} \\ [kg]}
Illegal unit of measure (pt inserted). ... & \multirow{2}{=}{\textbf{F} \\ [kg]}
Missing number, treated as zero. & \multirow{2}{=}{\textbf{G} \\ [kg]}
Illegal unit of measure (pt inserted). & \multirow{2}{=}{\textbf{G} \\ [kg]}
Missing number, treated as zero. ... & \multirow{2}{=}{\textbf{H} \\ [kg]}
Illegal unit of measure (pt inserted). ... & \multirow{2}{=}{\textbf{H} \\ [kg]}
Illegal unit of measure (pt inserted). ...\multirow{2}{=}{$\mathbf{\alpha}$ \\ [\%]}
Illegal unit of measure (pt inserted). ... \multirow{2}{=}{$\mathbf{\beta}$ \\ [\%]}
Missing number, treated as zero. ... \multirow{2}{=}{$\mathbf{\gamma}$ \\ [s]}
Illegal unit of measure (pt inserted). ... \multirow{2}{=}{$\mathbf{\gamma}$ \\ [s]}
Missing number, treated as zero. ...multirow{2}{=}{$\mathbf{\Delta}$ \\ [m/s]}
Illegal unit of measure (pt inserted). ...multirow{2}{=}{$\mathbf{\Delta}$ \\ [m/s]}
No file texstudio_tEAINe.aux.
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (37.0pt too high) detected
Overfull \vbox (37.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \hbox (6265.24237pt too wide) in alignment
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (37.0pt too high) detected
Overfull \vbox (37.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \vbox (12.0pt too high) detected
Overfull \hbox (6265.24237pt too wide) in alignment
Overfull \hbox (6265.24237pt too wide) in alignment
Label(s) may have changed. Rerun to get cross-references right.
我愿意接受任何能让这个表格正常工作的解决方案(即,一个横向格式的每页标题的长表)。在做这个问题的一些作业时,我找到了一些解决方案比如这个使用\thead
换行符似乎可以解决问题,但是经过一晚上的调试后,我不愿意在没有先获得帮助的情况下尝试另一种方法。
感谢任何人提供的帮助。
答案1
- 你对单元格的使用
multirow
是错误的。你应该使用包thead
中定义的宏。makecell
- 代码行
\caption
必须以\\
更正并完成的 MWE 为:
\documentclass[titlepage]{report}
\usepackage{geometry}
\usepackage{longtable, makecell}
\renewcommand\theadfont{\bfseries}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\begin{longtable}[c]{lllrrrrrrrrr}
% Set up caption and label
\caption{An example of badness}
\label{tab:table} \\ % <--- missed
% First page head
\hline
\thead{A} & \thead{B} & \thead{C} & \thead{D \\ {[kg]}}
& \thead{E \\ {[kg]}} & \thead{F \\ {[kg]}}
& \thead{G \\ {[kg]}} & \thead{H \\ {[kg]}}
& \thead{$\mathbf{\alpha}$ \\ {[\%]}}
& \thead{$\mathbf{\beta}$ \\ {[\%]}}
& \thead{$\mathbf{\gamma}$ \\ {[s]}}
& \thead{$\mathbf{\Delta}$ \\ {[m/s]}} \\
\hline
\endfirsthead
\caption[]{An example of badness (cont.)}\\ % <--- added
\hline
\thead{A} & \thead{B} & \thead{C} & \thead{D \\ {[kg]}}
& \thead{E \\ {[kg]}} & \thead{F \\ {[kg]}}
& \thead{G \\ {[kg]}} & \thead{H \\ {[kg]}}
& \thead{$\mathbf{\alpha}$ \\ {[\%]}}
& \thead{$\mathbf{\beta}$ \\ {[\%]}}
& \thead{$\mathbf{\gamma}$ \\ {[s]}}
& \thead{$\mathbf{\Delta}$ \\ {[m/s]}} \\
\hline
\endhead
\multicolumn{12}{r}{\small\textit{Continued on next page}} \\
\endfoot % <--- added
\hline
\endlastfoot % <--- added
% table body
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
\hline
\end{longtable}
\end{landscape}
\end{document}
如果表格内容与真实内容非常接近,则表格可以适合纵向页面。
附录:
使用该tabularray
包您可以获得更简单、更简短的表格代码以及更好的结果。
编辑:改进的表格布局:
\documentclass[titlepage]{report}
\usepackage{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\ExplSyntaxOn
\NewChildSelector{eachtwo}
{
\int_step_inline:nnnn {2}{2}{\l_tblr_childs_total_tl}
{ \clist_put_right:Nn \l_tblr_childs_clist {##1} }
}
\ExplSyntaxOff
\usepackage{pdflscape}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}% For dummy text. Don't use in a real document
\begin{document}
\lipsum[1-5]
% \begin{landscape}
\begin{longtblr}[
caption = {An example of badness},
label = {tab:table}
]{
rowhead = 1,
row{1} = {font=\small\bfseries},
row{2-Z} = {rowsep=-0.5pt},
row{eachtwo} = {abovesep=1ex},
colspec = {*{3}{l} *{9}{Q[r,m]} }
}
\toprule
A & B & C & {D \\ {[\si{\kilo\gram}]}}
& {E \\ {[\si{\kilo\gram}]}}
& {F \\ {[\si{\kilo\gram}]}}
& {G \\ {[\si{\kilo\gram}]}}
& {H \\ {[\si{\kilo\gram}]}}
& {$\mathbf{\alpha}$ \\ {[\si{\kilo\gram}]}}
& {$\mathbf{\beta}$ \\ {[\si{\kilo\gram}]}}
& {$\mathbf{\gamma}$ \\ {[\si{\kilo\gram}]}}
& {$\mathbf{\Delta}$ \\ {[\si{\kilo\gram}]}} \\
\midrule
% table body
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
A & B & C & D & E & F & G & H & I & 1 & 2 & 3 \\
\bottomrule
\end{longtblr}
% \end{landscape}
\end{document}
(红线表示页面布局)
答案2
只是为了了解发生了什么:您的文档中有三个问题。
- 仅对于类型列
{=}
才\multirow
可以正常工作p{}
。 \\
后面应该有一个\label
。- 形式的所有构造
\\ [kg]
都是错误的,因为[kg]
被视为 的可选参数\\
。 在 之外也一样\multirow
。 要么将所有这些放在括号内{[kg]}
,要么改用\newline [kg]
。 当然,这也适用于括号中的其他单位。
这是我的诊断。我认为其他带有makecell
或的解决方案tabularray
是解决这些问题的最简单方法(它们也使用构造{[...]}
)。