Drawing Grid Lines using foreach











up vote
2
down vote

favorite












I am trying to draw gridlines inside a square. Here is my code:



documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.15}

begin{document}

begin{figure}[ht]

centering

begin{tikzpicture}[scale=1.25,line width=1pt]
begin{axis}[
color= white,
xmin=-28.9,
xmax=28.9,
ymin=-28.9,
ymax=28.9,
axis equal image,
axis lines=middle,
]

foreach x in {-24,..., 24}
{draw[thin, gray] (x,-24) -- (x,24);}

draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);

node[black, below] at (24,-24) {$(1,0)$};

node[black, below] at (-24,-24) {$(0,0)$};

node[black, above] at (-24,24) {$(0,1)$};

node[black, above] at (24,24) {$(1,1)$};

end{axis}
end{tikzpicture}

end{figure}

end{document}


This code, however, will not compile. My goal is to make a square with a 24 x 24 grid.










share|improve this question






















  • No grid comes up when I use this. It only produces the square
    – Aiden Kenny
    1 hour ago















up vote
2
down vote

favorite












I am trying to draw gridlines inside a square. Here is my code:



documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.15}

begin{document}

begin{figure}[ht]

centering

begin{tikzpicture}[scale=1.25,line width=1pt]
begin{axis}[
color= white,
xmin=-28.9,
xmax=28.9,
ymin=-28.9,
ymax=28.9,
axis equal image,
axis lines=middle,
]

foreach x in {-24,..., 24}
{draw[thin, gray] (x,-24) -- (x,24);}

draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);

node[black, below] at (24,-24) {$(1,0)$};

node[black, below] at (-24,-24) {$(0,0)$};

node[black, above] at (-24,24) {$(0,1)$};

node[black, above] at (24,24) {$(1,1)$};

end{axis}
end{tikzpicture}

end{figure}

end{document}


This code, however, will not compile. My goal is to make a square with a 24 x 24 grid.










share|improve this question






















  • No grid comes up when I use this. It only produces the square
    – Aiden Kenny
    1 hour ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am trying to draw gridlines inside a square. Here is my code:



documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.15}

begin{document}

begin{figure}[ht]

centering

begin{tikzpicture}[scale=1.25,line width=1pt]
begin{axis}[
color= white,
xmin=-28.9,
xmax=28.9,
ymin=-28.9,
ymax=28.9,
axis equal image,
axis lines=middle,
]

foreach x in {-24,..., 24}
{draw[thin, gray] (x,-24) -- (x,24);}

draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);

node[black, below] at (24,-24) {$(1,0)$};

node[black, below] at (-24,-24) {$(0,0)$};

node[black, above] at (-24,24) {$(0,1)$};

node[black, above] at (24,24) {$(1,1)$};

end{axis}
end{tikzpicture}

end{figure}

end{document}


This code, however, will not compile. My goal is to make a square with a 24 x 24 grid.










share|improve this question













I am trying to draw gridlines inside a square. Here is my code:



documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.15}

begin{document}

begin{figure}[ht]

centering

begin{tikzpicture}[scale=1.25,line width=1pt]
begin{axis}[
color= white,
xmin=-28.9,
xmax=28.9,
ymin=-28.9,
ymax=28.9,
axis equal image,
axis lines=middle,
]

foreach x in {-24,..., 24}
{draw[thin, gray] (x,-24) -- (x,24);}

draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);

node[black, below] at (24,-24) {$(1,0)$};

node[black, below] at (-24,-24) {$(0,0)$};

node[black, above] at (-24,24) {$(0,1)$};

node[black, above] at (24,24) {$(1,1)$};

end{axis}
end{tikzpicture}

end{figure}

end{document}


This code, however, will not compile. My goal is to make a square with a 24 x 24 grid.







tikz-pgf foreach






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









Aiden Kenny

1585




