# Using \columncolor outside column definition

I'm trying to create a command, which calls on an environment, which defines a custom column definition with one argument using \newcolumntype, and defines a second command, containing parameters for a table column. Then, still within the first command, I begin the tabular environment and call the custom column, with the second command as its argument. I always get an undefined control sequence error, though. Any thoughts on why that might be?

I'm sure that was clear as mud, but I've cut it down to a minimal example, which follows:

\documentclass{article}
\usepackage{array}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{colortbl}

\newenvironment{tablecolor}{
\newcommand{\tlight}{\columncolor{SkyBlue!30}\color{black}}
\newcolumntype{C}[1]{>{##1}c}
}{}

\newcommand{\tablegoals}{
\begin{tablecolor}
\begin{tabular}{l|C{\tlight}}
Goal 7  & \\
\end{tabular}
\end{tablecolor}
}

\begin{document}
\tablegoals
\end{document}


Here's my second attempt. Different approach, same undefined control sequence error with \columncolor.

\documentclass{article}
\usepackage{array}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{colortbl}
\usepackage{ifthen}

\newenvironment{tablecolor}{
\newcolumntype{C}[1]{>{
\ifthenelse{\equal{dark}{##1}}{
\columncolor{Cyan}\color{white}
}{
\columncolor{SkyBlue!30}\color{black}
}
}c}
}{
}

\newcommand{\tablegoals}[1]{
\begin{tablecolor}
\begin{tabular}{l|C{dark}}
Goal 7  &   #1
\end{tabular}
\end{tablecolor}
}

\begin{document}
\tablegoals{Check mark}
\end{document}

-
Welcome to TeX.SE. Thanks for providing a MWE, that really helps. However, in this case I am not clear what you want, even after reading the comments in the other answer. I would suggest you edit this question to do a table that has ONE of the types that you want and then document that your would like to be able to reuse the same table and just change the specific colors. Then we could help you with the \newcommand –  Peter Grill Nov 13 '11 at 4:33
I would like to be able to quickly generate the same table, but I'd also like to be able to generate tables with different layouts employing the same color scheme, as well as a greyscale color scheme. –  volfied Nov 13 '11 at 5:18

Wow, you've got a bit of a mess on your hands there! First off, you don't need that new environment, or at least you don't need those commands in it. Try this:

\documentclass{article}
\usepackage{array}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{colortbl}
\newenvironment{tablegoals}[1]
{\begin{tabular}{l|>{\columncolor{#1}}c}}
{\end{tabular}}

\begin{document}
\begin{tablegoals}{SkyBlue!30}
Goal 7  & Something \\
\end{tablegoals}
\end{document}


The \newwnvironment here takes the name of your new environment as the first argument, the stuff that comes before the content of your environment as the second, and the stuff at the end your environment as the third. Thus the second argument contains the \begin{tabular}{...} including the colour definition of the second column. The third argument simply ends the table. There is no need to define any new commands, since this new environment allows you to type all this only once anyway.

If you find yourself defining a lot of tables with different column colours, then perhaps the following newcommand can help:

\newcommand{\newcolumnwithcolor}[2]{\newcolumntype{#1}{>{\columncolor{#2}}c}}


Now in your preamble you can define lots of column types quickly and readably

\newcolumnwithcolor{C}{SkyBlue!30}


which you may use in a tabular environment.

\begin{tabular}{l|C}
Goal 7  & Something \\
\end{tabular}


Note that is seems that these may only be named with a single character.

Finally, you can define a special column that takes arguments. If you use the same column definition and only change the colour for example, then the following works:

\newcolumntype{D}[1]{>{\columncolor{#1}}c}

\begin{tabular}{|l|D{SkyBlue!10}|}
Goal 7  & Something \\
\end{tabular}


The number of additional arguments may be more than one, so you can add alignment as an option etc.

-
Well, I did simplify to the extent that some of it makes no real sense. I'm trying to save myself the trouble of repeating things over and over. I need the ability to make a color table and a B&W table. The environment would redefine the parameters for the dark and light colors (blue, light blue; gray, white), along with arrayrulecolors. I'm not sure my method is the most efficient way to do it, but I'd like the flexibility to start an environment that changes the table to color or B&W, then start tabular and not have to muck around with lots of > commands. Am I making sense? –  volfied Nov 13 '11 at 3:50
I'm not sure I'm following. Why not define two tables like the above, one with colour and one without? Or you could add additional parameters to take a colour as part of the environment. –  qubyte Nov 13 '11 at 3:56
And if I want to use \newcolumntype, I just need to define separate ones for dark and light, lcr of each? I guess I was hoping to avoid that, since the column names can only be one character and they start getting completely arbitrary. Entirely possible I'm not making my life any less complicated this way, of course. –  volfied Nov 13 '11 at 3:56
Well, you don't need to define a new column type at all if you're using custom environments. You'll have to write it all once of course. I have the feeling that we're not on the same page though... –  qubyte Nov 13 '11 at 3:59
While I'm sure you're not inclined to wade through a ton of code, here's the full context, if you've interested and have the time. I appreciate whatever help you can offer. dl.dropbox.com/u/1716280/indiana.sty dl.dropbox.com/u/1716280/8-brumback.tex –  volfied Nov 13 '11 at 4:02

I asked David Carlisle, who wrote both the colortbl package and \newcolumntype in the array package about this problem. Here's his response:

the error message is telling you that \columncolor is not defined and in fact looking at colortbl.sty there is no definition for such a command, it is just used as a token in

\expandafter\CT@extract\the\toks\@tempcnta\columncolor!\@nil


which means essentially that after the normal array package table processing has done its stuff, colortbl goes in finds that token as a marker and inserts some extra stuff.

I can't remember why it was written that way (the log says it's 15 years ago:-) but probably there was a reason at the time.

The result (which is probably not documented too well) is that if you hide \columncolor in a macro it won't work, it has to be there explicitly at the top level. You could use newcolumntypes defining one color or the other, or taking a parameter taking the colour, as they are expanded out by array.sty before colortbl starts looking for its tokens.

-