Nie można skompilować podczas próby zmiany koloru czcionki w określonych wierszach tabeli

Nov 22 2020

Próbuję zmienić kolor tekstu w całym wierszu tabular, bez korzystania z tabupakietu. Chcę zastosować tę odpowiedź .

Co robię źle, żeby nie skompilować poniższego MWE? Otrzymuję komunikat o błędzie „Błąd LaTeX: polecenie @ już zdefiniowane”.

\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}

Odpowiedzi

2 MarianG. Nov 22 2020 at 15:33

Za długo na komentarz. Mam nadzieję, że poniższe podejście może być alternatywą, gdy trzeba pokolorować wybrane wiersze tabeli, pozostałe wiersze lub nawet więcej wierszy bez powtarzania specyfikacji kolorów.

Wynik

Należy wspomnieć, że rozwiązanie to może być używane tylko z pdfTeX. Istnieją również inne ograniczenia, ale w wielu przypadkach podejście działa dobrze.

Stosuj podejście

Po prostu zdefiniuj swoją ulubioną specyfikację kolorów rgb, np \def\mycolor{.6 0 .8}. Używając . Ponadto użyj polecenia \rowclrna początku wiersza (lub ogólnie gdzieś w wierszu), aby pokolorować pozostałą część wiersza. Aby zastosować zdefiniowany kolor, napisz \rowclr\mycolor. Jeśli chcesz pokolorować więcej wierszy naraz, lepiej użyć poleceń \startclr\mycolori \stopclrwyłączyć kolorowanie.

Kod

\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}
jsbibra Nov 22 2020 at 10:10

\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