39

When can I use functions and do calculations in TikZ? How do I make this work:

\draw (0,0) arc(0:90:sqrt(15)); %not ok

why is this working:

\draw (0,0) arc(0:asin(1):5); %ok

with \usetikzlibrary{calc} is this:

 \draw (0,0) -- ($ (4,0) + sqrt(7)*(0,1) $);  %ok

the only way to do single coordinate calculations? why is this

 \draw (0,0) -- ($ (4,sqrt(7)) $);  %not ok

not working?

Minimal example:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,0) arc(0:90:sqrt(15));              %not ok
\draw (0,0) arc(0:asin(1):5);                %ok
\draw (0,0) -- ($ (4,0) + sqrt(7)*(0,1) $);  %ok
\draw (0,0) -- ($ (4,sqrt(7)) $);            %not ok
\end{tikzpicture}
\end{document}

2 Answers2

51

You need to wrap the expression into { } to hide the second pair of ( ) from the TeX parser. Without the { } a ( will be closed by the next ) even if it belongs to another (. This means arc(0:90:sqrt(15)) will be taken as arc(0:90:sqrt(15) without the second ). This causes basically two errors, one in the expression because it misses the ) and another one in the \draw path which doesn't know what to do with a single ). With the { } it works because any { must be closed first with a } before the ) is taken.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
    \draw (0,0) arc(0:90:{sqrt(15)}); %now ok
    \draw (0,0) arc(0:{asin(1)}:5); %ok
    \draw (0,0) -- ($ (4,0) + sqrt(7)*(0,1) $);  %ok
    \draw (0,0) -- ($ (4,{sqrt(7)}) $);  %now ok
\end{tikzpicture}

\end{document}
Martin Scharrer
  • 262,582
  • Wow, I didn't know that... The Tikz manual does specify that uou can use curly braces in factors, I should have tried them in coordinates as well. I still don't understand why asin(x) in the arc would cause no problems, while sqrt(x) does. Can you explain this? – Roelof Spijker Oct 17 '11 at 15:12
  • 4
    @whlt3: This all is rooted in the way TeX handles a parameter text, i.e. is a low-level thing and not from TikZ. The arc arguments is read by the internal macro \def\tikz@@@arcto@check@slashand(#1:#2:#3){ ... } which awaits the three parts between :, so a ) in the second argument can't close the ( because TeX still looks for the : then stores everything up to the next ) as #3. You might want to read The TeXBook to learn about such TeX macros. – Martin Scharrer Oct 17 '11 at 15:21
  • 2
    Actually, arc(0:asin(1):5) works because of the reason stated in my comment above, but arc(0:{asin(1)}:5) is much saver. TikZ reads the whole (...) first (i.e. arc(0:asin(1) with the first form) and expands it using \edef, then passes it to the macro mentioned in my last comment. Using the first form anything after the inner ) is not expanded! – Martin Scharrer Oct 17 '11 at 15:23
  • Ah, that's awesome :) I'm currently trudging through TeX by Topic, will have a look at the TexBook if I ever get through that. – Roelof Spijker Oct 17 '11 at 15:31
2

Edit: This is incorrect, disregard this.. Well, it's correct up to the point that the coordinates can't have math in them. Disregard it anyway.

It's just how the Tikz calc library is set up. If we have a look at section 13.5 of the manual

The general syntax is the following:
([ options ]$ coordinate computation $).
As you can see, the syntax uses the TEX math symbol $ to indicate that a “mathematical computation” is involved. However, the $ has no other effect, in particular, no mathematical text is typeset.
The coordinate computation has the following structure:
1. It starts with < factor > * < coordinate > < modifiers >
2. This is optionally followed by + or - and then another < factor > * < coordinate > < modifiers >
3. This is once more followed by + or - and another of the above modified coordinate; and so on.
In the following, the syntax of factors and of the different modifiers is explained in detail.

The following you will have to read in the manual for yourself. The important thing here is that the math can only occur in the factor and the modifiers. Not in the coordinate itself, which explains why your second example does not work. The reason the first example doesn't work is explained in the manual as well. It is the same type of restriction in the Tikz grammar.

Roelof Spijker
  • 17,663
  • 5
  • 55
  • 63