Arithmetic inside tikz

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











up vote
2
down vote

favorite












I'm having difficulty performing arithmetic inside tikz.



I've written:



 foreach s in 6,...,8

node[draw,red,circle] at (0:(s-2)cm) $s$;



Somehow the arithmetic (s-2) is not working, I think its the cm that's causing the issue but I need the measurement in cm.



Also if I write:



 node[draw,red,circle] at (0:s cm) $(s-2)$;


the arithmetic does not work and LaTeX just merrily shows (6-2) (7-2) etc...










share|improve this question









New contributor




user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Welcome to TeX.SE! Please make your code snippet compilable ...
    – Kurt
    3 hours ago






  • 2




    Here, you must use braces around your formula (tex.stackexchange.com/a/218542/14500) and multiply by 1cm: (0:(s-2)*1cm) .
    – Paul Gaborit
    3 hours ago














up vote
2
down vote

favorite












I'm having difficulty performing arithmetic inside tikz.



I've written:



 foreach s in 6,...,8

node[draw,red,circle] at (0:(s-2)cm) $s$;



Somehow the arithmetic (s-2) is not working, I think its the cm that's causing the issue but I need the measurement in cm.



Also if I write:



 node[draw,red,circle] at (0:s cm) $(s-2)$;


the arithmetic does not work and LaTeX just merrily shows (6-2) (7-2) etc...










share|improve this question









New contributor




user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Welcome to TeX.SE! Please make your code snippet compilable ...
    – Kurt
    3 hours ago






  • 2




    Here, you must use braces around your formula (tex.stackexchange.com/a/218542/14500) and multiply by 1cm: (0:(s-2)*1cm) .
    – Paul Gaborit
    3 hours ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm having difficulty performing arithmetic inside tikz.



I've written:



 foreach s in 6,...,8

node[draw,red,circle] at (0:(s-2)cm) $s$;



Somehow the arithmetic (s-2) is not working, I think its the cm that's causing the issue but I need the measurement in cm.



Also if I write:



 node[draw,red,circle] at (0:s cm) $(s-2)$;


the arithmetic does not work and LaTeX just merrily shows (6-2) (7-2) etc...










share|improve this question









New contributor




user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm having difficulty performing arithmetic inside tikz.



I've written:



 foreach s in 6,...,8

node[draw,red,circle] at (0:(s-2)cm) $s$;



Somehow the arithmetic (s-2) is not working, I think its the cm that's causing the issue but I need the measurement in cm.



Also if I write:



 node[draw,red,circle] at (0:s cm) $(s-2)$;


the arithmetic does not work and LaTeX just merrily shows (6-2) (7-2) etc...







tikz-pgf foreach






share|improve this question









New contributor




user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago









CarLaTeX

26.2k444116




26.2k444116






New contributor




user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









user50123

111




111




New contributor




user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






user50123 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    Welcome to TeX.SE! Please make your code snippet compilable ...
    – Kurt
    3 hours ago






  • 2




    Here, you must use braces around your formula (tex.stackexchange.com/a/218542/14500) and multiply by 1cm: (0:(s-2)*1cm) .
    – Paul Gaborit
    3 hours ago












  • 1




    Welcome to TeX.SE! Please make your code snippet compilable ...
    – Kurt
    3 hours ago






  • 2




    Here, you must use braces around your formula (tex.stackexchange.com/a/218542/14500) and multiply by 1cm: (0:(s-2)*1cm) .
    – Paul Gaborit
    3 hours ago







1




1




Welcome to TeX.SE! Please make your code snippet compilable ...
– Kurt
3 hours ago




Welcome to TeX.SE! Please make your code snippet compilable ...
– Kurt
3 hours ago




2




2




Here, you must use braces around your formula (tex.stackexchange.com/a/218542/14500) and multiply by 1cm: (0:(s-2)*1cm) .
– Paul Gaborit
3 hours ago




Here, you must use braces around your formula (tex.stackexchange.com/a/218542/14500) and multiply by 1cm: (0:(s-2)*1cm) .
– Paul Gaborit
3 hours ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote













