Tikz: Zero-padding node labels?












5














I define an array in the following way:



begin{tikzpicture}
defn{10}
pgfmathparse{int(n-1)}
foreach x in {0,...,pgfmathresult} {
foreach y in {0,...,pgfmathresult} {
pgfmathtruncatemacro{nodelabel}{x + y*n}
node at (x,y) (x) {cnodelabel};
}
}
end{tikzpicture}


How can can I zero-pad the node labels such that I get c001, c010 etc.?










share|improve this question






















  • node at (x,y) (x) {c0nodelabel};?
    – marmot
    Nov 30 at 14:32






  • 1




    Welcome to TeX.se. For your future questions, please don't post code fragments. Instead put them into complete compilable documents as I did in my answer. This makes it a lot easier for people to help you.
    – Alan Munn
    Nov 30 at 15:14
















5














I define an array in the following way:



begin{tikzpicture}
defn{10}
pgfmathparse{int(n-1)}
foreach x in {0,...,pgfmathresult} {
foreach y in {0,...,pgfmathresult} {
pgfmathtruncatemacro{nodelabel}{x + y*n}
node at (x,y) (x) {cnodelabel};
}
}
end{tikzpicture}


How can can I zero-pad the node labels such that I get c001, c010 etc.?










share|improve this question






















  • node at (x,y) (x) {c0nodelabel};?
    – marmot
    Nov 30 at 14:32






  • 1




    Welcome to TeX.se. For your future questions, please don't post code fragments. Instead put them into complete compilable documents as I did in my answer. This makes it a lot easier for people to help you.
    – Alan Munn
    Nov 30 at 15:14














5












5








5







I define an array in the following way:



begin{tikzpicture}
defn{10}
pgfmathparse{int(n-1)}
foreach x in {0,...,pgfmathresult} {
foreach y in {0,...,pgfmathresult} {
pgfmathtruncatemacro{nodelabel}{x + y*n}
node at (x,y) (x) {cnodelabel};
}
}
end{tikzpicture}


How can can I zero-pad the node labels such that I get c001, c010 etc.?










share|improve this question













I define an array in the following way:



begin{tikzpicture}
defn{10}
pgfmathparse{int(n-1)}
foreach x in {0,...,pgfmathresult} {
foreach y in {0,...,pgfmathresult} {
pgfmathtruncatemacro{nodelabel}{x + y*n}
node at (x,y) (x) {cnodelabel};
}
}
end{tikzpicture}


How can can I zero-pad the node labels such that I get c001, c010 etc.?







tikz-pgf labels






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 30 at 14:08









loris

284




284












  • node at (x,y) (x) {c0nodelabel};?
    – marmot
    Nov 30 at 14:32






  • 1




    Welcome to TeX.se. For your future questions, please don't post code fragments. Instead put them into complete compilable documents as I did in my answer. This makes it a lot easier for people to help you.
    – Alan Munn
    Nov 30 at 15:14


















  • node at (x,y) (x) {c0nodelabel};?
    – marmot
    Nov 30 at 14:32






  • 1




    Welcome to TeX.se. For your future questions, please don't post code fragments. Instead put them into complete compilable documents as I did in my answer. This makes it a lot easier for people to help you.
    – Alan Munn
    Nov 30 at 15:14
















node at (x,y) (x) {c0nodelabel};?
– marmot
Nov 30 at 14:32




node at (x,y) (x) {c0nodelabel};?
– marmot
Nov 30 at 14:32




1




1




Welcome to TeX.se. For your future questions, please don't post code fragments. Instead put them into complete compilable documents as I did in my answer. This makes it a lot easier for people to help you.
– Alan Munn
Nov 30 at 15:14




Welcome to TeX.se. For your future questions, please don't post code fragments. Instead put them into complete compilable documents as I did in my answer. This makes it a lot easier for people to help you.
– Alan Munn
Nov 30 at 15:14










3 Answers
3






active

oldest

votes


















3














Another solution with siuntix:



documentclass{article}
usepackage{siunitx}
usepackage{tikz}

