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.
ris not a recognized type, you are not defining a custom macro. You can only usepoints ornumbers. – percusse May 27 '15 at 22:45p1also (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