指定表格第一行的颜色(在“newenvironment”中)

指定表格第一行的颜色(在“newenvironment”中)

我正在为表格使用一种特定的样式,并希望自动指定第一行文本的颜色。

目前,我必须手动指定行中每个单元格的文本的颜色。

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}
\usepackage{booktabs}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\newsavebox{\dataTableContent} % Box
\newenvironment{dataTable}[1] % \new environment
{%
\begin{lrbox}{\dataTableContent}%
\begin{tabular}{#1}}%
%
{%
\end{tabular}
\end{lrbox}
\begin{tikzpicture}
\node [inner xsep=0pt] (tbl){\usebox{\dataTableContent}};
\begin{pgfonlayer}{background}
% table
\draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black]
(tbl.north east) rectangle (tbl.south west);
% top line
\draw[rounded corners=1pt,top color=gray!10!black,bottom color=gray!50!black,draw=black]%
($(tbl.north west)$) rectangle ($(tbl.north east)-(0,1.5\baselineskip)$);
% bottom rule
\draw[rounded corners=0.25pt,fill=gray,draw=black]%
(tbl.south west) rectangle ($(tbl.south east)+(0,0)$);
\end{pgfonlayer}
\end{tikzpicture}}
\begin{document}
\begin{dataTable}{lc}
\color{white}Rien&\color{white}tout\\\midrule
si&non
\end{dataTable}
\end{document}

例子

使用经典命令创建表的想法:

\begin{dataTable}{lc}
Rien&tout\\\midrule
si&non
\end{dataTable}

感谢您的帮助。

答案1

您可以定义一个宏对,将颜色命令添加到第一行的每个单元格:

示例输出

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}

\usepackage{booktabs}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\usepackage{etoolbox}
\def\head#1\\{\shead#1&\\}
\def\shead#1&#2\\{\color{white}#1\ifstrempty{#2}{\\}{&\shead#2\\}}

\newsavebox{\dataTableContent} % Box
\newenvironment{dataTable}[1] % \new environment
{%
\begin{lrbox}{\dataTableContent}%
\begin{tabular}{#1}\head}%
%
{%
\end{tabular}
\end{lrbox}
\begin{tikzpicture}
  \node [inner xsep=0pt] (tbl){\usebox{\dataTableContent}};
  \begin{pgfonlayer}{background}
    % table
    \draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black]
    (tbl.north east) rectangle (tbl.south west);
    % top line
    \draw[rounded corners=1pt,top color=gray!10!black,bottom color=gray!50!black,draw=black]%
    ($(tbl.north west)$) rectangle ($(tbl.north east)-(0,1.5\baselineskip)$);
    % bottom rule
    \draw[rounded corners=0.25pt,fill=gray,draw=black]%
    (tbl.south west) rectangle ($(tbl.south east)+(0,0)$);
  \end{pgfonlayer}
\end{tikzpicture}}

\begin{document}

\begin{dataTable}{lcr}
Rien&tout&plus\\\midrule
si&non&oui\\
non&non&non
\end{dataTable}

\end{document}

我定义了一个命令\head,它位于环境tabular内部的开头dataTable。它调用辅助命令\shead来插入\color{white}命令,使用纯 TeX 提供的模式匹配\def

相关内容