Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:
    $ := incr($);

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

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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

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

To quote the MetaPost manual:

The str operator is generally for emergency use only.


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

share|improve this answer

If you need a label, which is in TeX (between btex and etex), and you want to use value of a variable there, you can do something like this:

for t=0 upto 7:
  draw (a[t]*ux,a[t]*uy)--(a[t]*ux,a[t+1]*uy);
  draw (a[t]*ux,a[t+1]*uy)--(a[t+1]*ux,a[t+1]*uy);
  draw (a[t]*ux,-dx)--(a[t]*ux,dx);
  write "label.bot (btex $x_{"&decimal(t)&"}$ etex,(a["&decimal(t)&"]*ux,-dx));"
  to "mptextmp.mp";

write EOF to "mptextmp.mp";
scantokens "input mptextmp"  

See also this post on SO: METAPOST: using loop variables in labels

share|improve this answer
The code I posted above was part of the metapost source used to obtain this. –  Martin Apr 26 at 12:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.