[Tex/LaTex] Intersection of paths with constructed names

tikz-pgf

This question is a "follow up" of the question naming paths inside a TikZ foreach loop. I can construct the name of paths, but there are errors when using them to calculate intersection.

An example follows

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}

\newcommand{\modulargrid}{%
\begin{tikzpicture}[remember picture,   overlay,  ]

%% Trick to name paths:
%% https://tex.stackexchange.com/questions/5242/naming-paths-inside-a-tikz-foreach-loop

%% Draw horizontal lines
\foreach \x in {1,...,6}   {
\edef\pathhone{name path global=horizontal\x}
\expandafter\draw\expandafter[\pathhone] ($(current page.north west) + \x-1*(0,20pt-5cm)$)
-- +(20cm, 0);

%% Draw vertical lines
\edef\pathvone{name path global=vertical\x}
\expandafter\draw\expandafter[\pathvone] ($(current page.south west) + \x-1*(20pt+5cm,0)$)
-- +(0,25cm);
}

\draw (0,0)--(0,0);
\fill[red, opacity=0.5, name intersections={of=vertical1 and horizonta1}]
(intersection-1) circle (2pt) node {1};
\end{tikzpicture}
}

\begin{document}

\modulargrid
\newpage

\end{document}


The purpose of the code

The idea behind this code is to make a grid of vertical and horizontal line, calculate the intersection points and them build rectangular shapes with the calculated points (I want to be able to each cell in the grid as a node.)

EDITED CODE

I edited the code according to the comment by @jake.
(Sorry for the errors, due to coping an more complex example to a MWE.)

My problem is to use \i in the \fill command

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}

\newcommand{\modulargrid}{%
\begin{tikzpicture}[remember picture,   overlay,  ]

%% Trick to name paths:
%% https://tex.stackexchange.com/questions/5242/naming-paths-inside-a-tikz-foreach-loop

%% Draw horizontal lines
\foreach \x[count=\i] in {1,...,6}   {
\draw[name path global=horizontal\i] ($(current page.north west) + \x-1*(0,20pt-5cm)$)
-- +(20cm, 0);

%% Draw vertical lines
\draw[name path global=vertical\i] ($(current page.south west) + \x-1*(20pt+5cm,0)$)
-- +(0,28cm);
\draw (0,0)--(0,0);
\fill[red, opacity=0.5, name intersections={of=vertical\i and horizontal\i}]
(intersection-1) circle (2pt) node {1};
}

\end{tikzpicture}
}

\begin{document}


Apart from typos the main problem is in name intersections. The error produced by latex is "Runaway argument?" showing that it is not correctly parsing the arguments. To fix this write {vertical\i} in brackets and similarly {horizontal\i}. The code below is also tidied in various ways that may help with your general problem, and now produces:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}

\newcommand{\modulargrid}{%
\begin{tikzpicture}[remember picture, overlay]
%% Draw horizontal lines
\foreach \x[count=\i] in {1,...,6}   {
\draw[name path global=horizontal\i] ($(current page.north west) + \x*(0,20pt-5cm)$)
-- +(22cm, 0);
%% Draw vertical lines
\draw[name path global=vertical\i] ($(current page.south west) + \x*(20pt+3cm,0)$)
-- +(0,28cm);
%% Dots at intersections
\fill[red, opacity=0.5,
name intersections={of={vertical\i} and {horizontal\i}}]
(intersection-1) circle (5pt) node {};
}
\end{tikzpicture}
}

\usepackage{lipsum}

\begin{document}

\modulargrid
\lipsum[1-6]
\end{document}


The main changes are

• making the \x variable multiply the offsets (and using just \x rather than \x - 1)
• increased size of dot for clarity and providing an empty node argument
• changed spacing and lengths and inclusion of background text via lipsum package for visualisation