Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have some text that I want to change colour depending on the current state of a counter. But I can't get this file to compile. (This is obviously simplified, since the texcolour command is actually part of a command defined in the preamble...)

\documentclass{article}
\usepackage{xcolor}
\newcounter{foo}
\setcounter{foo}{30}
\begin{document}
\textcolor[RGB]{0,\value{foo},0}{Text in FOO colour}
\end{document}

I get the following error repeated:

! Missing number, treated as zero.
<to be read again> 
                   \
l.8 ...r[RGB]{0,\value{foo},0}{Text in FOO colour}

! Illegal unit of measure (pt inserted).
<to be read again> 
                   \
l.8 ...r[RGB]{0,\value{foo},0}{Text in FOO colour}

I'm sure there's some subtlety I've not grasped yet... But \vskip \value{foo} pt compiles fine and gives me space depending on the value of the counter, so I can't be far off...

share|improve this question
    
The RGB model requires numbers between 0 and 1 for the components. So, I would not use a counter. –  Stefan Kottwitz Dec 15 '10 at 18:17
2  
Actually, the rbg model requires numbers between 0 and 1. The RGB model requires integers between 0 and \rangeRGB, which defaults to 255. –  Jan Hlavacek Dec 15 '10 at 19:09
    
The actual command uses red!\value{foo}!blue . But as @Jan says, RGB uses numbers up to 255 –  Seamus Dec 15 '10 at 22:30
    
Ah, I see. I just thought of the color package, but xcolor provides also that integer model. –  Stefan Kottwitz Dec 16 '10 at 13:24

1 Answer 1

up vote 7 down vote accepted

Insert \the before \value; then it works.

As to why you have to include \the: The culprit is \@onelevel@sanitize that is called inside \XC@edef. What?? Let me try and explain. At some point, your \textcolor command makes the call

\XC@edef\@@clr{0,\value{foo},0}

This in turn calls

\edef\@@tmp{0,\value{foo},0}

(with some trickery regarding active characters), thus making \@@tmp contain 0,\c@foo,0 and not 0,30,0: The \edef doesn't expand the counter \c@foo to it contents. Immediately after this, \XC@edef calls

\@onelevel@sanitize\@@tmp

which in turn expands to

\edef\@@tmp{\expandafter \strip@prefix \meaning \@@tmp}

And this does something horrible: It turns the 5 tokens 0,\c@foo,0 into the 11 tokens 0,\c@foo ,0, i.e., the control sequence \c@foo is turned into a sequence of single characters (all with catcode 12, except the space). Now you've got your explanation for the strange error message

! Missing number, treated as zero.
<to be read again> 
                   \

The \ is the first character of \c@foo! And it's not a number, indeed.

If you could follow up to this point, then it's easy to understand why \the helps out here:

\edef\@@tmp{0,\the\value{foo},0}

makes \@@tmp contain 0,30,0, and no evil can happen anymore.

share|improve this answer
    
Though it does work with \the, Stefan's comment to the question makes me wonder if my answer is stupid. –  Hendrik Vogt Dec 15 '10 at 18:25
    
@Hendrik see the above comments on why it is not a stupid idea! –  Seamus Dec 15 '10 at 22:38
1  
Also, could you explain why you need a \the? I thought \value expanded to the value, which is what you wanted for the argument for the colour command... I'm not sure I understand what's going on here –  Seamus Dec 15 '10 at 22:38
    
@Seamus: Well, that's a tough one. I got my answer just guessing, along the lines "Oh, let's see what happens if ...". I didn't include an explanation since I had none. It now took me ages to find it: I'll try and write something. –  Hendrik Vogt Dec 16 '10 at 12:45
    
@Hendrik Thanks for the explanation! I can't say I fully understand it, but at least I know I wasn't missing anything obvious. I learned a new bit of TeX lore: if in doubt, try \the... –  Seamus Dec 16 '10 at 13:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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