begin{document}
begin{tikzpicture}
defn{10}
pgfmathparse{int(n-1)}
foreach x in {0,...,pgfmathresult} {
foreach y [evaluate=y as ni using {int(x+y*n)}] in {0,...,pgfmathresult} {
node at (x,y) {cnum[minimum-integer-digits=3]{ni}};
}
}
end{tikzpicture}

end{document}


enter image description here






share|improve this answer





























    4














    Adapting the PGF answer given here: How to output a counter with leading zeros? we can use the same approach with your example. Instead of using pgfmathtruncatemacro I've use pgfmathsetcounter and then used the base conversion to pad the zeros.



    documentclass{article}
    usepackage{tikz}
    newcounter{nodelabel}
    begin{document}
    begin{tikzpicture}
    defn{10}
    pgfmathsetbasenumberlength{3}
    pgfmathparse{int(n-1)}
    foreach x in {0,...,pgfmathresult} {
    foreach y in {0,...,pgfmathresult} {
    pgfmathsetcounter{nodelabel}{x + y*n}
    pgfmathbasetodecnodelabel{thevalue{nodelabel}}{10}%
    node at (x,y) (x) {cnodelabel};
    }
    }
    end{tikzpicture}
    end{document}


    enter image description here






    share|improve this answer





























      2














      Update:



      I may have misunderstood the question, because labels can be written naturally like this:



      documentclass{article}
      usepackage{tikz}
      begin{document}
      begin{tikzpicture}
      defn{10}
      pgfmathparse{int(n-1)}
      foreach x in {0,...,pgfmathresult} {
      foreach y in {0,...,pgfmathresult} {
      node at (x,y) (x) {c0yx};
      }
      }
      end{tikzpicture}
      end{document}


      Old answer:



      You can use the macro opprint from the xlop package that prints the numbers as they are written useless zeros included.
      For example 00000.000 will be written 00000.000



      documentclass{article}
      usepackage{tikz}
      usepackage{xlop}
      begin{document}
      begin{tikzpicture}
      defn{10}
      pgfmathparse{int(n-1)}
      foreach x in {0,...,pgfmathresult} {
      foreach y in {0,...,pgfmathresult} {
      node at (x,y) (x) {copprint{0yx}};
      }
      }
      end{tikzpicture}
      end{document}


      array






      share|improve this answer























      • In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general.
        – loris
        Dec 3 at 8:53












      • @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part?
        – AndréC
        Dec 3 at 13:48










      • I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all.
        – loris
        Dec 4 at 9:25











      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
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f462563%2ftikz-zero-padding-node-labels%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Another solution with siuntix:



      documentclass{article}
      usepackage{siunitx}
      usepackage{tikz}

      begin{document}
      begin{tikzpicture}
      defn{10}
      pgfmathparse{int(n-1)}
      foreach x in {0,...,pgfmathresult} {
      foreach y [evaluate=y as ni using {int(x+y*n)}] in {0,...,pgfmathresult} {
      node at (x,y) {cnum[minimum-integer-digits=3]{ni}};
      }
      }
      end{tikzpicture}

      end{document}


      enter image description here






      share|improve this answer


























        3














        Another solution with siuntix:



        documentclass{article}
        usepackage{siunitx}
        usepackage{tikz}

        begin{document}
        begin{tikzpicture}
        defn{10}
        pgfmathparse{int(n-1)}
        foreach x in {0,...,pgfmathresult} {
        foreach y [evaluate=y as ni using {int(x+y*n)}] in {0,...,pgfmathresult} {
        node at (x,y) {cnum[minimum-integer-digits=3]{ni}};
        }
        }
        end{tikzpicture}

        end{document}


        enter image description here






        share|improve this answer
























          3












          3








          3






          Another solution with siuntix:



          documentclass{article}
          usepackage{siunitx}
          usepackage{tikz}

          begin{document}
          begin{tikzpicture}
          defn{10}
          pgfmathparse{int(n-1)}
          foreach x in {0,...,pgfmathresult} {
          foreach y [evaluate=y as ni using {int(x+y*n)}] in {0,...,pgfmathresult} {
          node at (x,y) {cnum[minimum-integer-digits=3]{ni}};
          }
          }
          end{tikzpicture}

          end{document}


          enter image description here






          share|improve this answer












          Another solution with siuntix:



          documentclass{article}
          usepackage{siunitx}
          usepackage{tikz}

          begin{document}
          begin{tikzpicture}
          defn{10}
          pgfmathparse{int(n-1)}
          foreach x in {0,...,pgfmathresult} {
          foreach y [evaluate=y as ni using {int(x+y*n)}] in {0,...,pgfmathresult} {
          node at (x,y) {cnum[minimum-integer-digits=3]{ni}};
          }
          }
          end{tikzpicture}

          end{document}


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 30 at 18:24









          Ignasi

          91.5k4165303




          91.5k4165303























              4














              Adapting the PGF answer given here: How to output a counter with leading zeros? we can use the same approach with your example. Instead of using pgfmathtruncatemacro I've use pgfmathsetcounter and then used the base conversion to pad the zeros.



              documentclass{article}
              usepackage{tikz}
              newcounter{nodelabel}
              begin{document}
              begin{tikzpicture}
              defn{10}
              pgfmathsetbasenumberlength{3}
              pgfmathparse{int(n-1)}
              foreach x in {0,...,pgfmathresult} {
              foreach y in {0,...,pgfmathresult} {
              pgfmathsetcounter{nodelabel}{x + y*n}
              pgfmathbasetodecnodelabel{thevalue{nodelabel}}{10}%
              node at (x,y) (x) {cnodelabel};
              }
              }
              end{tikzpicture}
              end{document}


              enter image description here






              share|improve this answer


























                4














                Adapting the PGF answer given here: How to output a counter with leading zeros? we can use the same approach with your example. Instead of using pgfmathtruncatemacro I've use pgfmathsetcounter and then used the base conversion to pad the zeros.



                documentclass{article}
                usepackage{tikz}
                newcounter{nodelabel}
                begin{document}
                begin{tikzpicture}
                defn{10}
                pgfmathsetbasenumberlength{3}
                pgfmathparse{int(n-1)}
                foreach x in {0,...,pgfmathresult} {
                foreach y in {0,...,pgfmathresult} {
                pgfmathsetcounter{nodelabel}{x + y*n}
                pgfmathbasetodecnodelabel{thevalue{nodelabel}}{10}%
                node at (x,y) (x) {cnodelabel};
                }
                }
                end{tikzpicture}
                end{document}


                enter image description here






                share|improve this answer
























                  4












                  4








                  4






                  Adapting the PGF answer given here: How to output a counter with leading zeros? we can use the same approach with your example. Instead of using pgfmathtruncatemacro I've use pgfmathsetcounter and then used the base conversion to pad the zeros.



                  documentclass{article}
                  usepackage{tikz}
                  newcounter{nodelabel}
                  begin{document}
                  begin{tikzpicture}
                  defn{10}
                  pgfmathsetbasenumberlength{3}
                  pgfmathparse{int(n-1)}
                  foreach x in {0,...,pgfmathresult} {
                  foreach y in {0,...,pgfmathresult} {
                  pgfmathsetcounter{nodelabel}{x + y*n}
                  pgfmathbasetodecnodelabel{thevalue{nodelabel}}{10}%
                  node at (x,y) (x) {cnodelabel};
                  }
                  }
                  end{tikzpicture}
                  end{document}


                  enter image description here






                  share|improve this answer












                  Adapting the PGF answer given here: How to output a counter with leading zeros? we can use the same approach with your example. Instead of using pgfmathtruncatemacro I've use pgfmathsetcounter and then used the base conversion to pad the zeros.



                  documentclass{article}
                  usepackage{tikz}
                  newcounter{nodelabel}
                  begin{document}
                  begin{tikzpicture}
                  defn{10}
                  pgfmathsetbasenumberlength{3}
                  pgfmathparse{int(n-1)}
                  foreach x in {0,...,pgfmathresult} {
                  foreach y in {0,...,pgfmathresult} {
                  pgfmathsetcounter{nodelabel}{x + y*n}
                  pgfmathbasetodecnodelabel{thevalue{nodelabel}}{10}%
                  node at (x,y) (x) {cnodelabel};
                  }
                  }
                  end{tikzpicture}
                  end{document}


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 30 at 14:33









                  Alan Munn

                  158k27423698




                  158k27423698























                      2














                      Update:



                      I may have misunderstood the question, because labels can be written naturally like this:



                      documentclass{article}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {c0yx};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      Old answer:



                      You can use the macro opprint from the xlop package that prints the numbers as they are written useless zeros included.
                      For example 00000.000 will be written 00000.000



                      documentclass{article}
                      usepackage{tikz}
                      usepackage{xlop}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {copprint{0yx}};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      array






                      share|improve this answer























                      • In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general.
                        – loris
                        Dec 3 at 8:53












                      • @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part?
                        – AndréC
                        Dec 3 at 13:48










                      • I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all.
                        – loris
                        Dec 4 at 9:25
















                      2














                      Update:



                      I may have misunderstood the question, because labels can be written naturally like this:



                      documentclass{article}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {c0yx};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      Old answer:



                      You can use the macro opprint from the xlop package that prints the numbers as they are written useless zeros included.
                      For example 00000.000 will be written 00000.000



                      documentclass{article}
                      usepackage{tikz}
                      usepackage{xlop}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {copprint{0yx}};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      array






                      share|improve this answer























                      • In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general.
                        – loris
                        Dec 3 at 8:53












                      • @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part?
                        – AndréC
                        Dec 3 at 13:48










                      • I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all.
                        – loris
                        Dec 4 at 9:25














                      2












                      2








                      2






                      Update:



                      I may have misunderstood the question, because labels can be written naturally like this:



                      documentclass{article}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {c0yx};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      Old answer:



                      You can use the macro opprint from the xlop package that prints the numbers as they are written useless zeros included.
                      For example 00000.000 will be written 00000.000



                      documentclass{article}
                      usepackage{tikz}
                      usepackage{xlop}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {copprint{0yx}};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      array






                      share|improve this answer














                      Update:



                      I may have misunderstood the question, because labels can be written naturally like this:



                      documentclass{article}
                      usepackage{tikz}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {c0yx};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      Old answer:



                      You can use the macro opprint from the xlop package that prints the numbers as they are written useless zeros included.
                      For example 00000.000 will be written 00000.000



                      documentclass{article}
                      usepackage{tikz}
                      usepackage{xlop}
                      begin{document}
                      begin{tikzpicture}
                      defn{10}
                      pgfmathparse{int(n-1)}
                      foreach x in {0,...,pgfmathresult} {
                      foreach y in {0,...,pgfmathresult} {
                      node at (x,y) (x) {copprint{0yx}};
                      }
                      }
                      end{tikzpicture}
                      end{document}


                      array







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 30 at 19:20

























                      answered Nov 30 at 19:10









                      AndréC

                      7,26211340




                      7,26211340












                      • In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general.
                        – loris
                        Dec 3 at 8:53












                      • @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part?
                        – AndréC
                        Dec 3 at 13:48










                      • I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all.
                        – loris
                        Dec 4 at 9:25


















                      • In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general.
                        – loris
                        Dec 3 at 8:53












                      • @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part?
                        – AndréC
                        Dec 3 at 13:48










                      • I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all.
                        – loris
                        Dec 4 at 9:25
















                      In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general.
                      – loris
                      Dec 3 at 8:53






                      In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general.
                      – loris
                      Dec 3 at 8:53














                      @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part?
                      – AndréC
                      Dec 3 at 13:48




                      @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part?
                      – AndréC
                      Dec 3 at 13:48












                      I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all.
                      – loris
                      Dec 4 at 9:25




                      I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all.
                      – loris
                      Dec 4 at 9:25


















                      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%2f462563%2ftikz-zero-padding-node-labels%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

                      Сан-Квентин

                      8-я гвардейская общевойсковая армия

                      Алькесар