# LaTeX doesn't recognize \columncolor from the colortbl package when when it's in a command

LaTeX doesn't recognize the \columncolor command from the colortbl package when when it's in a commmand.

Here is an example

\documentclass{article}
\usepackage{colortbl}
\newcommand{\cBlue}{\columncolor[rgb]{0,.6,1}}
%\newcommand{\Blue}{\color[rgb]{0,.6,1}}
\begin{document}
\begin{tabular}{ >{\cBlue} cc}
Blue&White\\
Blue&White
\end{tabular}

\end{document}


I get the error

! Undefined control sequence.
\cBlue ->\columncolor
[rgb]{0,.6,1}
l.7   B
lue&White\\
?


Interestingly, htlatex sails through: it gives the expected output of blue colored column.

Note that there is no problem if I replace \columncolor by \color.

Also, \rowcolor is also fine, as you can test it on this example

\documentclass{article}
\usepackage{colortbl}
%\newcommand{\cBlue}{\columncolor[rgb]{0,.6,1}}
\newcommand{\rBlue}{\rowcolor[rgb]{0,.6,1}}
%\newcommand{\Blue}{\color[rgb]{0,.6,1}}

\begin{document}
\begin{tabular}{cc}
\rBlue Blue&Blue\\
White&White
\end{tabular}

\end{document}

• welcome to my world:-) That's a feature of the package, yes. – David Carlisle Dec 13 '16 at 19:51
• Can you explain? Why doesn't \rowcolor or \color need this feature? Why is this feature needed? Isn't it useful to define private mnemonic commands? – Máté Wierdl Dec 13 '16 at 20:12
• i'll add a few more words to my answer ... – David Carlisle Dec 13 '16 at 20:18

As the error message correctly shows, \columncolor is not defined by the colortbl package.

It is just used as a placeholder in a modified array preamble parser so that the colour specification can be extracted and re-inserted where needed.

Depending on the actual use case you can parameterise the colour used by defining a columntype or by defining a colour name

\documentclass{article}
\usepackage{colortbl}

\newcolumntype{B}{>{\columncolor[rgb]{0,.6,1}}}
\definecolor{myblue}{rgb}{0,.6,1}

\begin{document}
\begin{tabular}{ Bcc>{\columncolor{myblue}}c}
Blue&White&Blue\\
Blue&White&Blue
\end{tabular}

\end{document}


\color and \rowcolor are defined TeX commands that act at the point they are used. \columncolor (because it seemed a good idea at the time sometime in the last millennium) doesn't act at that point (or act at all, there is no command of that name, the internals use it in a similar way to the way [] are used to delimit optional arguments, to delimit a colour specification that is extracted and used in the table internals.

Use it this way:

\documentclass{article}
\usepackage[table]{xcolor}
\newcolumntype{B}{>{\columncolor[rgb]{0,.6,1}}c}
\begin{document}
\edef\cBlue{}
\begin{tabular}{ B c }
Blue&White\\
Blue&White
\end{tabular}

\end{document}

• Excellent, thank you. And this also works with htlatex. – Máté Wierdl Dec 13 '16 at 20:30