1585












  • No grid comes up when I use this. It only produces the square
    – Aiden Kenny
    1 hour ago


















  • No grid comes up when I use this. It only produces the square
    – Aiden Kenny
    1 hour ago
















No grid comes up when I use this. It only produces the square
– Aiden Kenny
1 hour ago




No grid comes up when I use this. It only produces the square
– Aiden Kenny
1 hour ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Works fine if you use pgfplotsinvokeforeach instead of foreach:



enter image description here



Notes:




  • I would suggest you try the grid options built into pgfplots and tikz.

  • All the code you showing does not require pgfplots so you can eliminate the axis environment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axis environment version which uses foreach.


References:




  • foreach not behaving in axis environment


  • Tikz foreach loop and coordinate computation.


Code: pgfplots



documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.15}

begin{document}
%
%begin{figure}[ht]
%
%centering
%
begin{tikzpicture}%[scale=1.25,line width=1pt]
begin{axis}[
color= white,
xmin=-28.9,
xmax=28.9,
ymin=-28.9,
ymax=28.9,
axis equal image,
axis lines=middle,
]

pgfplotsinvokeforeach {-24,...,24} {%
draw[thin, gray] (#1,-24) -- (#1,24);
}

draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);

node[black, below] at (24,-24) {$(1,0)$};

node[black, below] at (-24,-24) {$(0,0)$};

node[black, above] at (-24,24) {$(0,1)$};

node[black, above] at (24,24) {$(1,1)$};

end{axis}
end{tikzpicture}%
%
%end{figure}
%
end{document}


Code: no pgfplots



documentclass{article}
usepackage{pgfplots}
pgfplotsset{compat=1.15}

begin{document}
begin{tikzpicture}[scale=0.10]

foreach x in {-24,...,24} {%
draw[thin, gray] (x,-24) -- (x,24);
}

draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
(-24,-24);

node[black, below] at (24,-24) {$(1,0)$};

node[black, below] at (-24,-24) {$(0,0)$};

node[black, above] at (-24,24) {$(0,1)$};

node[black, above] at (24,24) {$(1,1)$};

end{tikzpicture}%
end{document}





