2

So far I have written this:

\node (A) at (0, 0) {A};
\node (B) at (2, 0) {B};

\draw (A) -- node[midway, above=-.5ex] {\tiny connects} (B);

in order to produce this:

A connects B

Since I am about to do this a lot, I have refactorized the annotation this way:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}

\newcommand{\macro}[1]{node[midway, above=-.5ex] {\tiny #1}}

\begin{tikzpicture}

    \node (A) at (0, 0) {A};
    \node (B) at (2, 0) {B};

    \draw (A) -- \macro{connects} (B);

\end{tikzpicture}


\end{document}

Which produces:

! Package tikz Error: Cannot parse this coordinate.

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

l.14     \draw (A) -- \macro{connects}
                                       (B);

What is the problem here?
What are the restriction on macro usage in tikz?
Is there a convenient workaround?


Note: I am aware that a macro with 3 arguments would do the trick. But my point is to understand why this doesn't work, and further tikz macro limitations.
Also, a macro with 3 arguments would not extend to \draw (A) -- \macro{connects} -- (B) -- \macro{disconnect} (C);

  • You could use edges and quotes for that. – marmot Jul 16 '18 at 11:37
3

If you add the -- to your macro, it works.

\documentclass{standalone}

\usepackage{tikz}

\begin{document}

\newcommand{\macro}[1]{-- node[midway, above=-.5ex] {\tiny #1}}

\begin{tikzpicture}

    \node (A) at (0, 0) {A};
    \node (B) at (2, 0) {B};

    \draw (A) \macro{connects} (B);

\end{tikzpicture}
\end{document}

However, I would strongly recommend going the TikZy way.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{quotes} %<- added
\begin{document}

\begin{tikzpicture}
    \node (A) at (0, 0) {A};
    \node (B) at (2, 0) {B};
    \draw (A) edge ["\tiny connects"] (B);
\end{tikzpicture}
\end{document}

enter image description here

  • Well, thank you for this workaround :) Can you explain why -- is required for the macro to work? My naive feeling is that there should be no difference between -- \macro{connects} expansion in the first case and \macro{connects} expansion in the second case. How does tikz handle expansion? :\ – iago-lito Jul 16 '18 at 11:47
  • 1
    @iago-lito I cannot really explain, but I saw this happening rather often. The TikZ parser expects certain things, for instance after -- a coordinate, and if it does not see it, it complains. After a node it is a bit more "openminded". I am, however, still stunned how well and flexible the TikZ parser is in general. – marmot Jul 16 '18 at 11:50
  • 3
    @iago-lito after -- it needs to see a + or ( – percusse Jul 16 '18 at 11:55
  • @marmot Arf, I see. Thanks for sharing your feeling anyway :) – iago-lito Jul 16 '18 at 11:58
  • 2
    @iago-lito TikZ has its own custom parser to free the user from TeX parsing. This has pros and cons. There are certain things that makes TikZ look for certain characters appearing naked (not inside macros) in the input stream. If it sees a ( then it switches to coordinate parsing and so on. When it finishes a particular operation and starts looking for the next operation it usually expands macros but not in the middle of line operations when it is actively looking for a pattern such as + or (. In other words it does not expand things and check whether they start with +,(. – percusse Jul 16 '18 at 13:12

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.