[Tex/LaTex] Metapost label based on variable value

metapost

I want to label a scale based on a variable value and I wrote a metapost code like this.

$ := 1;
for i = -.3cm step .6cm until 3.6cm:
    label.bot(str$,(i,-3.7cm));
    $ := incr($);
endfor;

It just labels "$" instead of its value. How to output the variables value?

Best Answer

To get the value of the variable, use the decimal macro. The following works:

$:=1;
for i = -.3cm step .6cm until 3.6cm:
    label.bot(decimal $,(i,-3.7cm));
    $ := incr($);
endfor

To quote the MetaPost manual:

The str operator is generally for emergency use only.

further

The decimal operator takes a number and returns the string representation.

Related Question