You can't use arithmetic as you shown directly (in the description of a node or if you explicitly put a unit of measure after it).



Just use evaluate option of the foreach macro.



Here I create a new variable mys which assumes the value s-2.



In my first tikzpicture, mys is used for positioning; in the second, for description:



documentclassarticle
usepackagetikz
begindocument
begintikzpicture
foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

node[draw,red,circle] at (0:mys cm) $s$;

endtikzpicture

begintikzpicture
foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

node[draw,red,circle] at (0:s cm) $mys$;

endtikzpicture
enddocument


enter image description here






share|improve this answer





























    up vote
    1
    down vote













    The unit of tikz is the cm, so it is useless to specify it. As for the calculations, they are done here without the need for brackets.



    documentclass[tikz,border=5mm]standalone
    usetikzlibraryshadows,fadings
    usetikzlibrarycalc
    usetikzlibrarypositioning

    begindocument
    begintikzpicture
    node[draw,blue,circle] at(0:2cm)4;
    node[draw,blue,circle] at(0:3)5;
    node[draw,blue,circle] at(0:4cm)6;
    foreach s in 6,...,8

    node[fill=red,circle,opacity=.5] at (0:s-2) $s$;

    endtikzpicture
    enddocument


    If you still want to specify the unit, it is necessary to do it after each of the Numbers: (0:s cm-2cm) $s$;



    Or to do as indicated by @PaulGaborit by factoring the unit: (0:(s-2)*1cm)



    calculs






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



      );






      user50123 is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f457021%2farithmetic-inside-tikz%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      You can't use arithmetic as you shown directly (in the description of a node or if you explicitly put a unit of measure after it).



      Just use evaluate option of the foreach macro.



      Here I create a new variable mys which assumes the value s-2.



      In my first tikzpicture, mys is used for positioning; in the second, for description:



      documentclassarticle
      usepackagetikz
      begindocument
      begintikzpicture
      foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

      node[draw,red,circle] at (0:mys cm) $s$;

      endtikzpicture

      begintikzpicture
      foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

      node[draw,red,circle] at (0:s cm) $mys$;

      endtikzpicture
      enddocument


      enter image description here






      share|improve this answer


























        up vote
        2
        down vote













        You can't use arithmetic as you shown directly (in the description of a node or if you explicitly put a unit of measure after it).



        Just use evaluate option of the foreach macro.



        Here I create a new variable mys which assumes the value s-2.



        In my first tikzpicture, mys is used for positioning; in the second, for description:



        documentclassarticle
        usepackagetikz
        begindocument
        begintikzpicture
        foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

        node[draw,red,circle] at (0:mys cm) $s$;

        endtikzpicture

        begintikzpicture
        foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

        node[draw,red,circle] at (0:s cm) $mys$;

        endtikzpicture
        enddocument


        enter image description here






        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          You can't use arithmetic as you shown directly (in the description of a node or if you explicitly put a unit of measure after it).



          Just use evaluate option of the foreach macro.



          Here I create a new variable mys which assumes the value s-2.



          In my first tikzpicture, mys is used for positioning; in the second, for description:



          documentclassarticle
          usepackagetikz
          begindocument
          begintikzpicture
          foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

          node[draw,red,circle] at (0:mys cm) $s$;

          endtikzpicture

          begintikzpicture
          foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

          node[draw,red,circle] at (0:s cm) $mys$;

          endtikzpicture
          enddocument


          enter image description here






          share|improve this answer














          You can't use arithmetic as you shown directly (in the description of a node or if you explicitly put a unit of measure after it).



          Just use evaluate option of the foreach macro.



          Here I create a new variable mys which assumes the value s-2.



          In my first tikzpicture, mys is used for positioning; in the second, for description:



          documentclassarticle
          usepackagetikz
          begindocument
          begintikzpicture
          foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

          node[draw,red,circle] at (0:mys cm) $s$;

          endtikzpicture

          begintikzpicture
          foreach[evaluate=s as mys using int(s-2)] s in 6,...,8

          node[draw,red,circle] at (0:s cm) $mys$;

          endtikzpicture
          enddocument


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 3 hours ago









          CarLaTeX

          26.2k444116




          26.2k444116




















              up vote
              1
              down vote













              The unit of tikz is the cm, so it is useless to specify it. As for the calculations, they are done here without the need for brackets.



              documentclass[tikz,border=5mm]standalone
              usetikzlibraryshadows,fadings
              usetikzlibrarycalc
              usetikzlibrarypositioning

              begindocument
              begintikzpicture
              node[draw,blue,circle] at(0:2cm)4;
              node[draw,blue,circle] at(0:3)5;
              node[draw,blue,circle] at(0:4cm)6;
              foreach s in 6,...,8

              node[fill=red,circle,opacity=.5] at (0:s-2) $s$;

              endtikzpicture
              enddocument


              If you still want to specify the unit, it is necessary to do it after each of the Numbers: (0:s cm-2cm) $s$;



              Or to do as indicated by @PaulGaborit by factoring the unit: (0:(s-2)*1cm)



              calculs






              share|improve this answer


























                up vote
                1
                down vote













                The unit of tikz is the cm, so it is useless to specify it. As for the calculations, they are done here without the need for brackets.



                documentclass[tikz,border=5mm]standalone
                usetikzlibraryshadows,fadings
                usetikzlibrarycalc
                usetikzlibrarypositioning

                begindocument
                begintikzpicture
                node[draw,blue,circle] at(0:2cm)4;
                node[draw,blue,circle] at(0:3)5;
                node[draw,blue,circle] at(0:4cm)6;
                foreach s in 6,...,8

                node[fill=red,circle,opacity=.5] at (0:s-2) $s$;

                endtikzpicture
                enddocument


                If you still want to specify the unit, it is necessary to do it after each of the Numbers: (0:s cm-2cm) $s$;



                Or to do as indicated by @PaulGaborit by factoring the unit: (0:(s-2)*1cm)



                calculs






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The unit of tikz is the cm, so it is useless to specify it. As for the calculations, they are done here without the need for brackets.



                  documentclass[tikz,border=5mm]standalone
                  usetikzlibraryshadows,fadings
                  usetikzlibrarycalc
                  usetikzlibrarypositioning

                  begindocument
                  begintikzpicture
                  node[draw,blue,circle] at(0:2cm)4;
                  node[draw,blue,circle] at(0:3)5;
                  node[draw,blue,circle] at(0:4cm)6;
                  foreach s in 6,...,8

                  node[fill=red,circle,opacity=.5] at (0:s-2) $s$;

                  endtikzpicture
                  enddocument


                  If you still want to specify the unit, it is necessary to do it after each of the Numbers: (0:s cm-2cm) $s$;



                  Or to do as indicated by @PaulGaborit by factoring the unit: (0:(s-2)*1cm)



                  calculs






                  share|improve this answer














                  The unit of tikz is the cm, so it is useless to specify it. As for the calculations, they are done here without the need for brackets.



                  documentclass[tikz,border=5mm]standalone
                  usetikzlibraryshadows,fadings
                  usetikzlibrarycalc
                  usetikzlibrarypositioning

                  begindocument
                  begintikzpicture
                  node[draw,blue,circle] at(0:2cm)4;
                  node[draw,blue,circle] at(0:3)5;
                  node[draw,blue,circle] at(0:4cm)6;
                  foreach s in 6,...,8

                  node[fill=red,circle,opacity=.5] at (0:s-2) $s$;

                  endtikzpicture
                  enddocument


                  If you still want to specify the unit, it is necessary to do it after each of the Numbers: (0:s cm-2cm) $s$;



                  Or to do as indicated by @PaulGaborit by factoring the unit: (0:(s-2)*1cm)



                  calculs







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 1 hour ago

























                  answered 3 hours ago









                  AndréC

                  4,3021833




                  4,3021833




















                      user50123 is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      user50123 is a new contributor. Be nice, and check out our Code of Conduct.












                      user50123 is a new contributor. Be nice, and check out our Code of Conduct.











                      user50123 is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f457021%2farithmetic-inside-tikz%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery