样式化行单元格

样式化行单元格

\nlinemakecell 包中的数字行命令会插入n带有包含递增数字的模式的单元格。我想将它们视为标题,并使用\theadfont和进行\theadalign样式设置。我查看了 makecell 的源代码,但不明白如何修补它。

平均能量损失

\documentclass[border=3pt]{standalone}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries\footnotesize}
\renewcommand\theadalign{cc}
\begin{document}
{\small\setcellgapes{8pt}\makegapedcells
  \begin{tabular}{@{}rllllll@{}}
    & \nline{6}\\\hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz \\
\end{tabular}}
\end{document}

答案1

\documentclass{article}

\usepackage{makecell}
\usepackage{xinttools}

\newcommand{\headalign}{\multicolumn{1}{c}}

\renewcommand\theadfont{\bfseries\footnotesize}

\begin{document}

\begin{tabular}{ r *{6}{l} }
    \xintFor* #1 in {\xintSeq{1}{6}}\do{&\headalign{\theadfont{#1}}}\\
    %       & \nline{6} \\ %(was for comparison)
  \hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz
\end{tabular}

\end{document}

在此处输入图片描述

答案2

下面提供了您的问题的部分解决方案 - 应用于- via\theadfont中的每个条目:\nline\nheadline

在此处输入图片描述

\documentclass{article}

\usepackage{makecell,etoolbox}

\makeatletter
\newcommand{\nheadline}{%
  \patchcmd{\@@@nline}% <cmd>
    {& \Num}% <search>
    {& \theadfont\Num}% <replace>
    {}{}% <success><failure>
  \theadfont\nline
}
\makeatother

\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{tabular}{ r *{6}{l} }
    & \nheadline{6} \\
    & \nline{6}     \\
  \hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz \\
\end{tabular}

\end{document}

添加对齐的问题在于你需要\multicolumn{1}{<align>}对每个元素使用,这必须是第一的在其使用的单元格内输入。以下示例定义\createheadline{<num>}创建包含每个标题元素的适当标题组合。您可以在标题之外使用它,或者(就像我做的那样)在标题中没有其他任何内容的第一个单元格中\multicolumn{1}{c}使用它。tabular

在此处输入图片描述

\documentclass{article}

\usepackage{makecell,multido}

\makeatletter

\newcommand{\headalign}{\multicolumn{1}{c}}
\newcommand{\createheadline}[1]{%
  \def\@@@headline{}%
  {%
  \let\headalign\relax
  \let\theadfont\relax
  \multido{\i=1+1}{#1}{%
    \edef\x{\noexpand\gdef\noexpand\@@@headline{\@@@headline & \headalign{\theadfont\i}}}\x%
  }%
  \expandafter\xdef\expandafter\@@@headline\expandafter{\expandafter\@gobble\@@@headline}%
  }%
}

\newcommand{\setheadline}{\@@@headline}

\makeatother

\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{tabular}{ r *{6}{l} }
  \createheadline{6}  & \setheadline \\
    & \nline{6} \\
  \hline
  a & & & I'd like to center the numbers above. & & & \\
  b & & & (they should also be bold and footnotesize) & & & \\
  c & foo bar & & like thead & & & baz
\end{tabular}

\end{document}

相关内容