1

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}
    \edef\paddingX{.1}
    \pgfmathsetmacro\xStep{\xStart+\xStepSize}
    \pgfmathsetmacro\xEnd{\rectH-\paddingX}

    \def\yStart{.2}
    \def\yStepSize{.3}
    \edef\paddingY{.1}
    \pgfmathsetmacro\yStep{\yStart+\xStepSize}
    \pgfmathsetmacro\yEnd{\rectH-\paddingY}

    \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:

enter image description here

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 at 16:03
2

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,
  x padding/.initial=2.5mm,
  y padding/.initial=2.5mm,
  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)$),
          \n1={int(0.5+(\x1-\pgfkeysvalueof{/automagic/x padding})/\pgfkeysvalueof{/automagic/x distance})},
          \n2={int(0.5+(\y1-\pgfkeysvalueof{/automagic/y padding})/\pgfkeysvalueof{/automagic/y distance})}
          in
          foreach \XX in {1,...,\n1}
            {
              foreach \YY in {1,...,\n2} {
                  ([
                      xshift={(\XX-1)*\pgfkeysvalueof{/automagic/x distance}+\pgfkeysvalueof{/automagic/x padding}},
                      yshift={(\YY-1)*\pgfkeysvalueof{/automagic/y distance}+\pgfkeysvalueof{/automagic/y padding}}
                    ]%
                  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}

enter image description here

  • This did not build here? :s – Andreas Storvik Strauman Apr 12 at 15:35
  • Also it would be nice with some better line breaks and indentations? :) – Andreas Storvik Strauman Apr 12 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 at 15:40
  • 1
    Just reinstalled pgf. Suddenly works! Thanks. – Andreas Storvik Strauman Apr 14 at 13:47
  • 1
    Also -- I took the liberty of indenting your code :) – Andreas Storvik Strauman Apr 14 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.