Repeat nodes in tikz rectangle iteratively

So I want to fill a rectangle, like below, but without all the hassle of forlooping and getting coordinates.

I want a magic command that places my symbol repeatedly inside a rectangle. Preferably given a start and end coordinate;

\automagicGrid[step size x=0.1, step size y=0.1]{(rect.south west)}{(rect.north east)}{\node{$\otimes$}}


Here is an example of a manual, terrible way to do it.

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
\makeatletter
\tikzset{autofit/.style={
% autofit: automagically generate awesome anchorpoints
fit={(\pgf@pathminx,\pgf@pathminy) (\pgf@pathmaxx,\pgf@pathmaxy)},
inner sep=0pt
}
}
\makeatother
% Draw my rectangle
\def\rectH{2}
\def\rectW{2}
\draw(0,0) rectangle (\rectW,\rectH) node[autofit](rect){};
% I want this command
% \automagicGrid[step size x=0.1, step size y=0.1]{(rect.south west)}{(rect.north east)}{\node{$\otimes$}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  Below is what I DON'T want  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\xStart{.2}
\def\xStepSize{.3}
\pgfmathsetmacro\xStep{\xStart+\xStepSize}

\def\yStart{.2}
\def\yStepSize{.3}
\pgfmathsetmacro\yStep{\yStart+\xStepSize}

\foreach \x in {\xStart,\xStep,...,\xEnd}{
\foreach \y in {\yStart, \yStep,...,\yEnd}{
\node at (\x+\paddingX/2,\y+\paddingY/2){$\otimes$};
}
}
\end{tikzpicture}
\end{document}


And the result:

I will be happy though if someone just find a clean way to fetch said coordinates, because that's the real hassle of this.

• If you REALLY don't want a loop, you could do a recursive macro, not that it would be any easier. – John Kormylo Apr 12 '19 at 16:03

I do not see how one can get rid of the \foreach but one could put them into a style. Using a path picture we now how large the thing is we want to fill, and can dial the loop parameters accordingly.

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit,calc}
\pgfkeys{/automagic/.cd,
grid/.style={draw=gray!50,thin},
x distance/.initial=3mm,
y distance/.initial=3mm,
distance/.code={\pgfkeys{/automagic/x distance=#1,/automagic/y distance=#1}},
symbol/.initial= $\otimes$
}
\tikzset{automagic grid/.style={/utils/exec=\pgfkeys{/automagic/.cd,#1},
path picture={
\path[/automagic/grid]
let
\p1=($(path picture bounding box.north east)-(path picture bounding box.south west)$),
in
foreach \XX in {1,...,\n1}
{
foreach \YY in {1,...,\n2} {
([
]%
path picture bounding box.south west%
) node{\pgfkeysvalueof{/automagic/symbol}}
}
};
}}}
\begin{document}
\begin{tikzpicture}
% Draw my rectangle
\def\rectH{2}
\def\rectW{2}
\draw[automagic grid](0,0) rectangle ++(\rectW,\rectH);
\end{tikzpicture}
\end{document}


• This did not build here? :s – Andreas Storvik Strauman Apr 12 '19 at 15:35
• Also it would be nice with some better line breaks and indentations? :) – Andreas Storvik Strauman Apr 12 '19 at 15:37
• @AndreasStorvikStrauman The line breaks and indentations are controlled by the pgfkeys x distance/.initial=3mm, y distance/.initial=3mm, x padding/.initial=2.5mm, y padding/.initial=2.5mm,, where I just aimed to reproduce the output you got. Please feel free to improve these distances to make them "better". And I do not know what you mean by "This did not build here?", my screen shot is obtained by running pdflatex on a TeXLive2018 distribution on the above code. – user121799 Apr 12 '19 at 15:40
• Just reinstalled pgf. Suddenly works! Thanks. – Andreas Storvik Strauman Apr 14 '19 at 13:47
• Also -- I took the liberty of indenting your code :) – Andreas Storvik Strauman Apr 14 '19 at 13:55