在 \toprule 和 \midrule 之间填充

在 \toprule 和 \midrule 之间填充

我已经尝试过一种方法,但遇到了问题(清除中间行的颜色),现在我开始有了更多。因此,与其尝试修复这种方法,我认为值得看看 TeX-ers 可能会提出哪些更多经验。

我试图让所有表格中 \toprule 和 \midrule 之间的行都填充某种颜色,以创建如下效果:

演示图像

有人对如何做到这一点有什么建议吗?对于我目前(有缺陷的)方法,请参阅清除中间行的颜色

眼镜: 使用 pdflatex 和一系列软件包,包括 booktabe 和 xcolor(带有选项table

答案1

和 都会\toprule增加\midrule一些垂直空间,因此\rowcolor无法完全填充明显的单元格空间,但您可以使用来\specialrule制作特别指定规则:

姆韦

\documentclass{article}
\usepackage{geometry}
\usepackage[table]{xcolor}
\usepackage{booktabs,tabularx}
\renewcommand\toprule{\specialrule{1pt}{1pt}{0pt}\rowcolor{gray!10}}
\renewcommand\midrule{\specialrule{0.4pt}{0pt}{0pt}}
\begin{document}
\renewcommand\arraystretch{1.4}\tabcolsep3pt
\begin{tabularx}{\linewidth}{p{6em}XXXXXXXXXXX}
\toprule
\rowcolor{gray!10}  
Typeface\par \verb|\selectfont| & 
Bold\par  \verb|b| & 
Semibold\par  \verb|sb|& 
Medium\par \verb|mb|& 
Text\par \verb|tx|& 
Regular\par \verb|m|& 
Light\par \verb|l|& 
Extra L\par \verb|el|& 
Thin\par \verb|t|\\
\midrule
Plex Serif & Words & Words & Words & Words 
& Words & Words & Words & Words \\
Plex Sans & \sffamily Words & Words & Words 
& Words & Words & Words & Words & Words\\
\bottomrule
\end{tabularx}
\end{document}

答案2

这是一个使用的解决方案卡路里。 自从卡路里单元格命令在关闭之前一直有效,因此很容易为行着色。只需使用\gray我使用的定义定义您需要的每种颜色的命令即可。在您想要灰色的第一个单元格之前打开\gray(或您定义的任何其他颜色),并在您想要正常背景时将其关闭(相同的命令)。

使用背景颜色来强调行,和toprulemidrule不必要的(我在这里没有关闭它们)。我的表格看起来不同,因为我没有使用不同粗细的 Plex Serif 和 Plex Sans。

为了避免第二行标题不对齐,我已\vfil在单元格中明确将其对齐。此外,我已加载包逐字框允许在单元格中使用 verbatimematerial。

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage{cals, xcolor}
\usepackage{verbatimbox}
\usepackage[margin=2.5cm]{geometry}

\newsavebox\myVerb
\newcommand*{\verbBox}{\usebox\myVerb}

\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\begin{document}

\begin{calstable}

\begin{verbbox}[\footnotesize]
\selectfont
\end{verbbox}

% Defining 3 column relativ to each other and relativ to the margins
\colwidths{{\dimexpr(\columnwidth)/90*12\relax}
            {\dimexpr(\columnwidth)/90*9\relax}
            {\dimexpr(\columnwidth)/90*11\relax}
            {\dimexpr(\columnwidth)/90*10\relax}
            {\dimexpr(\columnwidth)/90*10\relax}
            {\dimexpr(\columnwidth)/90*10\relax}
            {\dimexpr(\columnwidth)/90*10\relax}
            {\dimexpr(\columnwidth)/90*10\relax}
            {\dimexpr(\columnwidth)/90*8\relax}
            }
% The tabular fills the text area

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0pt}
\def\cals@bodyrs@width{0.4pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{}

\def\gray{\ifx\cals@bgcolor\empty
\def\cals@bgcolor{gray!30}
\else \def\cals@bgcolor{} \fi}

\def\tb{\ifx\cals@borderT\relax     % Top border switch (off-on)
    \def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}

\def\bb{\ifx\cals@borderB\relax     % Botton border switch (off-on)
    \def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

\def\rb{\ifx\cals@borderR\relax     % Right border switch (off-on)
    \def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}

\def\lb{\ifx\cals@borderL\relax     % Left border switch (off-on)
    \def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}


% R1
\thead{
\brow
    \gray\bb\lb\rb\cell{Typeface}
    \cell{Bold}
    \cell{Semibold}
    \cell{Medium}
    \cell{Text}
    \cell{Regular}
    \cell{Light}
    \cell{Extra L}
    \cell{Thin}\bb
\erow
% R2 Body
\brow
    \ttfamily\footnotesize\cell{\vfil\theverbbox}
    \cell{\vfil b}
    \cell{\vfil sb}
    \cell{\vfil mb}
    \cell{\vfil tx}
    \cell{\vfil m}
    \cell{\vfil l}
    \cell{\vfil el}
    \cell{\vfil t}\rmfamily\small\gray
\erow
}
% R3 Body
\brow
    \bb\cell{Plex Serif}
    \cell{\bfseries Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
\erow
% R4 Body
\brow
    \itshape\cell{Plex Serif}
    \cell{\bfseries Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
\normalfont
\erow
% R5 Body
\brow
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
% R6 Body
\brow
    \sffamily\cell{Plex Sans}
    \cell{\bfseries Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
\erow
% R7 Body
\brow
    \itshape\cell{ Plex Sans}
    \cell{\bfseries Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}
    \cell{Word}\normalfont
\erow
\makeatletter
\end{calstable}\par % \par needed to align the tabular

\end{document}

答案3

环境{NiceTabular}具有nicematrix以与之兼容的方式为单元格、行和列着色的工具booktabs

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}
\begin{NiceTabular}{cc}
\CodeBefore
  \rowcolor{gray!20}{1,2}
\Body
  \toprule
  Typeface & Bold \\
  \verb|\selectfont| & b \\
  \midrule
  Plex Serif & \textbf{Words} \\
  \bottomrule
\end{NiceTabular}
\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容