36

Is it possible to create named paths inside a foreach loop with the name depending on the counter?

I want to write something like

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
    \foreach \i in {1,2,...,6}
        \draw[name path=line\i] (-3,-\i) -- (3,\i);
    \draw[name intersections={of=line1 and line2}] (intersection-1) circle (5pt);
\end{tikzpicture}

\end{document}

but this throws the following error:

! Missing \endcsname inserted.
<to be read again> 
                   \OT1\i 
l.11    \draw (0,3) circle (3);

Is this a bug or am I doing something wrong? What should I do instead?

2 Answers 2

19

It also possible a bit less hacky. (pgf-manual p. 496) Look at this modified code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
    \foreach \i in {1,2,...,6}
    {
  \draw[name path global/.expanded=line\i] (-3,\i) -- (3,-\i);
    }
\draw[name intersections={of=line1 and line2}] (intersection-1) circle (5pt);
\end{tikzpicture}

\end{document}
2
16

I've found that when passing options to TikZ elements as macros then I need to expand the macro before passing it in:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
    \foreach \i in {1,2,...,6}
{
  \edef\optname{name path global=line\i}
  \expandafter\draw\expandafter[\optname] (-3,\i) -- (3,-\i);
}
    \draw[name intersections={of=line1 and line2}] (intersection-1) circle (5pt);
\end{tikzpicture}

\end{document}

Now that I've looked up what naming paths does, I notice that the first version of this wouldn't have worked because the path naming occurred within a foreach loop, and that's a local scope. So the path names don't survive to the "outside". This is true even if the \foreach is a one-liner (so no curly braces). To verify this, one can simply do:

\foreach \i in {1}
  \draw[name path=line] (3,1) -- (-3,-1);

and then try to refer to the line path. TikZ will complain.

The solution to this is to use the name path global key instead. This makes the path name available For All Time (well, for the whole picture). Whereupon, it works.

Here's the result of the above:

alt text

6
  • This compiles and it sometimes works as expected. If I plug it into the example above tikz says that line1 as unknown. When I add an unrelated drawing statement between the loop and the example, it works as expected. In the more complicated picture where I actually want to use this, I can never get it to work.
    – Caramdir
    Nov 12, 2010 at 21:58
  • 1
    @Caramdir: Looking at the manual, I see that path names are only local unless declared otherwise. So I think you need to use path name global. I've updated my answer to reflect this. Nov 13, 2010 at 12:48
  • Thank you, this works perfectly. I'm somewhat embarrassed that I missed the path name global option.
    – Caramdir
    Nov 13, 2010 at 17:33
  • I have used this solution to name paths, but I couldn't use those names for intersections in a call to pgfplotsextra, as in \edef\extraopts{name intersections={of=\line\i and refLine}}.
    – Christoph
    Feb 9, 2012 at 9:56
  • @Christoph: I'd need to see the full code that you were trying. It's probably best to ask a follow-up question (refer to this one, of course). Feb 9, 2012 at 10:34

You must log in to answer this question.

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