5

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 $ ?

11

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...
      \tikz@cc@add%
    }{%
      \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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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