3

I'm having a hard time understanding how TikZ handles coordinates. This question is thus related to my previous two questions, and some more in the future...

In the code below I define a tikzset that takes two parameter. I want to store them in coordinates (yes - not needed and nonsense in this case, but not in what I'm trying to do with TikZ).

The resulting picture is correct but I get an error: ! Package tikz Error: Giving up on this path. Did you forget a semicolon?. l.17 \pic at (0,0) {somearrow={(1,1)}{(3,3)}}

What is wrong with my code? Please explain.

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

\tikzset{
  pics/somearrow/.style 2 args={
    code={
      \coordinate (A) at (#1);
      \coordinate (B) at (#2);
      \node at (A) {#1};
      \node at (B) {#2};
    }}}

\begin{document}

  \begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,4);
  \pic at (0,0) {somearrow={(1,1)}{(3,3)}};
  \end{tikzpicture}

\end{document}

enter image description here

cfr
  • 198,882
dani
  • 729

1 Answers1

4

You are asking it to place coordinates at ((0,0)) and ((3,3)) rather than (0,0) and (3,3). Just omit the additional parentheses:

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

\tikzset{
  pics/somearrow/.style 2 args={
    code={
      \coordinate (A) at #1;
      \coordinate (B) at #2;
      \node at (A) {#1};
      \node at (B) {#2};
    }}}

\begin{document}

  \begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,4);
  \pic at (0,0) {somearrow={(1,1)}{(3,3)}};
  \end{tikzpicture}

\end{document}

errorless output

cfr
  • 198,882
  • You are right, this works. Thought I tried this combination of {} and (). Could you give a quick explanation why I need to use {..} on parameters like {#1} but (..) on coordinates like (A)? – dani Dec 08 '15 at 08:06
  • I'm not sure I understand. It is going to be to do with expansion, though. When you use #1 in the pic, the outer curly brackets are removed and you get e.g. (0,0) or whatever which is what you want for a coordinate specification. When you are putting the parameter into a node, you need the curly brackets because that's the syntax for nodes i.e. you want {(0,0)}. So, you need to restore the ones which have been stripped. Does that help? As I say, I'm not sure I understand what you are asking.... (So it may well not help at all :(.) – cfr Dec 08 '15 at 13:23