11

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?

1

2 Answers 2

13

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}
3
  • So it is just a known limitation then? If my path has 20 coordinates in it, I will need to define a let variable for each one of them? I was hoping that I could pull it off with an extra set of braces.
    – nispio
    Commented Feb 22, 2013 at 23:18
  • @nispio Seems so, though I'm hardly an expert. Commented Feb 23, 2013 at 0:02
  • Thanks for the follow-up research and subsequent edit! It is much more satisfying to hear that the problem is the result of a bug, rather than an oversight in the implementation of features. However, the workaround definitely makes the code more fragile, since removing a seemingly innocuous space could break it.
    – nispio
    Commented Feb 25, 2013 at 19:59
5

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:

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}
5
  • I was originally using \pgfmathsetmacro but the number of them was starting to pile high because I had many coordinates within the path. Can you comment on what you mean by an additional {} group being necessary? I don't see it in your code.
    – nispio
    Commented Feb 22, 2013 at 23:31
  • 1
    Well, I was originally referring to the {1/tan(45)} which you already figured out, but looking more closely now I see that does not quite work with polar coordinate. I thought there was an question here (with an answer from Andrew Stacey?) addressing this parsing issue, but can't seem to locate it. Commented Feb 22, 2013 at 23:55
  • 2
    Thanks, your comment made me do a little search, which led me to tex.stackexchange.com/questions/41828/using-math-in-tikz Commented Feb 23, 2013 at 0:11
  • Yes, that's what I was looking for. Have added that to the answer... Your search skills are better than mine. Since I had incorrectly assumed who answered it, that did not make my search very likely to succeed. Commented Feb 23, 2013 at 0:22
  • 1
    It can sometimes be easier to find stuff with Google, rather than the site search. I searched for tex.stackexchange polar coordinate tikz calculations and it was one of the first hits. Commented Feb 23, 2013 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .