# Using math in TikZ

I know it has been asked and answered before, and I need to put { and } around it, but for some reason this doesn't work:

\draw (0,0) -- ({atan(1)}:{sqrt(2)});


Any idea why?

-
This is one of my favorites: Try \draw (0,0) -- ({atan(1)}:sqrt(2); :D Note the missing paranthesis. I'll try to find the relevant question. –  percusse Jan 21 '12 at 16:50
The problem seems to be with the polar coordinates, namely with the radius part. Both of these work fine: \draw (0,0) -- ({atan(1)},{sqrt(2)});, \draw (0,0) -- ({atan(1)}:1.4);, but this one does not: \draw (0,0) -- (45:{sqrt(2)});. –  Jan Hlavacek Jan 21 '12 at 16:52
percusse - Haha, that works. It's not the best since I need more complex computations, namely sqrt(5+2*sqrt(3)). –  Jon Jan 21 '12 at 16:55
All this is related to this question: tex.stackexchange.com/questions/39882/…. Braces around {atan(1)} and {sqrt(1)} are removed and send to a macro with a parameter text sort of like (#1), that is (sqrt(1)), so the argument given to the macro is actually sqrt(1. Sounds like a bug... –  cjorssen Jan 22 '12 at 14:31
The issue should now be fixed in the tikz/pgf cvs repository at sourceforge. –  cjorssen Jan 24 '12 at 13:38

# Important!

This issue has been fixed in the upcoming version of TikZ ( > v.2.10).

If you are looking for an immediate fix, then you need to install the CVS version of TikZ manually. This means that it's not your common TeXLive 2013 or MikTeX 2.9 distribution code but you have to download and install the package with some additional labor.

Please do not try to install CVS version by overwriting the current one!!

Instead copy it to a local folder and introduce it to LaTeX slowly. Here is a starting point: How to install a current version of TikZ?

It needs more cowbell!!

The reason is quite mystical for me and probably our wizards will explain it. You need an extra space in the end :

\begin{tikzpicture}
\draw (0,0) -- ({atan(1)}:{sqrt(2)} );
\end{tikzpicture}


or, remarkably, you don't put any braces in the last computation and skip the closing parenthesis:

\begin{tikzpicture}
\draw (0,0) -- ({atan(1)}:sqrt(2);
\end{tikzpicture}


This is simply black magic (or an extra parenthesis is carried from the math computation). So I welcome anyone to edit this answer and enlighten us.

I don't know if it is related or not but here is another one with more hilarity(!?)

\usetikzlibrary{calc}
\begin{tikzpicture}
\draw (0,0) arc (0:45:({veclen(2,2)}););
\end{tikzpicture}


In this example you can't get away with an extra space. You need to omit the last paranthesis. Also one can remove the last two characters ); since they play no role after the semicolon.

\begin{tikzpicture}
);
\end{tikzpicture}

-
Adding a space does work. Indeed witch crafting... Thanks! –  Jon Jan 21 '12 at 17:05
How peculiar. Can anyone shed some light on which this happens? –  qubyte Jan 21 '12 at 17:35
IMHO, the last two examples have clearly illegal syntax. Looking at the arc code, the delimited arguments are protected against what the OP mentioned. –  cjorssen Jan 24 '12 at 9:03
@cjorssen I'm not sure I understand your comment but happy that you have a fix in the new version (couldn't test it yet). So can you elaborate a little more? –  percusse Jan 24 '12 at 14:48
@percusse I mean that (0:45:{veclen(2,2)}) is correct (and works) and (0:45:({veclen(2,2)})) is not. Why surrounding {veclen(2,2)} with ()? –  cjorssen Jan 24 '12 at 16:09

Update The workaround below is now part of the pgf/tikz cvs version.

As I said in comment, this behavior is related to the fact that TeX removes braces around delimited arguments. Just a little example to understand:

\documentclass{minimal}
\def\foo(#1){%
\def\bar{#1}%
\show\bar}
\def\test{{test}}
\show\test
\foo(\test)
\begin{document}
\end{document}


Output:

\test=macro:->{test}.

\bar=macro:->\test .

tikz uses delimited arguments to parse polar coordinates in a succession of macro calls \tikz@...@parse@polar. So a naive workaround is to protect all delimited arguments in the "inner macro calls" (that is between the first macro using ( and ) to delimit arguments and the last one). That way, as braces are stripped each time those "inner" macros are called, there should not be any side effects (I may be wrong here).

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
% Code from tikz.code.tex
\def\tikz@parse@polar#1(#2:#3){%
% Braces surrounding #2 or #3 eventually disapeared
\pgfutil@ifundefined{tikz@polar@dir@#2}
% So we put them back
{\tikz@@parse@polar#1({#2}:{#3})}
{\tikz@@parse@polar#1(\csname tikz@polar@dir@#2\endcsname:#3)}%
}
\def\tikz@@parse@polar#1(#2:#3){%
% Again braces surrounding #2 or #3 were stripped
\pgfutil@in@{ and }{#3}%
\ifpgfutil@in@%
% So we put them back
\edef\tikz@args{({#2}:#3)}%
\else%
\edef\tikz@args{({#2}:{#3} and {#3})}%
\fi%
\expandafter\tikz@@@parse@polar\expandafter#1\tikz@args%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- ({atan(1)}:{sqrt(2)});
\draw[semitransparent,thick,red] (0,0) -- (45:1.41);
\end{tikzpicture}
\end{document}

-
Thank you very much for that fix! I will reward it with a small bounty. :-) –  Henri Menke Mar 7 at 20:21
@HenriMenke You're welcome :-) –  cjorssen Mar 9 at 21:13
@henrimenke Thanks! –  cjorssen Mar 10 at 23:25

Another option is to calculate the length beforehand:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}