2

I am trying to execute the following code where I compute the coordinates using the TikZ math library and then I draw them in TikZ.

Why does it output error ! Package pgf Error: No shape named '2*cos(60' is known. when defining the coordinate with mathematical functions (in my case with sin and cos)? The pgf math functions should be readily available in the TikZ math library.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}
\tikzmath{
coordinate \x;
\x1=(0,0);
\x2=(1.5,0);
\x3 = (2*cos(60), 2*sin(60));
}

\draw[help lines] (0,0) grid (2,2);
\draw[->] (-0.2,0) -- (2.2,0) node [anchor=west] {$x$};
\draw[->] (0,-0.2) -- (0,2.2) node [anchor=south] {$y$};
\draw [red,thick] (\x1) -- (\x2) -- (\x3) -- cycle;
\end{tikzpicture}

\end{document}

Expected drawing

enter image description here

2
  • 1
    See also tex.stackexchange.com/a/585586/38080
    – Rmano
    Commented Jun 23, 2021 at 9:13
  • 1
    @Rmano, thanks! your link explains exactly the reason for the brackets: the parser looks for the end parenthesis ) of the coordinate and the parenthesis of the function interfere. Commented Jun 23, 2021 at 9:30

1 Answer 1

1

I just missed (if someone could explain why...) some curly brackets around the math operations in the coordinates.

Such brackets are not needed when using a normal variable, e.g., \a= 2*cos(60), but seemingly are needed for coordinates definitions. For example, \a=2*cos(60); \b=2*sin(60); \x3=(\a,\b); in the \tikzmath environment would work, but it is too verbose.

Easiest solution:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\begin{tikzpicture}
\tikzmath{
coordinate \x;
\x1=(0,0);
\x2=(1.5,0);
\x3 = ({2*cos(60)}, {2*sin(60)});
}

\draw[help lines] (0,0) grid (2,2);
\draw[->] (-0.2,0) -- (2.2,0) node [anchor=west] {$x$};
\draw[->] (0,-0.2) -- (0,2.2) node [anchor=south] {$y$};
\draw [red,thick] (\x1) -- (\x2) -- (\x3) -- cycle;
\end{tikzpicture}

\end{document}

You must log in to answer this question.

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