share|improve this answer






























    up vote
    3
    down vote













    Two other options without pgfplots, one without foreach and one with foreach:



    documentclass[tikz,border=2mm]{standalone} 
    usetikzlibrary{positioning}

    begin{document}
    begin{tikzpicture}
    draw (0,0) grid (24,24);
    node[above right] at (24,24) {$(1,1)$};
    node[above left] at (0,24) {$(0,1)$};
    node[below left] at (0,0) {$(0,0)$};
    node[below right] at (24,0) {$(1,0)$};
    end{tikzpicture}

    begin{tikzpicture}
    foreach i in {0,...,24}{
    draw (i,0) -- ++(90:24);
    draw (0,i) -- ++(0:24);
    }
    node[above right] at (24,24) {$(1,1)$};
    node[above left] at (0,24) {$(0,1)$};
    node[below left] at (0,0) {$(0,0)$};
    node[below right] at (24,0) {$(1,0)$};
    end{tikzpicture}

    end{document}


    enter image description here






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "85"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f466054%2fdrawing-grid-lines-using-foreach%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      Works fine if you use pgfplotsinvokeforeach instead of foreach:



      enter image description here



      Notes:




      • I would suggest you try the grid options built into pgfplots and tikz.

      • All the code you showing does not require pgfplots so you can eliminate the axis environment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axis environment version which uses foreach.


      References:




      • foreach not behaving in axis environment


      • Tikz foreach loop and coordinate computation.


      Code: pgfplots



      documentclass{article}
      usepackage{pgfplots}
      pgfplotsset{compat=1.15}

      begin{document}
      %
      %begin{figure}[ht]
      %
      %centering
      %
      begin{tikzpicture}%[scale=1.25,line width=1pt]
      begin{axis}[
      color= white,
      xmin=-28.9,
      xmax=28.9,
      ymin=-28.9,
      ymax=28.9,
      axis equal image,
      axis lines=middle,
      ]

      pgfplotsinvokeforeach {-24,...,24} {%
      draw[thin, gray] (#1,-24) -- (#1,24);
      }

      draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
      (-24,-24);

      node[black, below] at (24,-24) {$(1,0)$};

      node[black, below] at (-24,-24) {$(0,0)$};

      node[black, above] at (-24,24) {$(0,1)$};

      node[black, above] at (24,24) {$(1,1)$};

      end{axis}
      end{tikzpicture}%
      %
      %end{figure}
      %
      end{document}


      Code: no pgfplots



      documentclass{article}
      usepackage{pgfplots}
      pgfplotsset{compat=1.15}

      begin{document}
      begin{tikzpicture}[scale=0.10]

      foreach x in {-24,...,24} {%
      draw[thin, gray] (x,-24) -- (x,24);
      }

      draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
      (-24,-24);

      node[black, below] at (24,-24) {$(1,0)$};

      node[black, below] at (-24,-24) {$(0,0)$};

      node[black, above] at (-24,24) {$(0,1)$};

      node[black, above] at (24,24) {$(1,1)$};

      end{tikzpicture}%
      end{document}





      share|improve this answer



























        up vote
        2
        down vote



        accepted










        Works fine if you use pgfplotsinvokeforeach instead of foreach:



        enter image description here



        Notes:




        • I would suggest you try the grid options built into pgfplots and tikz.

        • All the code you showing does not require pgfplots so you can eliminate the axis environment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axis environment version which uses foreach.


        References:




        • foreach not behaving in axis environment


        • Tikz foreach loop and coordinate computation.


        Code: pgfplots



        documentclass{article}
        usepackage{pgfplots}
        pgfplotsset{compat=1.15}

        begin{document}
        %
        %begin{figure}[ht]
        %
        %centering
        %
        begin{tikzpicture}%[scale=1.25,line width=1pt]
        begin{axis}[
        color= white,
        xmin=-28.9,
        xmax=28.9,
        ymin=-28.9,
        ymax=28.9,
        axis equal image,
        axis lines=middle,
        ]

        pgfplotsinvokeforeach {-24,...,24} {%
        draw[thin, gray] (#1,-24) -- (#1,24);
        }

        draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
        (-24,-24);

        node[black, below] at (24,-24) {$(1,0)$};

        node[black, below] at (-24,-24) {$(0,0)$};

        node[black, above] at (-24,24) {$(0,1)$};

        node[black, above] at (24,24) {$(1,1)$};

        end{axis}
        end{tikzpicture}%
        %
        %end{figure}
        %
        end{document}


        Code: no pgfplots



        documentclass{article}
        usepackage{pgfplots}
        pgfplotsset{compat=1.15}

        begin{document}
        begin{tikzpicture}[scale=0.10]

        foreach x in {-24,...,24} {%
        draw[thin, gray] (x,-24) -- (x,24);
        }

        draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
        (-24,-24);

        node[black, below] at (24,-24) {$(1,0)$};

        node[black, below] at (-24,-24) {$(0,0)$};

        node[black, above] at (-24,24) {$(0,1)$};

        node[black, above] at (24,24) {$(1,1)$};

        end{tikzpicture}%
        end{document}





        share|improve this answer

























          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Works fine if you use pgfplotsinvokeforeach instead of foreach:



          enter image description here



          Notes:




          • I would suggest you try the grid options built into pgfplots and tikz.

          • All the code you showing does not require pgfplots so you can eliminate the axis environment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axis environment version which uses foreach.


          References:




          • foreach not behaving in axis environment


          • Tikz foreach loop and coordinate computation.


          Code: pgfplots



          documentclass{article}
          usepackage{pgfplots}
          pgfplotsset{compat=1.15}

          begin{document}
          %
          %begin{figure}[ht]
          %
          %centering
          %
          begin{tikzpicture}%[scale=1.25,line width=1pt]
          begin{axis}[
          color= white,
          xmin=-28.9,
          xmax=28.9,
          ymin=-28.9,
          ymax=28.9,
          axis equal image,
          axis lines=middle,
          ]

          pgfplotsinvokeforeach {-24,...,24} {%
          draw[thin, gray] (#1,-24) -- (#1,24);
          }

          draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
          (-24,-24);

          node[black, below] at (24,-24) {$(1,0)$};

          node[black, below] at (-24,-24) {$(0,0)$};

          node[black, above] at (-24,24) {$(0,1)$};

          node[black, above] at (24,24) {$(1,1)$};

          end{axis}
          end{tikzpicture}%
          %
          %end{figure}
          %
          end{document}


          Code: no pgfplots



          documentclass{article}
          usepackage{pgfplots}
          pgfplotsset{compat=1.15}

          begin{document}
          begin{tikzpicture}[scale=0.10]

          foreach x in {-24,...,24} {%
          draw[thin, gray] (x,-24) -- (x,24);
          }

          draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
          (-24,-24);

          node[black, below] at (24,-24) {$(1,0)$};

          node[black, below] at (-24,-24) {$(0,0)$};

          node[black, above] at (-24,24) {$(0,1)$};

          node[black, above] at (24,24) {$(1,1)$};

          end{tikzpicture}%
          end{document}





          share|improve this answer














          Works fine if you use pgfplotsinvokeforeach instead of foreach:



          enter image description here



          Notes:




          • I would suggest you try the grid options built into pgfplots and tikz.

          • All the code you showing does not require pgfplots so you can eliminate the axis environment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axis environment version which uses foreach.


          References:




          • foreach not behaving in axis environment


          • Tikz foreach loop and coordinate computation.


          Code: pgfplots



          documentclass{article}
          usepackage{pgfplots}
          pgfplotsset{compat=1.15}

          begin{document}
          %
          %begin{figure}[ht]
          %
          %centering
          %
          begin{tikzpicture}%[scale=1.25,line width=1pt]
          begin{axis}[
          color= white,
          xmin=-28.9,
          xmax=28.9,
          ymin=-28.9,
          ymax=28.9,
          axis equal image,
          axis lines=middle,
          ]

          pgfplotsinvokeforeach {-24,...,24} {%
          draw[thin, gray] (#1,-24) -- (#1,24);
          }

          draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
          (-24,-24);

          node[black, below] at (24,-24) {$(1,0)$};

          node[black, below] at (-24,-24) {$(0,0)$};

          node[black, above] at (-24,24) {$(0,1)$};

          node[black, above] at (24,24) {$(1,1)$};

          end{axis}
          end{tikzpicture}%
          %
          %end{figure}
          %
          end{document}


          Code: no pgfplots



          documentclass{article}
          usepackage{pgfplots}
          pgfplotsset{compat=1.15}

          begin{document}
          begin{tikzpicture}[scale=0.10]

          foreach x in {-24,...,24} {%
          draw[thin, gray] (x,-24) -- (x,24);
          }

          draw[black, thin] (-24,-24) -- (-24,24) -- (24,24) -- (24, -24) --
          (-24,-24);

          node[black, below] at (24,-24) {$(1,0)$};

          node[black, below] at (-24,-24) {$(0,0)$};

          node[black, above] at (-24,24) {$(0,1)$};

          node[black, above] at (24,24) {$(1,1)$};

          end{tikzpicture}%
          end{document}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 1 hour ago









          Peter Grill

          163k25432744




          163k25432744






















              up vote
              3
              down vote













              Two other options without pgfplots, one without foreach and one with foreach:



              documentclass[tikz,border=2mm]{standalone} 
              usetikzlibrary{positioning}

              begin{document}
              begin{tikzpicture}
              draw (0,0) grid (24,24);
              node[above right] at (24,24) {$(1,1)$};
              node[above left] at (0,24) {$(0,1)$};
              node[below left] at (0,0) {$(0,0)$};
              node[below right] at (24,0) {$(1,0)$};
              end{tikzpicture}

              begin{tikzpicture}
              foreach i in {0,...,24}{
              draw (i,0) -- ++(90:24);
              draw (0,i) -- ++(0:24);
              }
              node[above right] at (24,24) {$(1,1)$};
              node[above left] at (0,24) {$(0,1)$};
              node[below left] at (0,0) {$(0,0)$};
              node[below right] at (24,0) {$(1,0)$};
              end{tikzpicture}

              end{document}


              enter image description here






              share|improve this answer

























                up vote
                3
                down vote













                Two other options without pgfplots, one without foreach and one with foreach:



                documentclass[tikz,border=2mm]{standalone} 
                usetikzlibrary{positioning}

                begin{document}
                begin{tikzpicture}
                draw (0,0) grid (24,24);
                node[above right] at (24,24) {$(1,1)$};
                node[above left] at (0,24) {$(0,1)$};
                node[below left] at (0,0) {$(0,0)$};
                node[below right] at (24,0) {$(1,0)$};
                end{tikzpicture}

                begin{tikzpicture}
                foreach i in {0,...,24}{
                draw (i,0) -- ++(90:24);
                draw (0,i) -- ++(0:24);
                }
                node[above right] at (24,24) {$(1,1)$};
                node[above left] at (0,24) {$(0,1)$};
                node[below left] at (0,0) {$(0,0)$};
                node[below right] at (24,0) {$(1,0)$};
                end{tikzpicture}

                end{document}


                enter image description here






                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Two other options without pgfplots, one without foreach and one with foreach:



                  documentclass[tikz,border=2mm]{standalone} 
                  usetikzlibrary{positioning}

                  begin{document}
                  begin{tikzpicture}
                  draw (0,0) grid (24,24);
                  node[above right] at (24,24) {$(1,1)$};
                  node[above left] at (0,24) {$(0,1)$};
                  node[below left] at (0,0) {$(0,0)$};
                  node[below right] at (24,0) {$(1,0)$};
                  end{tikzpicture}

                  begin{tikzpicture}
                  foreach i in {0,...,24}{
                  draw (i,0) -- ++(90:24);
                  draw (0,i) -- ++(0:24);
                  }
                  node[above right] at (24,24) {$(1,1)$};
                  node[above left] at (0,24) {$(0,1)$};
                  node[below left] at (0,0) {$(0,0)$};
                  node[below right] at (24,0) {$(1,0)$};
                  end{tikzpicture}

                  end{document}


                  enter image description here






                  share|improve this answer












                  Two other options without pgfplots, one without foreach and one with foreach:



                  documentclass[tikz,border=2mm]{standalone} 
                  usetikzlibrary{positioning}

                  begin{document}
                  begin{tikzpicture}
                  draw (0,0) grid (24,24);
                  node[above right] at (24,24) {$(1,1)$};
                  node[above left] at (0,24) {$(0,1)$};
                  node[below left] at (0,0) {$(0,0)$};
                  node[below right] at (24,0) {$(1,0)$};
                  end{tikzpicture}

                  begin{tikzpicture}
                  foreach i in {0,...,24}{
                  draw (i,0) -- ++(90:24);
                  draw (0,i) -- ++(0:24);
                  }
                  node[above right] at (24,24) {$(1,1)$};
                  node[above left] at (0,24) {$(0,1)$};
                  node[below left] at (0,0) {$(0,0)$};
                  node[below right] at (24,0) {$(1,0)$};
                  end{tikzpicture}

                  end{document}


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Ignasi

                  91.1k4164303




                  91.1k4164303






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f466054%2fdrawing-grid-lines-using-foreach%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Quarter-circle Tiles

                      build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                      Mont Emei