2

I'm trying to create a new columntype with options as in the source below, and all option work fine, except for the b option, which gives this error message:

! Undefined control sequence.
\KV@xcol@b #1->\columncolor 
                            {#1}

When I use the \columncolor} directly, as in the second tabularx below, ther is no problem. Why?

\documentclass{article}
\usepackage[papersize={90mm,60mm},margin=5mm]{geometry}
\usepackage{array,xkeyval,xcolor,colortbl,tabularx,ragged2e}
\pagestyle{empty}\parindent0pt

% keys for columntype x:
\makeatletter
\define@key{xcol}{w}{\hsize=#1\hsize}       % relative width (summarize to 1)
\define@key{xcol}{f}{\leavevmode\color{#1}} % foreground color
\define@key{xcol}{b}{\columncolor{#1}}      % background color
\define@key{xcol}{r}[]{\RaggedLeft}         % justify right
\define@key{xcol}{l}[]{\RaggedRight}        % justify left (the default)
\define@key{xcol}{c}[]{\Centering}          % center
\makeatother

\newcolumntype{x}[1]{>{\RaggedRight\setkeys{xcol}{#1}}X}

\begin{document}

% the b=yellow should make \colorcolumn{yellow}, but \columncolor is undefined?
\begin{tabularx}{80mm}[t]{|x{f=blue,b=yellow,w=.7,c}|x{w=.3,r}|}\hline
Hello & World\\\hline
\end{tabularx}
\bigskip

% using \columncolor directly: no problem:
\begin{tabularx}{50mm}[t]{|>{\columncolor{yellow}}x{f=blue,w=.7,r}|x{w=.3}|}\hline
Hello & World\\\hline
\end{tabularx}

\end{document}
  • This might be similar to having \multicolumn as the first entry in a cell. Your setting of keys makes \columncolor not the first thing in the cell. – Werner Oct 22 '15 at 14:53
  • You can't use \columncolor in a cell. \columncolor{yellow} Hello would give the same error. As you hide it in a setkeys-command you are too late. Use \cellcolor. – Ulrike Fischer Oct 22 '15 at 14:53

1 Answers1

1

\columncolor isn't really a command that is executed so much as a marker for a place that tokens are inserted (in fact it is not defined at all as a command).

Somewhere in colortbl you find

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

...

\def\CT@extract#1\columncolor#2#3\@nil{%...

So \CT@extract needs to see an explicit \columncolor token, not hidden behind a macro or brace groups.

The reasons for doing it that way are somewhat obscure:-)

David Carlisle
  • 757,742