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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThis issue has been fixed in the official version of TikZ ( v.3.0.0 and later).
The answer continues after these messages.... Community Promotion Ads - 2015
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}
arc
code, the delimited arguments are protected against what the OP mentioned.
(0:45:{veclen(2,2)})
is correct (and works) and (0:45:({veclen(2,2)}))
is not. Why surrounding {veclen(2,2)}
with ()
?
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}
:-)
Mar 7, 2014 at 20:21
Another option is to calculate the length beforehand:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetlengthmacro{\radius}{sqrt(5+2*sqrt(3))*1cm}
\draw (0,0) -- ({atan(1)}:\radius);
\end{tikzpicture}
\end{document}
\draw (0,0) -- ({atan(1)}:sqrt(2);
:D Note the missing paranthesis. I'll try to find the relevant question.\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)});
.sqrt(5+2*sqrt(3))
.{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 actuallysqrt(1
. Sounds like a bug...tikz/pgf
cvs repository at sourceforge.