How to define a variable as another variable divided by two?









up vote
5
down vote

favorite












I would like to define a variable j in a loop foreach i in 2, 4, 6 and use j to labels of the nodes.



Here is my code:



documentclassstandalone
usepackagebalance,complexity,pgfplots,tikz,tikz-3dplot
usetikzlibrarycalc

begindocument


begintikzpicture[scale=0.4]

def half 0.5;

foreach i in 2, 4, 6

def j i*half;
node[draw=none] at (i,0) $X_j$;
node[draw=none] at (-i,0) $X'_j$;



endtikzpicture
enddocument


I would like to get this output:



enter image description here



Instead, I get this:



enter image description here



How can I use mathematical operators to modify the label of a node?










share|improve this question





















  • Isn't it easier doing node[draw=none] at (2*i,0) $X_i$; with foreach i in 1,2,3?
    – egreg
    Nov 11 at 14:52










  • @egreg That is true, but I need the division or multiplication by 0.5 for a different reason.
    – padawan
    Nov 11 at 14:53










  • @egreg I just realized that I could also use the method that you suggested :)
    – padawan
    Nov 11 at 22:20














up vote
5
down vote

favorite












I would like to define a variable j in a loop foreach i in 2, 4, 6 and use j to labels of the nodes.



Here is my code:



documentclassstandalone
usepackagebalance,complexity,pgfplots,tikz,tikz-3dplot
usetikzlibrarycalc

begindocument


begintikzpicture[scale=0.4]

def half 0.5;

foreach i in 2, 4, 6

def j i*half;
node[draw=none] at (i,0) $X_j$;
node[draw=none] at (-i,0) $X'_j$;



endtikzpicture
enddocument


I would like to get this output:



enter image description here



Instead, I get this:



enter image description here



How can I use mathematical operators to modify the label of a node?










share|improve this question





















  • Isn't it easier doing node[draw=none] at (2*i,0) $X_i$; with foreach i in 1,2,3?
    – egreg
    Nov 11 at 14:52










  • @egreg That is true, but I need the division or multiplication by 0.5 for a different reason.
    – padawan
    Nov 11 at 14:53










  • @egreg I just realized that I could also use the method that you suggested :)
    – padawan
    Nov 11 at 22:20












up vote
5
down vote

favorite









up vote
5
down vote

favorite











I would like to define a variable j in a loop foreach i in 2, 4, 6 and use j to labels of the nodes.



Here is my code:



documentclassstandalone
usepackagebalance,complexity,pgfplots,tikz,tikz-3dplot
usetikzlibrarycalc

begindocument


begintikzpicture[scale=0.4]

def half 0.5;

foreach i in 2, 4, 6

def j i*half;
node[draw=none] at (i,0) $X_j$;
node[draw=none] at (-i,0) $X'_j$;



endtikzpicture
enddocument


I would like to get this output:



enter image description here



Instead, I get this:



enter image description here



How can I use mathematical operators to modify the label of a node?










share|improve this question













I would like to define a variable j in a loop foreach i in 2, 4, 6 and use j to labels of the nodes.



Here is my code:



documentclassstandalone
usepackagebalance,complexity,pgfplots,tikz,tikz-3dplot
usetikzlibrarycalc

begindocument


begintikzpicture[scale=0.4]

def half 0.5;

foreach i in 2, 4, 6

def j i*half;
node[draw=none] at (i,0) $X_j$;
node[draw=none] at (-i,0) $X'_j$;



endtikzpicture
enddocument


I would like to get this output:



enter image description here



Instead, I get this:



enter image description here



How can I use mathematical operators to modify the label of a node?







tikz-pgf calc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 14:46









padawan

559518




559518











  • Isn't it easier doing node[draw=none] at (2*i,0) $X_i$; with foreach i in 1,2,3?
    – egreg
    Nov 11 at 14:52










  • @egreg That is true, but I need the division or multiplication by 0.5 for a different reason.
    – padawan
    Nov 11 at 14:53










  • @egreg I just realized that I could also use the method that you suggested :)
    – padawan
    Nov 11 at 22:20
















  • Isn't it easier doing node[draw=none] at (2*i,0) $X_i$; with foreach i in 1,2,3?
    – egreg
    Nov 11 at 14:52










  • @egreg That is true, but I need the division or multiplication by 0.5 for a different reason.
    – padawan
    Nov 11 at 14:53










  • @egreg I just realized that I could also use the method that you suggested :)
    – padawan
    Nov 11 at 22:20















Isn't it easier doing node[draw=none] at (2*i,0) $X_i$; with foreach i in 1,2,3?
– egreg
Nov 11 at 14:52




Isn't it easier doing node[draw=none] at (2*i,0) $X_i$; with foreach i in 1,2,3?
– egreg
Nov 11 at 14:52












@egreg That is true, but I need the division or multiplication by 0.5 for a different reason.
– padawan
Nov 11 at 14:53




@egreg That is true, but I need the division or multiplication by 0.5 for a different reason.
– padawan
Nov 11 at 14:53












@egreg I just realized that I could also use the method that you suggested :)
– padawan
Nov 11 at 22:20




@egreg I just realized that I could also use the method that you suggested :)
– padawan
Nov 11 at 22:20










2 Answers
2






active

oldest

votes

















up vote
7
down vote



accepted










Use the pgfmath macros (pgfmathsetmacro for float calculations and pgfmathtruncatemacro if you only want the integer part).



