尝试更改指定表格行的字体颜色时无法编译

尝试更改指定表格行的字体颜色时无法编译

我正在尝试更改整行文本的颜色tabular,而不使用该tabu包。我想使用这个答案

我做错了什么,导致以下 MWE 无法编译?我收到错误消息“LaTeX 错误:命令 @ 已定义”。

\documentclass[oneside,11pt]{book}

\usepackage[semibold,tt=false]{libertine}
\usepackage{libertinust1math}
\usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
\usepackage[subrefformat=parens]{subcaption}
\usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
]{microtype}
\usepackage{booktabs}
\usepackage{acro}
\usepackage{siunitx}
\sisetup{%
  detect-family, detect-shape,
  product-units = power,
  list-final-separator = {, and },
  retain-explicit-plus,
  input-comparators = {<=>\approx\ge\geq\gg\le\leq\ll\sim\lesssim\gtrsim}
}
\DeclareSIUnit[number-unit-product = ]\percent{\char`\%}

% Code from https://tex.stackexchange.com/a/26364/15622
\newcommand*{\@rowstyle}{}
\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\@rowstyle{#1}%
  \@rowstyle\ignorespaces%
}
\newcolumntype{=}{% resets the row style
  >{\gdef\@rowstyle{}}%
}
\newcolumntype{+}{% adds the current row style to the next column
  >{\@rowstyle}%
}
\makeatother

\begin{document}

\begin{table}
  \centering
  \begin{tabular}{=l +l +l}
    \toprule
    A & {B} & {C}\\
    \midrule
    \rowstyle{\color{red}}
    Bob & \SI{75}{\percent} & -1.11\\
    Carla & \SI{75}{\percent} & 2.22\\
    Dale & \SI{75}{\percent} & -3.33\\
    Ena & \SI{75}{\percent} & 4.44\\
    \bottomrule
  \end{tabular}
  \caption{A}
\end{table}

\end{document}

答案1

评论太长了。我希望当需要为表格的选定行、其余行甚至更多行着色而无需重复指定颜色时,以下方法可以成为一种替代方法。

输出

在此处输入图片描述

值得一提的是,该解决方案只能与 pdfTeX 一起使用。此外,还有一些其他限制,但在许多情况下,该方法效果很好。

使用方法

只需定义您喜欢的 rgb 颜色规范,例如,使用。此外,在行的开头(或通常在行中的某个位置)\def\mycolor{.6 0 .8}使用命令为行的其余部分着色。要应用定义的颜色,请写入。如果您需要一次为多行着色,最好使用命令和关闭着色。\rowclr\rowclr\mycolor\startclr\mycolor\stopclr

代码

\documentclass[oneside,11pt]{book}

\usepackage[semibold,tt=false]{libertine}
\usepackage{libertinust1math}
\usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
\usepackage[subrefformat=parens]{subcaption}
\usepackage[
  expansion = false ,
  tracking = smallcaps ,
  letterspace = 40 ,
]{microtype}
\usepackage{booktabs}
\usepackage{acro}
\usepackage{siunitx}
\sisetup{%
  detect-family, detect-shape,
  product-units = power,
  list-final-separator = {, and },
  retain-explicit-plus,
  input-comparators = {<=>\approx\ge\geq\gg\le\leq\ll\sim\lesssim\gtrsim}
}
\DeclareSIUnit[number-unit-product = ]\percent{\char`\%}


% declare your favorite rgb color specifications here
\def\mycolor{.6 0 .8}
\def\othercolor{1 .2 .5}
\def\somecolor{.5 .8 .3}

\def\stopclr{\pdfliteral{0 g}}
\def\startclr#1{\pdfliteral{#1 rg}}

% color the 'rest' of the row
\def\rowclr#1#2\\{\startclr#1#2\stopclr\\}



\begin{document}

\begin{table}
  \centering
  \begin{tabular}{ccc}
    %------------------------------------
    \toprule
    A            & B        & C\\
    \midrule
    %------------------------------------
    \rowclr\mycolor
    Bob          & \SI{75}{\percent}    & -1.11\\
    %------------------------------------
    Carla        & \startclr\othercolor \SI{75}{\percent}& 2.22\\
    %------------------------------------
    Dale\stopclr & \SI{75}{\percent}    & -3.33\\
    %------------------------------------
    Ena          & \rowclr\somecolor\SI{75}{\percent}    & 4.44\\
    %------------------------------------
    \bottomrule
  \end{tabular}
  \caption{A}
\end{table}

\end{document}

答案2

在此处输入图片描述

\documentclass[oneside,11pt]{book}
\usepackage{xcolor}

%\usepackage[semibold,tt=false]{libertine}
%\usepackage{libertinust1math}
%\usepackage[font={sf,small},labelsep=quad,labelfont=sc]{caption}
%\usepackage[subrefformat=parens]{subcaption}
%\usepackage[
%  expansion = false ,
%  tracking = smallcaps ,
%  letterspace = 40 ,
%]{microtype}
\usepackage{booktabs}
%\usepackage{acro}
\usepackage{siunitx}
\sisetup{%
  detect-family, detect-shape,
  product-units = power,
  list-final-separator = {, and },
  retain-explicit-plus,
  input-comparators = {<=>\approx\ge\geq\gg\le\leq\ll\sim\lesssim\gtrsim}
}
\DeclareSIUnit[number-unit-product = ]\percent{\char`\%}

% Code from https://tex.stackexchange.com/a/26364/15622
\makeatletter
\newcommand*{\@rowstyle}{}
\newcommand*{\rowstyle}[1]{% sets the style of the next row
  \gdef\@rowstyle{#1}%
  \@rowstyle\ignorespaces%
}
\newcolumntype{=}{% resets the row style
  >{\gdef\@rowstyle{}}%
}
\newcolumntype{+}{% adds the current row style to the next column
  >{\@rowstyle}%
}
\makeatother

\begin{document}

\begin{table}
  \centering
  \begin{tabular}{=l +l +l}
    \toprule
    A & {B} & {C}\\
    \midrule
    \rowstyle{\color{red}}
    Bob & \SI{75}{\percent} & -1.11\\
    Carla & \SI{75}{\percent} & 2.22\\
    Dale & \SI{75}{\percent} & -3.33\\
    Ena & \SI{75}{\percent} & 4.44\\
    \bottomrule
  \end{tabular}
  \caption{A}
\end{table}

\end{document}
t

相关内容