3

Based on this question i did some experiments with tikzset, coordinates and foreach. The following code plots four times the same thing. Twice with and twice without a tikzset. Twice with and twice without foreach. The variante where I use foreach and coordinate does not work. Why is that? The syntax looks ok to me...

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\tikzset{
  pics/coordiarrow/.style 2 args={
    code={
      \coordinate (A) at #1;
      \coordinate (B) at #2;
      \node [left] at (A) {#1};
      \node [left] at (B) {#2};
      \draw [->, thick] (A) -- (B);
    }}}

\tikzset{
  pics/paraarrow/.style 2 args={
    code={
      \node [left] at #1 {#1};
      \node [left] at #2 {#2};
      \draw [->, thick] #1 -- #2;
    }}}

\begin{document}

  \begin{tikzpicture}
  \draw[help lines] (-3,-3) grid (3,3);
  \pic at (0,0) {coordiarrow={(1,1)}{(3,3)}}; %%% Uses coordinates but no foreach
  \pic at (0,0) {coordiarrow={(-1,1)}{(-3,3)}};
  \pic at (0,0) {coordiarrow={(1,-1)}{(3,-3)}};

  \begin{scope}[shift={(8,0)}]
  \draw[help lines] (-3,-3) grid (3,3);
  \pic at (0,0) {paraarrow={(1,1)}{(3,3)}}; %%% Uses no coordinates and no foreach
  \pic at (0,0) {paraarrow={(-1,1)}{(-3,3)}};
  \pic at (0,0) {paraarrow={(1,-1)}{(3,-3)}};
  \end{scope}

  \begin{scope}[shift={(0,7)}]
  \draw[help lines] (-3,-3) grid (3,3);
  \foreach \from/\to in                      %%% Uses no coordinates but uses foreach
  {{(1,1)}/{(3,3)},{(-1,1)}/{(-3,3)},{(1,-1)}/{(3,-3)}}
  {\pic at (0,0) {paraarrow={\from}{\to}};}

%%%%%% If uncommented, it does not compile anymore %%%%% 
%  \begin{scope}[shift={(8,0)}]
%  \draw[help lines] (-3,-3) grid (3,3);
%  \foreach \from/\to in                     %%% Uses coordinates and foreach
%  {{(1,1)}/{(3,3)},{(-1,1)}/{(-3,3)},{(1,-1)}/{(3,-3)}}
%  {\pic at (0,0) {coordiarrow={\from}{\to}};}
%  \end{scope}
  \end{scope}

  \end{tikzpicture}

\end{document}

enter image description here

dani
  • 729
  • Does this question help? http://tex.stackexchange.com/questions/34090/variables-in-tikz – ArTourter Dec 09 '15 at 22:55
  • Weird. Off-topic: \to is a bad choice since it is also a standard macro. That's not the problem, mind. – cfr Dec 10 '15 at 00:25

1 Answers1

3

The macros TikZ used to handle coordinate and node are different since node is more complex than coordinate. According to the error message, it sounds like an expanding-order problem: when TikZ meets a coordinate, it wants to see at (. But it sees at \to instead and reports doesn't match definition-error. See tikz.code.tex line 3614 for more information.

\def\tikz@@coordinate@@at[#1](#2)at#3({%
  \def\tikz@coordinate@caller{\tikz@fig ode[shape=coordinate,#1](#2)at}%
  \tikz@scan@one@point\tikz@@coordinate@at@math(%
}

To solve it, on can rewrite

\tikzset{
  pics/coordiarrow/.style 2 args={
    code={
      \path #1 coordinate (A);
      \path #2 coordinate (B);
      \node [left] at (A) {#1};
      \node [left] at (B) {#2};
      \draw [->, thick] (A) -- (B);
    }}}

So that \from and \to are expanded way before TikZ reads coordinate.


Or... one might redefine \tikz@@coordinate@@at and see what will happen.

Symbol 1
  • 36,855