表格标题格式

表格标题格式

我以前也遇到过这个问题(有点)——它与我之前问过这个问题。

基本上,我在表格标题方面遇到了麻烦。无论下面各列中的文本如何对齐,我都希望标题加粗并居中。我已定义一个新命令head来执行此操作,除了最后一列外,该命令似乎运行良好。如果我在最后一列中使用headin place of,\bfseries则会收到错误

! Misplaced \noalign. \cmidrule ->\noalign 
                     {\ifnum 0=`}\fi \@ifnextchar [{\@cmidrule }{\@cmidrule ... l.37 \cmidrule
              (r){1-1} \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-...

我读过各种关于“错位 \noalign”的投诉,这些投诉都与反斜杠有关,但我不明白为什么最后一列的行为应该与其他列有所不同。

我尝试通过添加 来使文本居中,\centering但所需的操作\par会弄乱垂直对齐。无论如何,我认为我不应该做其他事情,但我看不出我做错了什么?

\documentclass[11pt, a4paper, twoside, fleqn]{report}
\usepackage[showframe]{geometry}

\makeatletter
\g@addto@macro\@floatboxreset\centering % automatically center floats
\makeatother


%Table formatting
%------------------------------
\usepackage{array}
\usepackage{booktabs}
\renewcommand{\arraystretch}{1.5}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\newcommand{\head}[1]{\centering\textbf{#1}}

%Definitions of useful shortcuts (e.g. to add matrix formatting)
%---------------------------------------------------------------


\begin{document}         

\begin{table}

\footnotesize
%%%\addtolength{\tabcolsep}{-0.6pt}

\begin{tabular}{ @{} L{1.5cm}  C{1.5cm} C{2cm} C{1.5cm} C{2cm} L{4cm} @{}}

\toprule

\head{Head}  & \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & \bfseries Stuff Things \\
\head{Head}  & \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & {\head{Stuff Things}} \\

\cmidrule(r){1-1} \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-6} % no "r" here because it's the last   

\multicolumn{2}{@{}l}{\bfseries Subheading}\\% \addlinespce

text & text & some much longer text & text &   some much longer text & some even longer even longer even longer text \\

\bottomrule
\end{tabular}

\caption{And here's the caption}
\end{table}




\end{document}

更新:

\listfiles给出以下内容:

  report.cls    2005/09/16 v1.4f Standard LaTeX document class<br>
   fleqn.clo    1998/08/17 v1.1c Standard LaTeX option (flush left equations)<br>
  size11.clo    2005/09/16 v1.4f Standard LaTeX file (size option)<br>
   array.sty    2005/08/23 v2.4b Tabular extension package (FMi)<br>
booktabs.sty    2005/03/16 v1.6180 publication quality tables

我们很清楚我们的 latex 已经非常过时了!我正在尝试修复这个问题。如有必要,我可以在本地覆盖。

需要明确的是,如果我替换

& \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & {\bfseries Stuff Things} \\

& \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & \head{Stuff Things} \\

更新 2

我已经编辑了上面的代码,以使其更加明显地表明该问题仅出现在最后一列(第一列也是 L 对齐,但命令\head以我的标题为中心)。

根据 Johannes_B 的建议,如果我在最后一列添加一组额外的括号

& \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & {\head{Stuff Things}} \\

编译问题消失,但最后的列标题仍然保持左对齐:

桌子

makecell尤其是使用和thead似乎工作正常,但我仍然想知道我的代码出了什么问题!有人能告诉我为什么会出现这种情况吗?

答案1

使用makecell包:它特别定义了支持常见格式的\thead命令 \makecell单元格内的换行符。默认情况下,单元格的内容垂直和水平居中。此外,您的表格会按原样进入边距。我将环境更改为,tabularx并将最后一列X类型重新定义为单元格垂直居中。

\documentclass[11pt, a4paper, twoside, fleqn]{report}

\makeatletter
\g@addto@macro\@floatboxreset\centering % automatically center floats
\makeatother

%Table formatting
%------------------------------
\usepackage{array}
\usepackage{booktabs}
\renewcommand{\arraystretch}{1.5}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash\hspace{0pt}}m{#1}}

\newcommand{\head}[1]{\hfill\textbf{#1}\hfill\mbox{}}
\usepackage{makecell, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand{\tabularxcolumn}[1]{>{\small}m{#1}}
%---------------------------------------------------------------

\usepackage[nomarginpar,  showframe]{geometry}

\begin{document}

\begin{table}

\footnotesize
\begin{tabularx}{\linewidth}{ @{} L{1.5cm}  C{1.5cm} C{2cm} C{1.5cm} C{2cm}X @{}}

\toprule
  \multicolumn{1}{c}{\bfseries Something} & \multicolumn{2}{c}{\bfseries Something Else} &  \multicolumn{2}{c}{\bfseries Something Else Entirely} &  \\

  & \thead{Stuff} & \thead{Things}  & \thead{Stuff} & \thead{Things} & \head{ Stuff Things} \\

\cmidrule(r){1-1} \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-6} % no "r" here because it's the last

\multicolumn{2}{@{}l}{\bfseries Subheading}\\% \addlinespce

text & text & some much longer text & text &   some much longer text & some even longer even longer even longer text \\

\midrule

\multicolumn{2}{@{}l}{\bfseries Subheading 2}\\% \addlinespce

text & text & some much longer text & text &   some much longer text & some even longer even longer even longer text \\

\bottomrule
\end{tabularx}

\caption{And here's the caption}
\end{table}

\end{document} 

在此处输入图片描述

** 评论 OP 的代码:**

您不需要\let\newline\\,也不需要最后一个 周围的括号\head。最后一列的问题似乎来自于您更改了最后一列单元格的对齐方式。我看到两个解决方案(解决方法?):

  1. 在“头部”行中,将其替换\\\tabularnewline
  2. 或者保留\\,但将 head 的定义替换为以下内容:

    \newcommand{\head}[1]{\hfill\textbf{#1}\hfill\mbox{}}

在此处输入图片描述

相关内容