Here is my attempt. Initially I started looking for a solution similar to the one proposed by John Kormylo in his comment, and ended up using the decorations.markings
library. I hope it is not such an overkill :-) ! In every case the grid is self-adapting, as you can see from the following images:



My code is:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\def\NrLines{8}
\pgfmathsetmacro{\UnitSegment}{1/(\NrLines+1)}
\begin{document}
\begin{tikzpicture}[draw=red]
% Nodes definition
\node[inner sep=0pt,minimum size=0pt,label=below:1] (a) at (2,5) {};
\node[inner sep=0pt,minimum size=0pt,label=below:2] (b) at (5,1) {};
\node[inner sep=0pt,minimum size=0pt,label=above:3] (c) at (6,2) {};
\node[inner sep=0pt,minimum size=0pt,label=above:4] (d) at (5,3) {};
% Paths between nodes
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-1-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (a) -- (b);
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-2-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (b) -- (c);
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-3-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (c) -- (d);
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-4-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (d) -- (a);
% Grid
\draw (mark-1-2) -- (mark-3-9);
\draw (mark-1-3) -- (mark-3-8);
\draw (mark-1-4) -- (mark-3-7);
\draw (mark-1-5) -- (mark-3-6);
\draw (mark-1-6) -- (mark-3-5);
\draw (mark-1-7) -- (mark-3-4);
\draw (mark-1-8) -- (mark-3-3);
\draw (mark-1-9) -- (mark-3-2);
\draw (mark-2-2) -- (mark-4-9);
\draw (mark-2-3) -- (mark-4-8);
\draw (mark-2-4) -- (mark-4-7);
\draw (mark-2-5) -- (mark-4-6);
\draw (mark-2-6) -- (mark-4-5);
\draw (mark-2-7) -- (mark-4-4);
\draw (mark-2-8) -- (mark-4-3);
\draw (mark-2-9) -- (mark-4-2);
% Nodes circles
\foreach \a in {a,...,d}{%
\fill[black] (\a) circle (2pt);
}
\end{tikzpicture}
\end{document}
In this code \NrLines
is defined as the number of internal lines, which are separated by a distance equal to \UnitSegment
.
Unfortunately I could not find a way to draw the grid automatically; I tried with:
\pgfmathsetmacro{\End}{\NrLines+1}
\foreach \x in {1,2}{%
\pgfmathsetmacro{\y}{\x+2}
\foreach \i/\j in {2/\End,...,\End/2}{%
\draw (mark-\x-\i) -- (mark-\y-\j);
}
}
but there was a problem with using the result of \pgfsetmacro{}
inside a node name. Maybe someone more expert than me may suggest a solution to use this \foreach
loop.
EDIT: Finally, I improved the code to draw the grid automatically! There were two issues:
- the use of
\pgfmathsetmacro
inside a node name, solved thanks to Stefan Kottwitz's answer "Pointing to a node with calculated name, why do I get to the east, not to the center?": instead of pgfmathsetmacro
, one should use \pgfmathtruncatemacro
to remove decimal points in the result;
- the internal
\foreach
loop can not work using \foreach \i/\j in {2/\End,...,\End/2}
since it is not clear (for the solver :-) ) how \i
and \j
are changing. Therefore, \j
is evaluated in each step as a function of \i
, which is the only counter for the loop.
The new improved code is:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\def\NrLines{8}
\pgfmathsetmacro{\UnitSegment}{1/(\NrLines+1)}
\pgfmathsetmacro{\End}{\NrLines+1}
\begin{document}
\begin{tikzpicture}[draw=red]
% Nodes definition
\node[inner sep=0pt,minimum size=0pt,label=below:1] (a) at (2,0) {};
\node[inner sep=0pt,minimum size=0pt,label=below:2] (b) at (5,0) {};
\node[inner sep=0pt,minimum size=0pt,label=above:3] (c) at (4,3) {};
\node[inner sep=0pt,minimum size=0pt,label=above:4] (d) at (3,3) {};
% Paths between nodes
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-1-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (a) -- (b);
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-2-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (b) -- (c);
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-3-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (c) -- (d);
\draw [postaction={decorate},decoration={markings,
mark=between positions 0 and 1 step \UnitSegment with {
\node [inner sep=0pt,minimum size=0pt,
name=mark-4-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}]
{};}}] (d) -- (a);
% Grid
\foreach \x in {1,2}{%
\pgfmathtruncatemacro{\y}{\x+2}
\foreach \i in {2,...,\End}{%
\pgfmathtruncatemacro{\j}{\End-\i+2}
\draw (mark-\x-\i) -- (mark-\y-\j);
}
}
% Nodes circles
\foreach \a in {a,...,d}{%
\fill[black] (\a) circle (2pt);
}
\end{tikzpicture}
\end{document}
Now you can easily modify the grid setting the number of internal lines \NrLines
, and the code will work without problems!