2

I am trying to change the text color of an entire row of a tabular, without using the tabu package. I want to employ this answer.

What am I doing wrong such that the following MWE does not compile? I get the error message, "LaTeX Error: Command @ already defined."

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

Andrew
  • 1,249

2 Answers2

2

Too long for a comment. I hope that the following approach can be an alternative when one needs to color the selected rows of your table, the rests of the rows, or even more rows without repeating the color specification.

Output

enter image description here

It is important to mention that the solution can be used with pdfTeX only. Also, there are some other limitations, but in many cases the approach works fine.

Usign the approach

Simply define your favorite rgb color specification, e.g., by using \def\mycolor{.6 0 .8}. Further, use the command \rowclr at the beginning of the row (or, generally, somewhere in the row) to color the rest of the row. To apply the defined color, write \rowclr\mycolor. If you need to color more rows at once, it is better to use the commands \startclr\mycolor and \stopclr to turn-off the coloring.

Code

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

Marian G.
  • 1,951
  • 1
  • 9
  • 14
0

enter image description here

\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

js bibra
  • 21,280
  • 1
    @Andrew have alook at the revised code -- i have commented out certain packages and get the desired output -- you can uncomment packages as per choice -- other than \makeatletter in the code you were also missing out on the xcolor package – js bibra Nov 22 '20 at 03:12