1

The code below generates coordinate A at (cos 37°, sin 37°).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math,calc}
\title{}
\author{}
\date{}
\begin{document}
\maketitle

\tikzmath{\x=37;} \begin{tikzpicture} \coordinate (A) at ($cos(\x)(1,0)+sin(\x)(0,1)$); \end{tikzpicture}

\end{document}

However, when I change the line defining the coordinate to below, it no longer works.

\coordinate (A) at ($(cos(\x),sin(\x))$);

Separating axes could be a pain when there are many of them. How can I write everything in one coordinate?

  • To your last paragraph: How many "separating axes" do you have? You can also do ([x=(45:1cm), y=(-45:2cm)]37: 1) to get cos 37 * (45:1cm) + sin 37 * (-45:2cm). Maybe there's an easier way to specify the coordinates than with all of that math. – Qrrbrbirlbel Sep 22 '22 at 10:53

1 Answers1

2

You need to protect the ) from the trigonometric functions otherwise the coordinate parser will do nasty things.

Meaning it will think cos(\x is a coordinate specification and since it doesn't fit any of the other valid TikZ coordinate specifications it will assume cos(\x to be the name of a coordinate or node:

Package pgf Error: No shape named `cos(37' is known.

And then the calc library can't deal with ), sin(\x anymore:

Package tikz Error: + or - expected.

In your case, this should be

\coordinate (A) at ($({cos(\x)}, {sin(\x)})$);

or – in this simple case –

\coordinate (A) at ($(cos \x, sin \x)$);

Note that you don't need the calc library for this. TikZ throws every coordinate part in PGFmath anyway:

\coordinate (A) at (cos \x, sin \x);
% or
\coordinate (A) at ({cos(\x)}, {sin(\x)});

PGF/TikZ also has polar coordinates built-in. You could have just used

\coordinate (A) at (\x : 1);

This can also deal with different radii:

\coordinate (A) at (\x : 1 and 2); % i.e. cos \x * (1, 0) + sin \x * (0, 2)
Qrrbrbirlbel
  • 119,821