Can I use trig functions in TikZ within polar coordinates

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}

I personally prefer to do the calculations separately via \pgfmathsetmacro and then use the result. Otherwise using an additional {} group is necessary for the parser to to work.

References:

  • Using math in TikZ

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro{\CosValue}{1/cos(45)}%
    \draw (0,0) -- (2,0) -- (45:\CosValue) -- cycle;
\end{tikzpicture}
%
\begin{tikzpicture}
    \pgfmathsetmacro{\CosValue}{cos(45)}%
    \draw (0,0) -- (2,0) -- (45:1/\CosValue) -- cycle;
\end{tikzpicture}
\end{document}

Tags:

Tikz Pgf