documentclassstandalone
usepackagetikz

begindocument


begintikzpicture[scale=0.4]
pgfmathsetmacrohalf 0.5;
foreach i in 2, 4, 6

pgfmathtruncatemacroji*half;
node[draw=none] at (i,0) $X_j$;
node[draw=none] at (-i,0) $X'_j$;

endtikzpicture
enddocument





share|improve this answer





























    up vote
    7
    down vote













    You can do the calculations within the foreach loop with



    foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6 


    see page 904 of manual 3.0.1a.



    half



    documentclassstandalone
    usepackagetikz

    begindocument
    begintikzpicture[scale=0.4]
    foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6

    node[draw=none] at (i,0) $X_moitie$;
    node[draw=none] at (-i,0) $X'_moitie$;

    endtikzpicture
    enddocument





    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: 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%2f459473%2fhow-to-define-a-variable-as-another-variable-divided-by-two%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








      up vote
      7
      down vote



      accepted










      Use the pgfmath macros (pgfmathsetmacro for float calculations and pgfmathtruncatemacro if you only want the integer part).



      documentclassstandalone
      usepackagetikz

      begindocument


      begintikzpicture[scale=0.4]
      pgfmathsetmacrohalf 0.5;
      foreach i in 2, 4, 6

      pgfmathtruncatemacroji*half;
      node[draw=none] at (i,0) $X_j$;
      node[draw=none] at (-i,0) $X'_j$;

      endtikzpicture
      enddocument





      share|improve this answer


























        up vote
        7
        down vote



        accepted










        Use the pgfmath macros (pgfmathsetmacro for float calculations and pgfmathtruncatemacro if you only want the integer part).



        documentclassstandalone
        usepackagetikz

        begindocument


        begintikzpicture[scale=0.4]
        pgfmathsetmacrohalf 0.5;
        foreach i in 2, 4, 6

        pgfmathtruncatemacroji*half;
        node[draw=none] at (i,0) $X_j$;
        node[draw=none] at (-i,0) $X'_j$;

        endtikzpicture
        enddocument





        share|improve this answer
























          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted






          Use the pgfmath macros (pgfmathsetmacro for float calculations and pgfmathtruncatemacro if you only want the integer part).



          documentclassstandalone
          usepackagetikz

          begindocument


          begintikzpicture[scale=0.4]
          pgfmathsetmacrohalf 0.5;
          foreach i in 2, 4, 6

          pgfmathtruncatemacroji*half;
          node[draw=none] at (i,0) $X_j$;
          node[draw=none] at (-i,0) $X'_j$;

          endtikzpicture
          enddocument





          share|improve this answer














          Use the pgfmath macros (pgfmathsetmacro for float calculations and pgfmathtruncatemacro if you only want the integer part).



          documentclassstandalone
          usepackagetikz

          begindocument


          begintikzpicture[scale=0.4]
          pgfmathsetmacrohalf 0.5;
          foreach i in 2, 4, 6

          pgfmathtruncatemacroji*half;
          node[draw=none] at (i,0) $X_j$;
          node[draw=none] at (-i,0) $X'_j$;

          endtikzpicture
          enddocument






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 15:26

























          answered Nov 11 at 14:57









          TeXnician

          23.8k62984




          23.8k62984




















              up vote
              7
              down vote













              You can do the calculations within the foreach loop with



              foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6 


              see page 904 of manual 3.0.1a.



              half



              documentclassstandalone
              usepackagetikz

              begindocument
              begintikzpicture[scale=0.4]
              foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6

              node[draw=none] at (i,0) $X_moitie$;
              node[draw=none] at (-i,0) $X'_moitie$;

              endtikzpicture
              enddocument





              share|improve this answer


























                up vote
                7
                down vote













                You can do the calculations within the foreach loop with



                foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6 


                see page 904 of manual 3.0.1a.



                half



                documentclassstandalone
                usepackagetikz

                begindocument
                begintikzpicture[scale=0.4]
                foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6

                node[draw=none] at (i,0) $X_moitie$;
                node[draw=none] at (-i,0) $X'_moitie$;

                endtikzpicture
                enddocument





                share|improve this answer
























                  up vote
                  7
                  down vote










                  up vote
                  7
                  down vote









                  You can do the calculations within the foreach loop with



                  foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6 


                  see page 904 of manual 3.0.1a.



                  half



                  documentclassstandalone
                  usepackagetikz

                  begindocument
                  begintikzpicture[scale=0.4]
                  foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6

                  node[draw=none] at (i,0) $X_moitie$;
                  node[draw=none] at (-i,0) $X'_moitie$;

                  endtikzpicture
                  enddocument





                  share|improve this answer














                  You can do the calculations within the foreach loop with



                  foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6 


                  see page 904 of manual 3.0.1a.



                  half



                  documentclassstandalone
                  usepackagetikz

                  begindocument
                  begintikzpicture[scale=0.4]
                  foreach i [evaluate=i as moitie using int(i/2)] in 2, 4, 6

                  node[draw=none] at (i,0) $X_moitie$;
                  node[draw=none] at (-i,0) $X'_moitie$;

                  endtikzpicture
                  enddocument






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 15:13

























                  answered Nov 11 at 15:02









                  AndréC

                  6,84211140




                  6,84211140



























                      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%2f459473%2fhow-to-define-a-variable-as-another-variable-divided-by-two%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







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3