4

I want to use latex macros in the calculation of the coordinates in a \draw command but that seems not to be possible if both the target point coordinates and [in=...,out=...] instructions involve such macros.

Take this MWE:

\documentclass{standalone}
\usepackage{tikz} 
\begin{document}
\def\a{25}
\def\b{0}
\def\r{1.5}
\begin{tikzpicture}
\draw (0,0) -- ({\r*cos(\b)},{\r*sin(\b)});
\draw [red] (0,0) to [out=\a,in=180-\a] (1.5,0);
\draw [blue] (0,0) to [out=-\a,in=180+\a] ({\r*cos(\b)},{\r*sin(\b)});
\end{tikzpicture}
\end{document}

The first \draw command works fine, tikz does the sin and cos calculations; the second \draw command also works fine, latex substitutes \a before tikz processes the to [in=-\a ,out=180+\a] instruction. However, the final \draw command fails with error message

"""
! Package pgf Error: No shape named 1 is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.11 ...-\a,in=180+\a] ({\r*cos(\b)},{\r*sin(\b)})  
"""

Why is this, and how can I get it to work? Thanks Jose

3 Answers 3

4

The draw command to doesn't accept computed coordinates directly, you can simply create a named coordinate, or use proper polar coordinates as I do with the green line:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\newcommand*\a{25}
\newcommand*\b{0}
\newcommand*\r{1.5}
\begin{tikzpicture}
  \coordinate (target) at ({\r*cos(\b)},{\r*sin(\b)});
  \draw (0,0) -- (target);
  \draw [red] (0,0) to [out=\a,in=180-\a] (1.5,0);
  \draw [blue] (0,0) to [out=-\a,in=180+\a] (target);
  \draw [green,very thick,dashed] (0,0) to [out=-\a,in=180+\a] +(\b:\r);
\end{tikzpicture}
\end{document}
2

I do not know why your solution does not work, but this one works

\documentclass{standalone}
    \usepackage{tikz} 
    \usetikzlibrary{calc}
    \begin{document}
    \def\a{25}
    \def\b{0}
    \def\r{1.5}
    \begin{tikzpicture}
    \draw (0,0) -- ({\r*cos(\b)},{\r*sin(\b)});
    \draw [red] (0,0) to [out={\a},in={180-\a}] (1.5,0);
    \pgfmathsetmacro{\rC}{\r*cos(\b)}
       \pgfmathsetmacro{\rS}{\r*sin(\b)}
  \draw [blue] (0,0) to [out={-\a},in={180+\a}] (\rC,\rS);
    \end{tikzpicture}
    \end{document}

enter image description here

0

This appears to be a bug in TikZ, which occurs whenever the x-coordinate after to[in=...,out=...] has a function, including sin, cos, round, floor, and so on.

A simple solution is to add another pair of curly brackets in the x-coordinate, like so:

\documentclass{standalone}
\usepackage{tikz} 
\begin{document}
\def\a{25}
\def\b{0}
\def\r{1.5}
\begin{tikzpicture}
\draw (0,0) -- ({\r*cos(\b)},{\r*sin(\b)});
\draw [red] (0,0) to [out=\a,in=180-\a] (1.5,0);
\draw [blue] (0,0) to [out=-\a,in=180+\a] ({{\r*cos(\b)}},{\r*sin(\b)});
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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