Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\documentclass[convert = false, tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
  \coordinate (P1) at (60:4cm);
  \coordinate (P2) at (-20:4cm);

  \def\rad{1}

  \draw[name path global = line] (0, 0) -- (P1);

  \foreach \pos/\i in {(P1)/1, (P2)/2}{
    \draw[name path global = circ\i] \pos circle[radius = \rad];
  }

  \node[coordinate, name intersections = {of = line and circ1}] (P) at
  ($(intersection-1)$) {};
\end{tikzpicture}
\end{document}

I am told that LaTeX doesn't know the name \circ1 and perhaps I misspelled it. How can I access the named path for an intersection calculation?

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

The problem seems to be that the brackets around P1 and P2 throw the foreach parser off: \i contains (P1) and (P2), not 1 and 2. You can fix this by removing the brackets:

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

\begin{document}
\begin{tikzpicture}
  \coordinate (P1) at (60:4cm);
  \coordinate (P2) at (-20:4cm);

  \def\rad{1}

  \draw[name path global = line] (0, 0) -- (P1);

  \foreach \pos/\i in  {P1/1, P1/2}{
    \draw[name path global = circ\i] (\pos) circle[radius = \rad];
    \node[coordinate, name intersections = {of = line and circ\i}] (P) at
  (intersection-1) {};
  }


\end{tikzpicture}
\end{document}

Edit:

In the case of just finding the intersection of the line and only circ2, we can move the node out of the \foreach command and incorporate Qrrbrbrilbel's comment of using /.expanded.

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

\begin{document}
\begin{tikzpicture}
  \coordinate (P1) at (60:4cm);
  \coordinate (P2) at (-20:4cm);

  \def\rad{1}

  \draw[name path global = line] (0, 0) -- (P1);

  \foreach \pos/\i in  {P1/1, P1/2}{
    \draw[name path global/.expanded = circ\i] (\pos) circle[radius = \rad];
  }

  \node[coordinate, name intersections = {of = line and circ1}] (P) at
  (intersection-1) {};

\end{tikzpicture}
\end{document}
share|improve this answer
    
@dustin: They do need to be in brackets, that's why I added them in the \draw line (note how it says (\pos) in my code, and just \pos in yours). –  Jake Aug 1 '13 at 19:57
    
Can you add Qrrbrbirlbel's comment into your solution? What I ended up doing was moving the node out of the \foreach and finding the intersection of \circ1 and the line only. So I need to use name path global/.expanded=circ\i. –  dustin Aug 1 '13 at 20:13
    
@dustin: Feel free to edit my answer –  Jake Aug 1 '13 at 20:16
add comment

You can use ( and ) to loop over coordinates without protecting the , (in Cartesian coordinates), you can say

\foreach \pos in {(0,0), (0,1), (1,0), (1,1)}
   \fill \pos circle [radius=.5];

which will not split the list in eight ( (0, 0), (0, 1), etc.) elements but in four.

This is most likely more naturally to write then

\foreach \pos in {{(0,0)}, {(0,1)}, {(1,0)}, {(1,1)}}
   \fill \pos circle [radius=.5];

or even worse

\foreach \x/\y in {0/0, 0/1, 1/0, 1/1}
   \fill (\x,\y) circle [radius=.5];

which also would one hinder to use any other coordinate than Cartesian ones (you cannot input polar coordinates or named nodes or …).

The same thing happens with your already defined coordinates. The problem though is that \foreach only accepts this at the start of an element in the list and also throws away everything else coming after the ) (making every other variable in the / list defaulting to the scanned coordinate).

Why this is implemented this way I don’t know but one can (at least for this example) “fix” this by issuing

\makeatletter
\def\pgffor@scanround(#1)#2,{\def\pgffor@value{(#1)#2}\pgffor@scanned}
\makeatother

in the preamble.

(Of course, you could have written your loop differently, say

\foreach \i in {1, 2}
  \draw[name path global/.expanded = circ\i] (P\i) circle[radius = \rad];

but that’s not the point of this question.)

The /.expanded handler is still needed, though.

Code

\documentclass[convert = false, tikz]{standalone}
\usetikzlibrary{intersections}
\makeatletter
\def\pgffor@scanround(#1)#2,{\def\pgffor@value{(#1)#2}\pgffor@scanned}
\makeatother
\begin{document}
\begin{tikzpicture}
  \coordinate (P1) at (60:4cm);
  \coordinate (P2) at (-20:4cm);

  \def\rad{1}

  \draw[name path global = line] (0, 0) -- (P1);

  \foreach \pos/\i in {(P1)/1, (P2)/2}
    \draw[name path global/.expanded = circ\i] \pos circle[radius = \rad];

  \node[coordinate, name intersections = {of = line and circ1}] (P) at  (intersection-1) {};
\end{tikzpicture}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.