2

I am trying to plot a number of lines from a point on the segment connecting points from a given list of points to the coordinate origin (0,0). Since I want to automate this procedure, I am using \foreach. When I am trying to use the calc features to calculate the coordinates of the points I am running into an error.

Here is my code

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\def\points{(-2,0), (-1.2, 1), (0, 1.55), (0.8, 1.65), (1.3, 1.4), (2, 0.1), (1.8, -0.7), (1,-1.6), (-1, -1)}

\begin{document}
\begin{tikzpicture}
  \foreach \p in \points{
    \draw[gray!80!white] \p--(0,0);
    \draw[gray, ->] \p--($\p!0.1!(0,0)$);}
\end{tikzpicture}
\end{document}

And here the error:

File ended while scanning use of \tikz@cc@parse@factor.

Note that this question is different from this one since I do not use the axis environment.

  • Note that you're writing also ...!(0.0)$), it should be ...!(0,0)$). – Alenanno Jan 16 '18 at 13:56
  • @Alenanno thanks - I completely missed that one. – Christian Jan 16 '18 at 13:58
  • A quick and durty hack is to add single ( at the begining, like this $(\p!0.1!(0,0)$ – Kpym Jan 16 '18 at 20:27
3

Here are two possible alternatives. In the first one I changed the definition of the \points macro to not include (), and added those in the code as well. You then need {} instead of () in \points to delimit the entries in the "array".

The other version uses your definition of \points, but leaves out the coordinate calculation altogether, using instead a decoration to add the arrow tips.

enter image description here

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,decorations.markings}

\def\points{{-2,0}, {-1.2, 1}, {0, 1.55}, {0.8, 1.65}, {1.3, 1.4}, {2, 0.1}, {1.8, -0.7}, {1,-1.6}, {-1, -1}}

\begin{document}
\begin{tikzpicture}
  \foreach \p in \points{
    \draw[gray!80!white] (\p)--(0,0);
    \draw[gray, ->] (\p)--($(\p)!0.1!(0,0)$);
}
\end{tikzpicture}

% second version
\def\points{(-2,0), (-1.2, 1), (0, 1.55), (0.8, 1.65), (1.3, 1.4), (2, 0.1), (1.8, -0.7), (1,-1.6), (-1, -1)}

\begin{tikzpicture}[decoration={markings,mark=at position 0.1 with \arrow{>}}]
  \foreach \p in \points
    \draw[gray!80!white, postaction={decorate}] \p--(0,0);
\end{tikzpicture}
\end{document}
  • Is there a simple explanation why the original code did not work? Is it a mistake on my side or is there a bug in one of the packages? – Christian Jan 16 '18 at 13:57
  • 2
    @Christian I would guess an expansion issue, that the \p in ($\p!0.1!(0,0)$) isn't expanded so that the parser doesn't "see" that coordinate, but I don't know. Which I wouldn't necessarily call a bug. I don't know how to fix it either. – Torbjørn T. Jan 16 '18 at 14:00
  • 2
    @Christian it has to see a naked ( to understand that it is a coordinate expression. – percusse Jan 16 '18 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.