TikZ nested foreach for staggered rectangular grid
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I am trying to use TikZ to produce a staggered rectangular grid in LaTeX: (x,y) with 1<=y<=5 and y<=x<=y+5, say. I have tried the following:
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y in 1,...,5
foreach x in y, ...,10
%%if (x <= y+5)
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
%%fi
endtikzpicture
enddocument
And this gets me the staggered start. However, if I change the limit of "10" in the second foreach to "y+5", I get an error "! Illegal unit of measure (pt inserted)." If, instead of that, I remove the comment marks and apply an if test, then the diagram is empty. (If I try a simpler "if x < 8", I get the column for x==11, which I do not understand.)
I can easily believe that I haven't got the syntax right -- this is my first attempt at anything in TikZ. So, as well as the simple fix, I would appreciate some meta-advice: How do I debug this? Can you recommend an alternative package where I would not struggle to do such a simple thing? Thanks.
tikz-pgf
add a comment |Â
up vote
5
down vote
favorite
I am trying to use TikZ to produce a staggered rectangular grid in LaTeX: (x,y) with 1<=y<=5 and y<=x<=y+5, say. I have tried the following:
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y in 1,...,5
foreach x in y, ...,10
%%if (x <= y+5)
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
%%fi
endtikzpicture
enddocument
And this gets me the staggered start. However, if I change the limit of "10" in the second foreach to "y+5", I get an error "! Illegal unit of measure (pt inserted)." If, instead of that, I remove the comment marks and apply an if test, then the diagram is empty. (If I try a simpler "if x < 8", I get the column for x==11, which I do not understand.)
I can easily believe that I haven't got the syntax right -- this is my first attempt at anything in TikZ. So, as well as the simple fix, I would appreciate some meta-advice: How do I debug this? Can you recommend an alternative package where I would not struggle to do such a simple thing? Thanks.
tikz-pgf
Welcome to TeX.SE! You cannot do computations in the argument offoreach
. Just dopgfmathtruncatemacroxmaxy+5
and then replace10
byxmax
. And you need to work withifnum
instead ofif
, which works for integers.
â marmot
Aug 23 at 15:50
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I am trying to use TikZ to produce a staggered rectangular grid in LaTeX: (x,y) with 1<=y<=5 and y<=x<=y+5, say. I have tried the following:
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y in 1,...,5
foreach x in y, ...,10
%%if (x <= y+5)
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
%%fi
endtikzpicture
enddocument
And this gets me the staggered start. However, if I change the limit of "10" in the second foreach to "y+5", I get an error "! Illegal unit of measure (pt inserted)." If, instead of that, I remove the comment marks and apply an if test, then the diagram is empty. (If I try a simpler "if x < 8", I get the column for x==11, which I do not understand.)
I can easily believe that I haven't got the syntax right -- this is my first attempt at anything in TikZ. So, as well as the simple fix, I would appreciate some meta-advice: How do I debug this? Can you recommend an alternative package where I would not struggle to do such a simple thing? Thanks.
tikz-pgf
I am trying to use TikZ to produce a staggered rectangular grid in LaTeX: (x,y) with 1<=y<=5 and y<=x<=y+5, say. I have tried the following:
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y in 1,...,5
foreach x in y, ...,10
%%if (x <= y+5)
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
%%fi
endtikzpicture
enddocument
And this gets me the staggered start. However, if I change the limit of "10" in the second foreach to "y+5", I get an error "! Illegal unit of measure (pt inserted)." If, instead of that, I remove the comment marks and apply an if test, then the diagram is empty. (If I try a simpler "if x < 8", I get the column for x==11, which I do not understand.)
I can easily believe that I haven't got the syntax right -- this is my first attempt at anything in TikZ. So, as well as the simple fix, I would appreciate some meta-advice: How do I debug this? Can you recommend an alternative package where I would not struggle to do such a simple thing? Thanks.
tikz-pgf
tikz-pgf
asked Aug 23 at 15:23
Ed Wynn
1283
1283
Welcome to TeX.SE! You cannot do computations in the argument offoreach
. Just dopgfmathtruncatemacroxmaxy+5
and then replace10
byxmax
. And you need to work withifnum
instead ofif
, which works for integers.
â marmot
Aug 23 at 15:50
add a comment |Â
Welcome to TeX.SE! You cannot do computations in the argument offoreach
. Just dopgfmathtruncatemacroxmaxy+5
and then replace10
byxmax
. And you need to work withifnum
instead ofif
, which works for integers.
â marmot
Aug 23 at 15:50
Welcome to TeX.SE! You cannot do computations in the argument of
foreach
. Just do pgfmathtruncatemacroxmaxy+5
and then replace 10
by xmax
. And you need to work with ifnum
instead of if
, which works for integers.â marmot
Aug 23 at 15:50
Welcome to TeX.SE! You cannot do computations in the argument of
foreach
. Just do pgfmathtruncatemacroxmaxy+5
and then replace 10
by xmax
. And you need to work with ifnum
instead of if
, which works for integers.â marmot
Aug 23 at 15:50
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
Two reasons why your attempt did not quite give you what you want:
- You cannot do computations in the argument of
foreach
. - The
if
clause does not work like that. Since these are integers, you could have worked withifnum
, something likeifnumx<8
or so.
Here is a version that does what I think you want to do.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y [evaluate=y as xmax using int(y+5)] in 1,...,5
foreach x in y, ...,xmax
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
endtikzpicture
enddocument
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
Two reasons why your attempt did not quite give you what you want:
- You cannot do computations in the argument of
foreach
. - The
if
clause does not work like that. Since these are integers, you could have worked withifnum
, something likeifnumx<8
or so.
Here is a version that does what I think you want to do.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y [evaluate=y as xmax using int(y+5)] in 1,...,5
foreach x in y, ...,xmax
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
endtikzpicture
enddocument
add a comment |Â
up vote
9
down vote
accepted
Two reasons why your attempt did not quite give you what you want:
- You cannot do computations in the argument of
foreach
. - The
if
clause does not work like that. Since these are integers, you could have worked withifnum
, something likeifnumx<8
or so.
Here is a version that does what I think you want to do.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y [evaluate=y as xmax using int(y+5)] in 1,...,5
foreach x in y, ...,xmax
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
endtikzpicture
enddocument
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
Two reasons why your attempt did not quite give you what you want:
- You cannot do computations in the argument of
foreach
. - The
if
clause does not work like that. Since these are integers, you could have worked withifnum
, something likeifnumx<8
or so.
Here is a version that does what I think you want to do.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y [evaluate=y as xmax using int(y+5)] in 1,...,5
foreach x in y, ...,xmax
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
endtikzpicture
enddocument
Two reasons why your attempt did not quite give you what you want:
- You cannot do computations in the argument of
foreach
. - The
if
clause does not work like that. Since these are integers, you could have worked withifnum
, something likeifnumx<8
or so.
Here is a version that does what I think you want to do.
documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach y [evaluate=y as xmax using int(y+5)] in 1,...,5
foreach x in y, ...,xmax
draw (x,-y) +(-.5,-.5) rectangle ++(.5,.5);
draw (x,-y) node$a_x^y$;
endtikzpicture
enddocument
answered Aug 23 at 15:59
marmot
59.4k463128
59.4k463128
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f447351%2ftikz-nested-foreach-for-staggered-rectangular-grid%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Welcome to TeX.SE! You cannot do computations in the argument of
foreach
. Just dopgfmathtruncatemacroxmaxy+5
and then replace10
byxmax
. And you need to work withifnum
instead ofif
, which works for integers.â marmot
Aug 23 at 15:50