TikZ nested foreach for staggered rectangular grid

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
2












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.










share|improve this question





















  • 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















up vote
5
down vote

favorite
2












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.










share|improve this question





















  • 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













up vote
5
down vote

favorite
2









up vote
5
down vote

favorite
2






2





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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 23 at 15:23









Ed Wynn

1283




1283











  • 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
















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











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:



  1. You cannot do computations in the argument of foreach.

  2. The if clause does not work like that. Since these are integers, you could have worked with ifnum, something like ifnumx<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


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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f447351%2ftikz-nested-foreach-for-staggered-rectangular-grid%23new-answer', 'question_page');

    );

    Post as a guest






























    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:



    1. You cannot do computations in the argument of foreach.

    2. The if clause does not work like that. Since these are integers, you could have worked with ifnum, something like ifnumx<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


    enter image description here






    share|improve this answer
























      up vote
      9
      down vote



      accepted










      Two reasons why your attempt did not quite give you what you want:



      1. You cannot do computations in the argument of foreach.

      2. The if clause does not work like that. Since these are integers, you could have worked with ifnum, something like ifnumx<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


      enter image description here






      share|improve this answer






















        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:



        1. You cannot do computations in the argument of foreach.

        2. The if clause does not work like that. Since these are integers, you could have worked with ifnum, something like ifnumx<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


        enter image description here






        share|improve this answer












        Two reasons why your attempt did not quite give you what you want:



        1. You cannot do computations in the argument of foreach.

        2. The if clause does not work like that. Since these are integers, you could have worked with ifnum, something like ifnumx<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


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 23 at 15:59









        marmot

        59.4k463128




        59.4k463128



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?