[Tex/LaTex] use trig functions in TikZ within polar coordinates

tikz-pgf

I am trying to use simple mathematical functions such as sin, cos, and sqrt within the declaration of a TikZ coordinate. I am finding that this works for cartesian coordinates, but breaks for polar coordinates:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
% \draw (0,0) -- (2,0) -- (45:1/0.707) -- (0,0); % <-- works
% \draw (0,0) -- (2,0) -- (45:{1/cos(45)}) -- (0,0); % <--doesn't work
\draw (0,0) -- (2,0) -- (1,{1/tan(45)}) -- (0,0); % <--works
\end{tikzpicture}

\end{document}

Is this just a known limitation, or is there some way to get around this?

Best Answer

This is actually a bug that is fixed in the CVS version of PGF/TikZ, see Using math in TikZ

As percusse reports in his answer to that question, you can get around this bug by simply adding a space after the closing brace in the radius calculation:

\draw (0,0) -- (2,0) -- (45:{1/cos(45)} ) -- (0,0);

cjorssen also posted the fix that is now in the CVS version, and which you can use in your document. See his answer to said question.


Original answer

You can get around this by using the let keyword to do the calculation outside the coordinate. This is described in section 14.15 The Let Operation of the manual, which is on page 150 (for version 2.10, dated 25 October 2010).

I used cycle to close the path, as this ensures a proper merging of the lines at that corner.

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\draw let \n1 = {1/cos(45)}  in
   (0,0) -- (2,0) -- (45:\n1) -- cycle; 
\end{tikzpicture}

\end{document}