4

Using the \foreach statement, I generate a set of lines that form a grid. I want to use the loop variables to name the individual paths to later draw something at their intersections.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\newlength{\vlinedist}
\setlength{\vlinedist}{.4cm}
\newlength{\hlinedist}
\setlength{\hlinedist}{.4cm}
\usetikzlibrary{intersections}
\begin{tikzpicture}
   \foreach \x in {1,...,10}
   {
      \draw[name path global/.expanded=h \x] (0,\hlinedist*\x) -- ++(33*\vlinedist,0);
   }
   \foreach \x in {1,...,32} {
      \draw[name path global/.expanded=v \x] (\vlinedist*\x, 0) -- ++(0,11*\hlinedist);
   }
   \foreach \x in {1,...,10}
   {
      \foreach \y in {1,...,32}{
         \draw[name intersections={of=h \x and v \y}] (intersection-1) circle (2pt);
      }
   }
\end{tikzpicture}
\end{document}

From this question I found I must use the global/.expanded stuff in order for the paths to be named (whatever the reason). But when trying to reference the different paths in the intersections={of=h \x and v \y} part, this does not work, I get the error:

Runaway argument?
h \x and v \y \tikz@stop \else \pgfkeys@case@two \fi \fi \fi \pgfkeys@parse \ET
C.
! File ended while scanning use of \tikz@intersect@path@names@parse.
<inserted text>
                \par
<*> file.tex

?

Now I know there are other ways to make a grid and place points at some chosen intersections, however that is beside the point, as this is a rather general question of how I can use loop variables to reference named paths in tikz.

oarfish
  • 1,419

1 Answers1

2

The error comes from intersections={of=h \x and v \y} where {h-\x} and {v-\y} should be used. Actually, no dashes in h-\x and v-\y are required, only the { }. Thanks to @percusse for the kind comment to me.

enter image description here

Code

\documentclass[border=10pt]{standalone}%{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\newlength{\vlinedist}
\setlength{\vlinedist}{.4cm}
\newlength{\hlinedist}
\setlength{\hlinedist}{.4cm}
\begin{document}
\begin{tikzpicture}
   \foreach \x in {1,...,10}
   {
      \draw[name path global/.expanded=h-\x] (0,\hlinedist*\x) -- ++(33*\vlinedist,0);
   }

   \foreach \x in {1,...,32} {
      \draw[name path global/.expanded=v-\x] (\vlinedist*\x, 0) -- ++(0,11*\hlinedist);
   }

   \foreach \x in {1,...,10}
   {
      \foreach \y in {1,...,32}{
         \draw[name intersections={of={h-\x} and {v-\y}}](intersection-1) circle (2pt);
      }
   }
\end{tikzpicture}
\end{document}
Jesse
  • 29,686