0

This question already has an answer here:

I want to create a new command to set the column colour, but it's causing the following error:

! Undefined control sequence.
\redcolumn ->\columncolor 
                          {red}
l.10 A & B
           \\

I don't know if it's because I'm calling a command with a parameter or because of where I'm trying to call the new command, but here's the source code:

\documentclass[letterpaper,20pt]{extarticle}
\usepackage[table,xcdraw]{xcolor}
\newcommand{\redcolumn}{\columncolor{red}}
\begin{document}
\begin{tabular}{l >{\columncolor{red}}l}
A & B \\
\end{tabular}

\begin{tabular}{l >{\redcolumn}l}
A & B \\
\end{tabular}

\end{document}

The first tabular uses \columncolor, and it works. The second uses my new command, and it fails. How have I misunderstood \newcommand?

marked as duplicate by David Carlisle macros Nov 17 '15 at 8:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Thanks to Jérôme's comment, I was able to find a solution: use \newcolumntype instead of \newcommand:

\documentclass[letterpaper,20pt]{extarticle}
\usepackage[table,xcdraw]{xcolor}
\newcolumntype{R}{>{\columncolor{red}}l}
\begin{document}
\begin{tabular}{l >{\columncolor{red}}l}
A & B \\
\end{tabular}

\begin{tabular}{l R}
A & B \\
\end{tabular}

\end{document}

It's probably weird to use R for a left-aligned column, but it stands for red.

Not the answer you're looking for? Browse other questions tagged or ask your own question.