# Intersection of paths with constructed names

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}


• The problem (apart from the typo in horizonta1) is simply that the first horizontal and the first vertical line do not intersect: The first horizontal line is too high. If you change the -- +(0,25cm) to -- +(0,28cm), it works. Note that you don't need to go to all that trouble with the \edef: You can simply use \foreach \x [count=\i] ... to introduce an integer counter, and then use \draw [name path global=horizontal\i] (... in your loop. – Jake Apr 23 '13 at 14:49
• @Jake Still can not compile with your corrections. I edited the code to reflect better my question. – TeXtnik Apr 23 '13 at 15:05
• Well, you can't use the counter \i in the name intersections={of=vertical\i and horizontal\i}: You have to use 1 there (there's at most one intersection between two straight lines). Could you maybe edit your question to include in more detail what exactly you're trying to achieve? I have the feeling that you're going a very roundabout way, but I don't understand exactly what your final goal is. – Jake Apr 23 '13 at 15:17
• Write name intersections={of={vertical\i} and {horizontal\i}} with extra brackets to delimit the names. – Andrew Swann Apr 23 '13 at 15:19
• @jake It would be just one intersection point (intersection-1) between each (horizontal\i, vertical\i) pair of line. I want to find the intersections to define nodes with the size and position of the "cells" in this grid. – TeXtnik Apr 23 '13 at 15:21

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
• Thanks it work great. A related problem is that if I take the \fill command out of the \foreach and place in a loop by itself (with the same \foreach statement) the path names are unknown. Shouldn't the global key made the name known in all the tikzpicture? – TeXtnik Apr 24 '13 at 8:24
• That works fine for me, the global option makes the names globally defined and I can use them in other groups and loops. May be you have a typo in your code. – Andrew Swann Apr 24 '13 at 10:05
• I took your code and before the \fill command I add } \foreach \x[count=\i] in {1,...,6} {. It work well if don't try to calculate the intersection, but not with same command as in the code. – TeXtnik Apr 24 '13 at 10:32
• Damn! It worked when I change from CVS version of pgf to the TexLive2012 version. – TeXtnik Apr 24 '13 at 10:35