3

Given a possibly big integer (up to $10^12$ or more) is there a way to print the digits of that number in fixed position using metapost? For example given the number 123456789 or 2365411111 is it possible to print the $i$-th digits at coordinates $(i,0)$?

The idea should be something in the spirit of the code below (that however doesn't work). There are two order of problems

  1. The inner limits of metapost in dealing with big numbers.
  2. How to deal with modulus function.

Here is what I have tried so far:

```
verbatimtex
\documentclass{article}
\begin{document}
etex
prologues := 3;
outputtemplate := "%j%c.eps";
n:=123;
input TEX
for i = 0 upto 3: 
label(TEX(decimal (Mod(floor((n)/(10**(i))),10**i))),(20+22.7i,-7));
endfor
endfig
end
and do something similar for n=564319741369 for example
3
  • Do you really need a big integer and not only a big string? My answer uses -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 its length and substring operators.
    – user691586
    Commented Mar 28, 2023 at 10:52
  • I need only a string. The problem now is that the with the command you propose I get the following error: 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) ) (/usr/local/texlive/2023/texmf-dist/fonts/source/public/aramaic-serto/test.mf >> font_size10pt# ! Isolated expression. <to be read again> ; l.8 font_size 10pt#; ?
    – Mathland
    Commented Mar 28, 2023 at 20:09
  • I don't know and can not guess the code leading to the error you report in your comment. Anyway, as you need only a string then a completely different approach is in order, which I will post later.
    – user691586
    Commented Mar 28, 2023 at 20:15

1 Answer 1

3

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:

screenshot from gv

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)

1
  • This is exactly what I'm looking for. Thank you a lot.
    – Mathland
    Commented Mar 28, 2023 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .