Edit: OP has clarified that the string of digits was only a string, not something involved in computations. So we can use string operations and avoid having to launch mpost with decimal numbersystem.
prologues := 3;
outputtemplate := "%j%c.eps";
string nstring;
nstring := "999999564319741369111111";
% input TEX % may be needed for matters unrelated to the core goal here
beginfig(1)
k:=length(nstring);
for i:=0 upto k-1:
j:= k - i - 1;% from least significant (i.e. last) to first characters
label(substring(j,j+1) of nstring,(20+22.7i,-7));
endfor
endfig
end
The above may need some adaptations unrelevant to core issue with how to insert that in a context using LaTeX (e.g. some btex...etex
or usage of TEX()
...), but these are secondary details.
Output is same as below.
Compilation produces on command line:
$ mpost onebyone.mp
This is MetaPost, version 2.02 (TeX Live 2023) (kpathsea version 6.3.5)
(/usr/local/texlive/2023/texmf-dist/metapost/base/mpost.mp
(/usr/local/texlive/2023/texmf-dist/metapost/base/plain.mp
Preloading the plain mem file, version 1.005) ) (./onebyone.mp [1{psfonts.map}<
cmr10.pfb>] )
1 output file written: onebyone1.eps
Transcript written on onebyone.log.
Initial answer. Based on a misunderstanding that n
for really to be treated as a numeric variable. Thus this requiers -numbersystem=decimal
.
prologues := 3;
outputtemplate := "%j%c.eps";
n:=999999564319741369111111;
% input TEX % not needed after all
beginfig(1)
i:=0;
forever:;% somehow the ; is mandatory here
m:=floor(n/10);
digit:=n-10m;
label(decimal (digit),(20+22.7i,-7));
i:=i+1;
n:=m;
exitif(n=0);
endfor
endfig
end
Compile with mpost -numbersystem=decimal test
. Produces on my system:
$ mpost -numbersystem=decimal test
This is MetaPost, version 2.02 (TeX Live 2023) (kpathsea version 6.3.5)
(/usr/local/texlive/2023/texmf-dist/metapost/base/mpost.mp
(/usr/local/texlive/2023/texmf-dist/metapost/base/plain.mp
Preloading the plain mem file, version 1.005) ) (./test.mp [1{psfonts.map}<cmr1
0.pfb>] )
1 output file written: test1.eps
Transcript written on test.log.
and this is screenshot:

The digits are output from least significant ot most significant which I believe matches your initial attempt. Use 20-22.7i
for ordering them differently.
An alternative approach would use inside TEX a call to a TeX macro with two arguments N and a "string", and extracts the Nth item from the string.
(in an edit I removed usage of TEX
which was unneeded)
-numbersystem=decimal
to allow mpost to digest big integers. But if your data is not really used for integer arithmetic, then one can do things with TeX macros. Or entirely within metapost using itslength
andsubstring
operators.