Drawing Grid Lines using foreach
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
add a comment |
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
No grid comes up when I use this. It only produces the square
– Aiden Kenny
Dec 16 at 9:07
add a comment |
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
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
tikz-pgf foreach
asked Dec 16 at 9:02
Aiden Kenny
1685
1685
No grid comes up when I use this. It only produces the square
– Aiden Kenny
Dec 16 at 9:07
add a comment |
No grid comes up when I use this. It only produces the square
– Aiden Kenny
Dec 16 at 9:07
No grid comes up when I use this. It only produces the square
– Aiden Kenny
Dec 16 at 9:07
No grid comes up when I use this. It only produces the square
– Aiden Kenny
Dec 16 at 9:07
add a comment |
2 Answers
2
active
oldest
votes
Works fine if you use pgfplotsinvokeforeach instead of foreach:

Notes:
- I would suggest you try the
gridoptions built intopgfplotsandtikz. - All the code you showing does not require
pgfplotsso you can eliminate theaxisenvironment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axisenvironment version which usesforeach.
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}
Just curious, for usingpgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening?
– Aiden Kenny
Dec 23 at 17:09
@AidenKenny: I am guessng that you are usingpgfplotsinvokeforeachwithin a macro where#1refers to the first parameter of the macro. Thus, if you are usingpgfplotsinvokeforeachwithin a macro definition, you need to use##1to refer to the parameters of thepgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where#1would refer to the first parameter of the outer macro and##1would refe to the first parameter of the inner macro.
– Peter Grill
Dec 23 at 17:58
@AidenKenny: If that does not solve your issue, please ask a new question with a MWE includingdocumentclassthat reproduces the problem.
– Peter Grill
Dec 23 at 18:00
add a comment |
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}

add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Works fine if you use pgfplotsinvokeforeach instead of foreach:

Notes:
- I would suggest you try the
gridoptions built intopgfplotsandtikz. - All the code you showing does not require
pgfplotsso you can eliminate theaxisenvironment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axisenvironment version which usesforeach.
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}
Just curious, for usingpgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening?
– Aiden Kenny
Dec 23 at 17:09
@AidenKenny: I am guessng that you are usingpgfplotsinvokeforeachwithin a macro where#1refers to the first parameter of the macro. Thus, if you are usingpgfplotsinvokeforeachwithin a macro definition, you need to use##1to refer to the parameters of thepgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where#1would refer to the first parameter of the outer macro and##1would refe to the first parameter of the inner macro.
– Peter Grill
Dec 23 at 17:58
@AidenKenny: If that does not solve your issue, please ask a new question with a MWE includingdocumentclassthat reproduces the problem.
– Peter Grill
Dec 23 at 18:00
add a comment |
Works fine if you use pgfplotsinvokeforeach instead of foreach:

Notes:
- I would suggest you try the
gridoptions built intopgfplotsandtikz. - All the code you showing does not require
pgfplotsso you can eliminate theaxisenvironment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axisenvironment version which usesforeach.
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}
Just curious, for usingpgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening?
– Aiden Kenny
Dec 23 at 17:09
@AidenKenny: I am guessng that you are usingpgfplotsinvokeforeachwithin a macro where#1refers to the first parameter of the macro. Thus, if you are usingpgfplotsinvokeforeachwithin a macro definition, you need to use##1to refer to the parameters of thepgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where#1would refer to the first parameter of the outer macro and##1would refe to the first parameter of the inner macro.
– Peter Grill
Dec 23 at 17:58
@AidenKenny: If that does not solve your issue, please ask a new question with a MWE includingdocumentclassthat reproduces the problem.
– Peter Grill
Dec 23 at 18:00
add a comment |
Works fine if you use pgfplotsinvokeforeach instead of foreach:

Notes:
- I would suggest you try the
gridoptions built intopgfplotsandtikz. - All the code you showing does not require
pgfplotsso you can eliminate theaxisenvironment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axisenvironment version which usesforeach.
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}
Works fine if you use pgfplotsinvokeforeach instead of foreach:

Notes:
- I would suggest you try the
gridoptions built intopgfplotsandtikz. - All the code you showing does not require
pgfplotsso you can eliminate theaxisenvironment. So, unless there is other functionality that is not shown in the MWE, you should consider the non-axisenvironment version which usesforeach.
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}
edited Dec 16 at 9:32
answered Dec 16 at 9:13
Peter Grill
164k25434746
164k25434746
Just curious, for usingpgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening?
– Aiden Kenny
Dec 23 at 17:09
@AidenKenny: I am guessng that you are usingpgfplotsinvokeforeachwithin a macro where#1refers to the first parameter of the macro. Thus, if you are usingpgfplotsinvokeforeachwithin a macro definition, you need to use##1to refer to the parameters of thepgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where#1would refer to the first parameter of the outer macro and##1would refe to the first parameter of the inner macro.
– Peter Grill
Dec 23 at 17:58
@AidenKenny: If that does not solve your issue, please ask a new question with a MWE includingdocumentclassthat reproduces the problem.
– Peter Grill
Dec 23 at 18:00
add a comment |
Just curious, for usingpgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening?
– Aiden Kenny
Dec 23 at 17:09
@AidenKenny: I am guessng that you are usingpgfplotsinvokeforeachwithin a macro where#1refers to the first parameter of the macro. Thus, if you are usingpgfplotsinvokeforeachwithin a macro definition, you need to use##1to refer to the parameters of thepgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where#1would refer to the first parameter of the outer macro and##1would refe to the first parameter of the inner macro.
– Peter Grill
Dec 23 at 17:58
@AidenKenny: If that does not solve your issue, please ask a new question with a MWE includingdocumentclassthat reproduces the problem.
– Peter Grill
Dec 23 at 18:00
Just curious, for using
pgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening?– Aiden Kenny
Dec 23 at 17:09
Just curious, for using
pgfplotsinvokeforeach, the resulting drawing is what I want, but I get the error message: "Illegal parameter number in definition of test". It then says "You meant to type ## instead of #, right?" Do you have any idea why this is happening?– Aiden Kenny
Dec 23 at 17:09
@AidenKenny: I am guessng that you are using
pgfplotsinvokeforeach within a macro where #1 refers to the first parameter of the macro. Thus, if you are using pgfplotsinvokeforeach within a macro definition, you need to use ##1 to refer to the parameters of the pgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where #1 would refer to the first parameter of the outer macro and ##1 would refe to the first parameter of the inner macro.– Peter Grill
Dec 23 at 17:58
@AidenKenny: I am guessng that you are using
pgfplotsinvokeforeach within a macro where #1 refers to the first parameter of the macro. Thus, if you are using pgfplotsinvokeforeach within a macro definition, you need to use ##1 to refer to the parameters of the pgfplotsinvokeforeach. This is similar to the issue with defening a macro within a macro: where #1 would refer to the first parameter of the outer macro and ##1 would refe to the first parameter of the inner macro.– Peter Grill
Dec 23 at 17:58
@AidenKenny: If that does not solve your issue, please ask a new question with a MWE including
documentclass that reproduces the problem.– Peter Grill
Dec 23 at 18:00
@AidenKenny: If that does not solve your issue, please ask a new question with a MWE including
documentclass that reproduces the problem.– Peter Grill
Dec 23 at 18:00
add a comment |
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}

add a comment |
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}

add a comment |
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}

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}

answered Dec 16 at 9:18
Ignasi
91.5k4165303
91.5k4165303
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
No grid comes up when I use this. It only produces the square
– Aiden Kenny
Dec 16 at 9:07