3

I have a graph that's out of scale, but I want to use it for a class I'm preparing. For me to use it, I'd need just to change the numbers in the axis. I'd like to make every x multiplied by 2. I tried this, but it doesn't work.

\foreach \x in {-6,-3,...,6}
            \draw (\x,-6.25) node{2*\x};

The image just appears with "2*x" in the axis. Does anyone know how to operate inside a node?

1 Answers1

6

There are a few options here:

  1. Make the original numbers correct and do the calculations in the coordinates. This works because coordinates are already put through an expression evaluator.

    \foreach \x in {-12,-6,...,12}
            \draw (\x/2,-6.25) node{\(\x\)};
    
  2. Do an extra calculation in the optional options to the \foreach.

    \foreach[evaluate=\x as \nodeLabel using \x*2] \x in {-6,-3,...,6}
            \draw (\x,-6.25) node{\(\nodeLabel\)};
    
  3. Put the node contents through a mathematical parser. As this isn't done automatically it has to be done explicitly.

    \foreach \x in {-6,-3,...,6}
            \draw (\x,-6.25) node{\pgfmathparse{\x*2}\(\pgfmathresult\)};
    

Which is best is rather up to you. They're all pretty much the same in this case. If you go on to use the recalculated numbers elsewhere as well then the middle one does the calculation just once per iteration which might save a few nanoseconds.

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • This is what I wanted, but for whatever reason when I put a number that's not integer it kind of "breaks". For example, I used:

    \foreach[evaluate=\x as \nodeLabel using \x*1.2] \x in {-5,-3,...,5} \draw (\x,-5.5) node{(\nodeLabel)};

    And it doesn't give me "6" to start with, it gives me 5.99999999

    – Cora Sanroman Aug 16 '21 at 19:43
  • 1
    You're hitting the limits of the precision of the calculations there. Take a look at https://tex.stackexchange.com/q/610368/86 to see some solutions to that. – Andrew Stacey Aug 16 '21 at 19:46
  • Thank you so very much! ♥ – Cora Sanroman Aug 16 '21 at 19:50
  • 1
    the third one is cool! ^^ – Black Mild Aug 16 '21 at 20:42
  • Just to update you: 1.2x doesn't work, but when I write 6/5x it does. Don't know why, but it did the trick. Thank you! – Cora Sanroman Aug 16 '21 at 21:28