# How TikZ uses $for calculations of relative coordinates? The following code \node at ($(tag-node) + (2.5,0)$) allows to place a node by calculating $(tag-node) + (2.5,0)$. Does TikZ do that by changing the catcode of $ ?

• – minhthien_2016 Oct 19 at 14:34
• The short answer is that TikZ uses its own parser. Everything from \path to ; basically ignores LaTeX protocols. – John Kormylo Oct 19 at 15:27

No, TikZ doesn't change the \catcode of $. Indeed, if you try to change yourself the \catcode of $ TikZ will give an error:

! Package tikz Error: Sorry, some package has redefined the meaning of the math
-mode dollar sign. This is incompatible with tikz and its calc library and migh
t cause unrecoverable errors.


TikZ assumes that $ is a math-shift character and uses a parser to check for that. In tikz.code.tex there is this code: \def\tikz@@@scan@@absolute#1({% \tikz@ensure@dollar@catcode \pgfutil@ifnextchar{$}%${\tikz@parse@calculator#1(} {\tikz@scan@no@calculator#1(}% }%  The \tikz@ensure@dollar@catcode is responsible for giving the error above in case \catcode$ != 3. Then \pgfutil@ifnextchar{$} checks if the character following the ( is a $, and uses \tikz@parse@calculator in that case.

In tikzlibrarycalc.code.tex there is the definition of \tikz@parse@calculator:

\def\tikz@parse@calculator#1(${%$
\def\tikz@cc@command{#1}%
\begingroup%
%
% Parse main computation. It's a series of optional factors in front
% of coordinates.
%
\pgf@xa=0pt% We accumulate the result in here.
\pgf@ya=0pt%
\tikz@cc@parse+%
}%

\def\tikz@cc@parse{%
\pgfutil@ifnextchar${%$
% Ok, we found the end...
\tikz@cc@end%
}
{\pgfutil@ifnextchar+{%
% Ok, we found a coordinate...
}{%
\pgfutil@ifnextchar-{%
\tikz@cc@sub%
}{%
\tikzerror{+ or - expected}%
\tikz@cc@end$%$
}%
}%
}%
}%


The macro \tikz@parse@calculator will use \tikz@cc@parse which will parse the expression and eventually check for the ending $ and finish the calculation. All of that assuming \catcode$=3.

• What an answer ! Thanks for all the details. :-) – projetmbc Oct 19 at 22:25