4

In TikZ, I'm sometimes in the need to use macros in coordinate names, usually while using \foreach. My problem is that macros in coordinate names eliminate any following spaces.

For example, here is an example of this problem:

\usepackage{tikz}

\begin{document} \begin{tikzpicture} \def\a{a} \coordinate (\a 1); \node at (a1) {Test}; % works correctly \node at (a 1) {Test}; % error: "No shape named `a 1' is known. \end{tikzpicture} \end{document}

In the example I'm using the macro \a in the definition of the coordinate (\a 1). I would expect to get the coordinate (a 1), but the whitespace following the macro is eliminated and I end up with just (a1).

Is there any way to tell TikZ (or LaTeX, I don't really know how the macro expansion works behind the scenes) that I want to keep the space after the macro? I've tried escaping it ((\a\ 1), which doesn't compile), adding more spaces ((\a 1), where any number of spaces still get eliminated) and adding some braces (({\a} 1) or (\a{} 1), which both kind of work to prevent the elimination of the whitespace, but just because the braces are included in the coordinate name, producing ({a} 1) and (a{} 1) respectively).

Is there a solution for my problem or do I just need to avoid using spaces after macros?

PF98
  • 65

1 Answers1

6

Spaces after control sequences get gobbled by TeX because they're there to end the control sequence.

If you use {a} or a{} the {} are going to be part of the node name. (PGF/TikZ uses \csname internally to store a node which just works that way).

\ is a special macro that does not just input a , similar ~ (the unbreakable space).

What you want to use is \space, a literal :

\coordinate (\a\space 1);

As a work-around, any non-letter character ends the control sequence as well, so

\coordinate (\a-1); % or (\a -1)

will make the coordinate be named a-1 which might be easier to deal with than with \spaces and s.

You could also use \def\a#1{a #1} but don't, that's just very inelegant, in my opinion.

Qrrbrbirlbel
  • 119,821