4

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}
David Carlisle
  • 757,742
volfied
  • 151
  • 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 04: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 05:18

2 Answers2

3

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.

Torbjørn T.
  • 206,688
volfied
  • 151
2

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.

Moriambar
  • 11,466
qubyte
  • 17,299
  • 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 03: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 03: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 03: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 03: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.

    http://dl.dropbox.com/u/1716280/indiana.sty
    http://dl.dropbox.com/u/1716280/8-brumback.tex

    – volfied Nov 13 '11 at 04:02
  • I'll take a quick look. In the mean time I've made the colour an extra argument in my example above. Any help? – qubyte Nov 13 '11 at 04:05
  • That's certainly closer to what I'm trying to achieve. Thing is, once I've expanded everything out so that I have the flexibility I'm after, it'll be unmanageably complex again. I'll keep plugging away at it with this method, though. We'll see. – volfied Nov 13 '11 at 04:11
  • Do you have a mock-up of how you want it to look? Keep in mind that flexibility = general, and general = unimaginably complex. ;) – qubyte Nov 13 '11 at 04:13
  • I'm still working through it, but it should wind up looking like this: http://dl.dropbox.com/u/1716280/8%20%28Brumback%29%20Power%20-%20Leaders%2C%20legacy%2C%20leverage.odt – volfied Nov 13 '11 at 04:14
  • Every table will be the same? I can throw in the colour definitions for the first row as well if you like. – qubyte Nov 13 '11 at 04:15
  • That one table will appear regularly, but I need the flexibility to create color or b&w tables with other layouts as the need arises. Sorry I can't take this into chat. Don't have the privilege yet. I'm starting to think I might need to use ifthen to make this work right. – volfied Nov 13 '11 at 04:17
  • I added another convenience command that may at least make life a little simpler. – qubyte Nov 13 '11 at 04:36
  • Here's another way of looking at the problem. Different approach, same result. I'll probably just ditch the flexibility and go with less natural column names, unless there's some solution to this undefined control sequence with \columncolor that keeps popping up.

    I guess I have to edit the original.

    – volfied Nov 13 '11 at 05:14
  • Is it possible that the solution involves some judicious \expandafter's? I feel like the \columncolor command is getting invoked out of place somehow. – volfied Nov 13 '11 at 05:23
  • Just call them A, B, C etc. and comment your code. – qubyte Nov 13 '11 at 05:35
  • Yeah, I just finished doing that and it did work. I could even use commands as arguments with \newcolumntype. I really don't know what's causing \columncolor to become undefined like that. Might ask the developer. Thanks so much for all your help. – volfied Nov 13 '11 at 05:40
  • I see your issue now. I'll have a think about what's going on. – qubyte Nov 13 '11 at 05:42