0

I used the following code to draw a grid, and to fill certain cells with certain fills or shapes.

    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{calc,shapes}
    \begin{document}
    \tikzset{
      pics/square/.default={1.5},
      pics/square/.style = {
        code = {
        \draw[pic actions] (0,0) rectangle (#1,#1);
    }}}    
    \begin{center}
    \begin{tikzpicture}
    \foreach \x in {1,2,...,5}  {
     \foreach \y in {1,2,...,5} {
       \pic[fill=white] at (\x,\y) {square};
     }
   }
\node[regular polygon,regular polygon sides=4,draw=black,thick, fill=blue,minimum size=.8cm] at (5.5,4.5) {};
\pic[draw=black,thick,fill=red] at (2,2) {square};
\end{tikzpicture}
\end{center}
\end{document}

When I used the code

pics/square/.default={1},

I got a square grid.

enter image description here

But when I used the code

pics/square/.default={1.5},

to get a larger grid size, the grid became rectangles not squares.

enter image description here

How can this be fixed by increasing the default grid size, without increasing the scale of the \tikzpicture.

SebGlav
  • 19,186
Hany
  • 4,709
  • You create your squares with foreach loops and you use {1,2,...} inside them. This is the issue. You have to modify your increment into the foreach loops, depending on your default square siez. – SebGlav Dec 29 '21 at 08:19

2 Answers2

2

You can use the grid-command to draw the grid and then define commands to make the squares. In the below I use \sqw to scale the squares in the grid, here set to 1.5.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\newcommand\sqw{1}
\newcommand\square[4][1]{\fill[#4] (#2*\sqw,#3*\sqw) rectangle +(#1*\sqw,#1*\sqw);}
\begin{document}
\renewcommand\sqw{1.5}
\begin{tikzpicture}
  \draw[step=\sqw] (0,0) grid (5*\sqw, 5*\sqw);
  \square{2}{1}{red}
  \square[0.6]{1.2}{3.2}{blue}
\end{tikzpicture}
\end{document}

enter image description here

EDIT

With use of the picand node commands as in the question you do more or less the same. In the question it is specifically said you should not scale the picture environment, and then everything within it must scale.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\newcommand\sqw{1}
\tikzset{
      pics/square/.default={\sqw},
      pics/square/.style = {
        code = {
        \draw[pic actions] (0,0) rectangle (#1,#1);
    }}}   
\begin{document}
\renewcommand\sqw{1.5}
\begin{tikzpicture}
  \draw[step=\sqw] (\sqw,\sqw) grid (6*\sqw, 6*\sqw);
  \node[regular polygon,regular polygon sides=4,draw=black,thick, fill=blue,minimum size=.8*\sqw cm] at (5.5*\sqw,4.5*\sqw) {};
  \pic[draw=black,thick,fill=red] at (2*\sqw,2*\sqw) {square};
\end{tikzpicture}
\end{document}
StefanH
  • 13,823
  • @StefanHThank you for your solution. How can I insert the \node and \pic code lines in your solution. Because this is just an example of my file using these codes with many polygon and star shapes, not just squares. I mean I want to use addresses of the grid cells (as mentioned in my post), not calculating each cell separately for each node or pic. – Hany Dec 29 '21 at 09:40
  • @Hany I have updated. It is more or less as before, but you will need to scale inside the picture if you don't want to scale the whole. – StefanH Dec 29 '21 at 09:53
  • @StefanHThank you Stefan very much. – Hany Dec 29 '21 at 15:06
0

Modify your foreach loops to match with your default square size.

   \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{calc,shapes}
    \begin{document}
    \tikzset{
      pics/square/.default={1.5},
      pics/square/.style = {
        code = {
        \draw[pic actions] (0,0) rectangle (#1,#1);
    }}}    
    \begin{center}
    \begin{tikzpicture}
    \foreach \x in {1,2.5,...,7.5}  {
     \foreach \y in {1,2.5,...,7.5} {
       \pic[fill=white] at (\x,\y) {square};
     }
   }
\node[regular polygon,regular polygon sides=4,draw=black,thick, fill=blue,minimum size=.8cm] at (6.25,6.25) {};
\pic[draw=black,thick,fill=red] at (2.5,2.5) {square};
\end{tikzpicture}
\end{center}
\end{document}

SebGlav
  • 19,186