I want to write a macro in LaTeX2e that can pass \textcolor, which is embedded in another macro, the values of the colour model gray, rgb, or cmyk. The number of arguments specified implies the colour model to be used; 1 argument for gray, 3 for the rgb, and 4 for the cmyk model. I was able to write a macro that does what I want, but with the arguments in the standard manner in braces. This is the code I was able to write by modifying the answers here.
\makeatletter
\def\setmycolour#1{%
\@ifnextchar\bgroup%
{\docolour{#1}}
{\dogray{#1}}
}
\def\dogray#1{This is gray hue #1.}
\def\docolour#1#2#3{%
\@ifnextchar\bgroup%
{\docmyk{#1}{#2}{#3}}
{\dorgb{#1}{#2}{#3}}
}
\def\dorgb#1#2#3{This is rgb colour #1,#2,#3.}
\def\docmyk#1#2#3#4{This is cmyk colour #1,#2,#3,#4.}
\makeatother
I use the macro as
\setmycolour{0.85}\\
\setmycolour{1}{0}{0}\\
\setmycolour{1}{0}{0}{0}\
I want to use the macro, for example, as \setmycolour{1,0,0} or \setmycolour{0.85}. How do I parse the arguments in the macro definition to do this? Is the above code the best way to get the effect I want?
setmycolour@ito ensure that this macro call always succeeds but depending on the number of commas in#1, you might pass too many (which does not hurt). – Christian Lindig Apr 13 '11 at 15:27\def\setmycolour@i#1,#2,#3,#4,#5\@nilexpects at least 4 commas, the reason why i have t add these ones if there is only one argument, then #2#3#4#5 are all empty. When there are four comma separated arguments given, #5 collects all trhe rest, in this case three commas. #5 is like a garbage can. The other cases are between these two ones. – Apr 13 '11 at 15:34\ifx$#2$? This seems tricky as well. Is this evaluated as\ifx$$(true) when#2is empty? But why does it evaluate to false when#2is not empty? Could you have used something other than$? – Christian Lindig Apr 13 '11 at 18:10\ifx\relax#2\relaxfor example. – Apr 13 '11 at 18:43\expandafterin\setmycolourreally necessary? The code seems to work fine without it. Am I missing something? – Luis Costa Apr 18 '11 at 09:16\setmycolour{{0.5,0.6,0.7,0.8}}, which gives a wrong result without expanding it. – Apr 18 '11 at 10:09\expandafterwhen doing\setmycolour{{0.5,0.6,0.7,0.8}}; the grouping around the arguments isn't lost after the expansion. Or so it seems. – Luis Costa Apr 18 '11 at 12:43\ifx a#2awith "a" in place of "$". So when #2 is empty\ifxwill test left-hand "a" and right-hand "a" and evaluate to true and to false when testing left-hand "a" and "contents-of-#2" when #2 isn't empty whence the right-hand "a" is ignored, now it being part of the 'then' execution. – Luis Costa Apr 18 '11 at 14:52\ifx a#2awith#2not being empty, why is the right-handanot showing up somewhere in the output but seems to be consumed by\ifx? – Christian Lindig Apr 18 '11 at 17:36\ifxis\ifx<token1><token2> do if token test is true \fi, and a token is either a character (+ category code) or a control sequence (macro). I'm fairly new to TeX, so correct me if I'm wrong. When I replaced the character "a" for "$" and then again with "1" in your code above it worked fine both times. – Luis Costa Apr 20 '11 at 13:38\ifx a#2a true\else false\fias I already wrote, you get "atrue" instead of "true" if #2=a. for\ifx $#2$ true\else false\ficauses an error if #2=$. Only macros or active characters make some sense. – Apr 20 '11 at 14:52\ifx c#2#c hilly \else hilly\fi, far-fetched, I acknowlegde, but possible. – Luis Costa Apr 21 '11 at 08:13#2=ccand your test fails ... – Apr 21 '11 at 12:24\ifevaluates to true the code won't always work as desired when testing characters. – Luis Costa May 02 '11 at 11:30