3

I am trying to use coordinate evaluation ($ ... $) inside a tikz foreach loop to do arithmetic with my loop variable. However, I cannot seem to get it to work.

My code so far:

\begin{tikzpicture}
    \coordinate (A) at (1,1);
    \coordinate (B) at (3,2);
    \coordinate (D) at ($(B)-(A)$);
    \begin{axis}[
        anchor=origin,
        disabledatascaling,
        xmin=-1,xmax=5,
        ymin=-1,ymax=3,
        x=1cm,y=1cm,
        grid=both,
        grid style={line width=.1pt, draw=gray!10},
        %major grid style={line width=.2pt,draw=gray!50},
        axis lines=middle,
        minor tick num=0,
        enlargelimits={abs=0.5},
        axis line style={latex-latex},
        ticklabel style={font=\tiny,fill=white},
        xlabel style={at={(ticklabel* cs:1)},anchor=north west},
        ylabel style={at={(ticklabel* cs:1)},anchor=south west}
    ]

    \draw [fill] (A) circle (1.5pt) node [below right] {$P$};
    \draw [fill] (B) circle (1.5pt) node [below right] {$Q$};
    \draw[->,thick,red!60!white] (A) -- (B) node [midway,below right,yshift=2pt] {$\vec d$};

    \foreach \x in {-3,...,6} {
        \draw [fill] ($(A)+\x/3*(D)$) circle (1.5pt);% node [below right] {$P$};
    }

    \end{axis}
\end{tikzpicture}

The error seems to be ($(A)+\x/3*(D)$). How is one supposed to do this computation? I have tried defining a new point in my loop with

\path let \p1 = (D) in coordinate (r) at (\x*\x1,\x*\y1);

but that does not work either.

0

1 Answer 1

5

Inside the axis environment foreach does not get expanded properly hence you need to use the appropriate loop

\pgfplotsinvokeforeach{-3,...,6} {
    \draw [fill] ($(A)+#1/3*(D)$) circle (1.5pt);% node [below right] {$P$};
}

would work.

1
  • I didn't anticipate the axis environment could be the problem. Thank you! Nov 26, 2016 at 18:41

You must log in to answer this question.

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