# \foreach with indexed names of curves

I am constructing several curves, which are all constructed differently, but they have path name=lambda1, ..., lambda4. Later, I intersect each of them with one path. Attached is a working minimal example.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\path[name path=t1] (6,0) -- (6,7);
\draw[name path=lambda1] (0,4) -- (7,3.5);
\draw[name path=lambda2](0,2.5) sin (7,4.5);
\draw[name path=lambda3] (3,0) parabola (7,5);
\draw[name path=lambda4] (2,0) .. controls (3,4) .. (7,7);
\fill [red, name intersections={of=lambda1 and t1}] (intersection-1) circle (2pt) node    [below] {$\lambda_1$};
\fill [red, name intersections={of=lambda2 and t1}] (intersection-1) circle (2pt) node[below] {$\lambda_2$};
\fill [red, name intersections={of=lambda3 and t1}] (intersection-1) circle (2pt) node[below] {$\lambda_3$};
\fill [red, name intersections={of=lambda4 and t1}] (intersection-1) circle (2pt) node[below] {$\lambda_4$};
\end{tikzpicture}
\end{document}


The last 4 lines beg for a \foreach, but I can't make it work entirely. To me, it seems that

\foreach \n in {1,...,4} {
\fill [red, name intersections={of=lambda\n and t1}]
(intersection-1) circle (2pt) node[below] {$\lambda_\n$};}


should be the right solution, but it is not. However,

\foreach \n in {1,...,4} {
\fill[red, name intersections={of=t1 and lambda\n}]
(intersection-1) circle (2pt) node[below] {$\lambda_\n$};}


works fine. What am I missing here?

• My guess is that it is a space issue: lambda\n and expands to lambda1and and TikZ interprets this as the path name, whereupon it gets confused because the  and  is missing. Jake's solutions avoid this space-swallowing by ensuring that \n is not followed by a space. Apr 4 '12 at 21:50

## 1 Answer

If you group lambda\n by enclosing it in {...}, it works:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\path[name path=t1] (6,0) -- (6,7);
\draw[name path=lambda1] (0,4) -- (7,3.5);
\draw[name path=lambda2](0,2.5) sin (7,4.5);
\draw[name path=lambda3] (3,0) parabola (7,5);
\draw[name path=lambda4] (2,0) .. controls (3,4) .. (7,7);
\foreach \n in {1,...,4} {
\fill [blue, name intersections={of={lambda\n} and t1}]
(intersection-1) circle (2pt) node[below] {$\lambda_\n$};
}
\end{tikzpicture}
\end{document}