I would like to use simple macros within tikz commands, but it seems tikz doesn't expand them in the ways I would expect.
For instance, the code
\pgfpointcurveattime{.25}{\pgfpoint{0cm}{0cm}}{\pgfpoint{0cm}{1cm}}{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{0cm}}
nicely returns the point one quarter of the way along the bezier curve specified by those four points. However the code
\newcommand{\bcurve}{{\pgfpoint{0cm}{0cm}}{\pgfpoint{0cm}{1cm}}{\pgfpoint{1cm}{1cm}}{\pgfpoint{1cm}{0cm}}}
\pgfpointcurveattime{.25}\bcurve
gives an error, even though it expands to the same string. Putting braces around \bcurve doesn't help.
What is the cause of this trouble, and more generally how can you use a macro in such a situation in tikz?
Best Answer
My knowledge of TeX expansion is rather limited, but I think the problem is not with PGF but with TeX itself. It tries to expand
\pgfpoincurveattime
first and looks for its arguments before expanding\bcurve
. Hence\bcurve
is taken as one of the arguments of\pgfpointcurveattime
and TeX looks for the other arguments in the text that follows.So what you want to do, is to expand
\bcurve
first (once). To change the order of expansion, one uses\expandafter
:\expandafter\a\b
expands\b
and adds the result after\a
and then continues on to process\a
. The problem here is that you want to skip over five tokens ({.25}
), which would require a lot of\expandafter
s:After the execution of the
\expandafter
s, this will leave TeX withOne possible solution to save on typing all those
\expandafters
and getting more readable code is to save.25
into a macro that can be skipped over in one step:After the execution of the
\expandafter
s, this will leave TeX with