我在使用带有换行符的 s中的\textbf
或等字体时遇到了一些麻烦。以下代码有效:\spacedlowsmallcaps
\makecell
\begin{table}
\centering
\begin{tabular}{lcc}
\toprule
\textbf{HEADER 1} & \spacedlowsmallcaps{Header 2} & \spacedlowsmallcaps{Header 3} \\
\midrule
cell 1 & cell 2 & cell 3 \\
\end{tabular}
\end{table}
但是,如果第二个标题很长并且需要换行符而我想使用它\makecell
来获得良好的格式,则会收到错误:
\begin{table}
\centering
\begin{tabular}{lcc}
\toprule
\textbf{HEADER 1} & \makecell[c]{\spacedlowsmallcaps{Very Long Header 2\\that needs a line break}} & \spacedlowsmallcaps{Header 3} \\
\midrule
cell 1 & cell 2 & cell 3 \\
\end{tabular}
\end{table}
这里,LaTeX 向我抛出了以下多个错误:
Missing } inserted.
<inserted text>
Extra }, or forgotten \endgroup.
<recently read> \egroup
这是为什么?我该如何解决?我知道我可以将长文本拆分成
\spacedlowsmallcaps{Very Long Header 2}\\\spacedlowsmallcaps{that needs a line break}
newcommand
但这会使代码变得更加混乱,如果我想在只传递可能包含或不包含换行符的文本的地方使用它,它就不起作用。
编辑1:更清楚一点:当我用类似或 之\spacedlowsmallcaps
类的东西替换时,同样适用。\textbf
\textit
编辑2:根据要求,这里有一份完整的文档来说明该问题:
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}
\newcommand{\tableheader}[2]{\makecell[#1]{\textbf{#2}}}
\begin{document}
\begin{table}
\centering
\begin{tabular}{lcc}
\toprule
% \textbf{HEADER 1} & \makecell[c]{\textbf{Very Long Header 2\\that needs a line break}} & \textbf{Header 3} \\
% the above line does not work
% \textbf{HEADER 1} & \makecell[c]{\textbf{Very Long Header 2}\\\textbf{that needs a line break}} & \textbf{Header 3} \\
% this line is syntactically correct,
% but it becomes a problem when using it with a predefined command:
\tableheader{l}{HEADER 1} & \tableheader{c}{Very Long Header 2\\that needs a line break} & \tableheader{c}{Header 3} \\
\midrule
cell 1 & cell 2 & cell 3 \\
\end{tabular}
\end{table}
\end{document}
答案1
不用重新发明轮子,而是使用包中为以下情况提供的\thead
宏:makecell
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}
\centering
\begin{tabular}{lcc}
\toprule
\thead{HEADER 1} & \thead[c]{Very Long Header 2\\that needs a line break} & \thead{Header 3} \\
\midrule
cell 1 & cell 2 & cell 3 \\
\end{tabular}
\end{table}
\end{document}
答案2
编辑:
使用makcell
每个用某些命令格式化的行,作为在行终止之前必须关闭的命令。所以\textbf{...}
,\spacedlowsmallcaps{...}
\makecell[c]{\spacedlowsmallcaps{Very Long Header 2 \\
that needs a line break}}
是错误的,
\makecell[c]{\spacedlowsmallcaps{Very Long Header 2}\\
\spacedlowsmallcaps{that needs a line break}}
是正确的。
根据上述规则,你的代码片段应该写成:
\begin{table}[ht]
\centering
\begin{tabular}{lcc}
\toprule
\textbf{HEADER 1} & \spacedlowsmallcaps{Header 2} & \spacedlowsmallcaps{Header 3} \\
\midrule
cell 1 & cell 2 & cell 3 \\
\end{tabular}
\end{table}
\begin{table}[ht]
\centering
\begin{tabular}{lcc}
\toprule
\textbf{HEADER 1} & \makecell[c]{\spacedlowsmallcaps{Very Long Header 2}\\
\spacedlowsmallcaps{that needs a line break}}
& \spacedlowsmallcaps{Header 3} \\
\midrule
cell 1 & cell 2 & cell 3 \\
\end{tabular}
\end{table}
\begin{table}[ht]
\centering
\begin{tblr}{colspec={lcc}, row{1}={m}}
\toprule
\textbf{HEADER 1} & {\spacedlowsmallcaps{Very Long Header 2}\\
\spacedlowsmallcaps{that needs a line break}}
& \spacedlowsmallcaps{Header 3} \\
\midrule
cell 1 & cell 2 & cell 3 \\
\end{tblr}
\end{table}
当您提供带有表格的完整小文档时,我将能够完成提供的代码片段。
对于最后一张表,您需要在序言中加载。
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
附录:
- 在某些情况下,当格式化文本的命令可以用开关替换时,例如
\textbf{...}
,\bfseries
你可以写
\textbf{HEADER 1} & \bfseries\makecell[c]{Very Long Header 2\\
that needs a line break}
& \textbf{Header 3} \\
这相当于@samcarter_is_at_topanswers.xyz 答案中提出的解决方案。
- 在类似情况下,使用
tabularry
包会非常方便。例如,您添加到问题的示例可以写成:
\documentclass[10pt,a4paper]{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}[ht]
\centering
\begin{tblr}{colspec={lcc},
row{1} = {font=\bfseries, c, m}
}
\toprule
HEADER 1 & {Very Long Header 2\\ that needs a line break}
& header 3 \\
\midrule
cell 1 & cell 2 & cell 3 \\
cell 1 & cell 2 & cell 3 \\
\bottomrule
\end{tblr}
\end{table}
\end{document}