6

I am looking at this answer But still can't get the syntax right.

I have this example to draw a line of length 2 units, which works fine:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings}
\begin{document}
\begin{tikzpicture}
\draw  (0,0) -- ({2*cos(30)},{2*sin(30)});
\end{tikzpicture}
\end{document}

I want to make the length a variable. So I wrote (following the answer in the link above)

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings}
\begin{tikzpicture}
\draw let \r1={2} in 
        (0,0) -- ({\r1*cos(30)},{\r1*sin(30)});
\end{tikzpicture}
\end{document}

But this gives syntax error

    Package tikz Error: ``\p'' or ``\n'' expected. \draw let \r

I thought maybe the { is confusing it. So I changed it to

\begin{tikzpicture}
\draw let \r1={2} in (0,0) -- (\r1*{cos(30)},\r1*{sin(30)});
\end{tikzpicture}

Still an error. I kept trying different things, and they all are failing. So I gave up. I know I need to have {} in tickz when doing calculations. So I do not understand the problem.

Any help on the correct syntax?

Update

I also tried p1 but it gives this error

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings}

\begin{tikzpicture}
\draw let \p1={2} in (0,0) -- ({\p1*cos(30)},{\p1*sin(30)});
\end{tikzpicture}
\end{document}

 Extra }, or forgotten \endgroup. \draw let \p1={2}

Then I tried removing the {}

 \draw let \p1={2} in (0,0) -- (\p1*cos(30),\p1*sin(30));

An error. I tried keeping the {} around the trig only

  \draw let \p1={2} in (0,0) -- (\p1*{cos(30)},\p1*{sin(30)});

error. I also tried

   \draw let \p1={2} in (0,0) -- ({\p1}*{cos(30)},{\p1}*{sin(30)});

error.

Tikz syntax is mystery to me.

Nasser
  • 20,220
  • r is not a recognized type, you are not defining a custom macro. You can only use points or numbers. – percusse May 27 '15 at 22:45
  • @percusse I actualy tried p1 also (this was in the number of other trials I mentioned, I just did not show it. Will update now) – Nasser May 27 '15 at 22:48

2 Answers2

7

2 is a number, thus the let assignment would be \n1 or \n{r1} if the variable is named 1 or r1. \p expects a point, e.g., \p{1} = (3, 4). \x1 and \y1 then selects the x and y components of point \p1. \r is not defined.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings}

\begin{document}
\begin{tikzpicture}
\draw let \n1={2} in
        (0,0) -- ({\n1*cos(30)},{\n1*sin(30)});
\end{tikzpicture}
\end{document}

With the new math library, \r1 can be defined as array variable \r:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings,math}

\begin{document}
\begin{tikzpicture}
\draw[evaluate={
  \r1=2;
}]
  (0,0) -- ({\r1*cos(30)},{\r1*sin(30)});
\end{tikzpicture}
\end{document}
Heiko Oberdiek
  • 271,626
  • thanks. So I should have used n1 if the RHS is number. Ok, will remember this. I think I will use evaluate it seems better. – Nasser May 27 '15 at 23:04
3

There is a confusion I think about the function of let instead of the syntax I think.

The main idea of let syntax (and calc library in general) is not about doing arbitrary computations or numerical assignments. There is pgfmath for that and the associated macros.

let provides a nice frontend for graphical computations such as defining points and finding their x,y coordinates etc.

Hence it only understands two things, numbers and points and it understands them by the starting letter p or n. If it stumbles upon a p it starts to parse the point definition just as it would be doing it on the path and also defines two additional macros x and y.

For example, you can say things like

\p1=($(a)-(b)$), \p{aaaa} = (a|-b)

then it already gives you automatically, \x1,\y1 as the first point coordinates and \x{aaaa},\y{aaaa} as the second point coordinates such that you can use it in number crunching.

\n macros are numbers and they are parsed with \pgfmath engine and again can be used later together with others.

Here the problem is that r doesn't exist hence the error is correct and also 2 is not a point so cannot be a proper syntax for \p1.

Heiko Oberdiek
  • 271,626
percusse
  • 157,807