1

I am using TikZ math library which is great.

However, I can't get my head around this (see MWE).

I missed something from the documentation (https://tikz.dev/library-math)?

\documentclass[tikz]{standalone}

\usetikzlibrary{math}

\begin{document} \begin{tikzpicture}

\tikzmath{ \a = 2; \b = 1; } \draw[step=1cm,gray,very thin] (0,0) grid (3,2); % just for orientation

\draw (\a, \b) circle (2pt); % no problem here, so \a and \b are fine

\draw [->] (2, 0) arc (0:360:2 and 1); % no problem here %\draw [->] (2, 0) arc (0:360:\a and \b); % gives me an error after removing comment - why?

\end{tikzpicture} \end{document}

Stephen
  • 3,826
17tmh
  • 91
  • 1
    Try \draw [->] (2, 0) arc (0:360:{\a} and {\b});. Sometimes, the TikZ parser has a hard time understanding what you tell it when you put macros where it expects letters or numbers. In such cases, it is often a good idea to wrap the macro in curly braces. – Jasper Habicht Jul 25 '23 at 06:58
  • 1
    By the way: it is recommended to use a newer syntax for arc, which would be arc[start angle=0, end angle=360, x radius={\a}, y radius={\b}] (the curly braces would be optional in this case). – Jasper Habicht Jul 25 '23 at 08:25
  • Try \draw [->] (2, 0) arc (0:360:\a{} and \b);... – Paul Gaborit Jul 25 '23 at 12:14
  • Or try \draw [->] (2, 0) arc (0:360:\a\space and \b);... – Paul Gaborit Jul 25 '23 at 12:15
  • 1
    A little bit related because of spaces but with node names: Q95521, Q667361. However, with node names {\a} and {\b} would lead to the node name {2} and {1} and not 2 and 1. TikZ expects <y> and <x> including the spaced around and but the space after \a vanishes because it ends the control sequence. – Qrrbrbirlbel Jul 25 '23 at 12:49

1 Answers1

2

You probaby did not miss anything from the manual, but you should note that the TikZ parser needs to understand a lot of different syntax uses and it is therefore often necessary to help it by wrapping macros or on-the-fly calculations in curly braces. In this case, the space after the macro \a is gobbled (so strictly speaking, it is not a parser problem, but wrapping macros in curly braces would still help here):

In this case, the following will solve the problem:

\draw [->] (2, 0) arc (0:360:{\a} and {\b});

However, note that there is a newer syntax for the arc fuction which also would allow you to omit the curly braces. The above would be written as:

\draw [->] (2, 0) arc[start angle=0, end angle=360, x radius=\a, y radius=\b];
  • 2
    Not a TikZ parser problem. Just a (La)TeX macro that eats the next space... :-) – Paul Gaborit Jul 25 '23 at 12:19
  • Oh, right! Thanks! Well, the strategy to wrap macros in curly braces in TikZ is still a good idea ... as it also prevents spaces being gobbled or interpreting unrelated things as part of macro names. – Jasper Habicht Jul 25 '23 at 12:45