[metapost] labels in for loops?

Stephan Hennig mailing_list at arcor.de
Fri Nov 16 15:13:15 CET 2007


Martin Kaffanke schrieb:

> Looking about the result I'd like to make 2 modifications: I'd like
> to have the german "," instead of the "." for the decimal place. And
> I'd like to format it so it has two digits in the decimal place, so
> 1,00 instead of just 1 and 0,90 instead of 0.9
> 
> How can I do that?

Two solutions are appended, a lengthy MetaPost-ish one and one that
builds on the LaTeXMP package to utilize the numprint LaTeX package.

Best regards,
Stephan Hennig


input format% for fig1
input latexmp% for fig2
setupLaTeXMP(packages="numprint[np]", preamble="\nprounddigits{2}");
prologues := 3;
u := 5mm;

beginfig(1);
string num;
numeric decSep;
  for i=0 upto 10:
    %%% Round and convert to string.
    num := decimal roundd(i/10, 2);
    %%% Search position of decimal separator.
    decSep := 0;
    for c = 1 upto length num:
      if substring(c-1,c) of num = ".":
        decSep := c;
      fi
      exitif decSep > 0;
    endfor
    %% Add decimal separator if needed.
    if decSep = 0:
      num := num & ".";
      decSep := length(num);
    fi
    %%% Append two zero digits.
    num := num & "00";
    %%% Finally, format string.
    num := substring (0,decSep-1) of num
            & "," & substring(decSep,decSep+2) of num;
    label(num, (.5u, i*u));
  endfor
endfig;

beginfig(2);
  for i=0 upto 10:
    label(textext("\np{" & decimal(i/10) & "}"), (.5u, i*u));
  endfor
endfig;
end


More information about the metapost mailing list