[metapost] input command

mskala at ansuz.sooke.bc.ca mskala at ansuz.sooke.bc.ca
Sat Apr 8 14:15:04 CEST 2023


On Fri, 7 Apr 2023, Ernst Reissner wrote:

> I read the metapost manual by J.D. Hobby.
>
> What I wonder: the 'command' input is used several times but it is introduced
> nowhere.
>
> What is it? a macro?, a command??

I think some of the confusion among people trying to answer this question
comes about because this is a question about "input," not a question about
"command," despite the use of quotation marks above.

It's true, "input" seems to be used without explanation in the Metapost
manual.  It is explained in the METAFONT book, which says (in Chapter 20,
page 179 in the version I have here):

    input <filename>.  When 'input' is expanded, the expansion is null,
    but METAFONT prepares to read from the specified file before looking
    at any more tokens from its curernt source.  A <filename> is subject
    to special restrictions explained on the next page.

This is very similar to \input in TeX.  It appears to work the same way in
Metapost that it does in METAFONT.  The idea is that you'd use this
feature to include another file, such as a file of pre-prepared macros.
For example, TEX.mp (mentioned in chapter 13, starting from page 64, in
the Metapost manual) defines a macro named "TEX" that formats strings with
TeX, and the Metapost manual gives this example code for it:

     input TEX;
     beginfig(100)
       last := 10;
       for i := 0 upto last:
         label(TEX("$n_{" & decimal(i) & "}$"), (5mm*i,0));
       endfor
       ...
     endfig;

That first line, "input TEX;", causes Metapost to read the file TEX.mp,
which defines the TEX() macro.  Then a few lines later the example code
can invoke TEX(), which otherwise wouldn't exist.  This code behaves as if
the "input TEX;" line were replaced by the entire contents of the file
TEX.mp.

I'm surprised that this really doesn't seem to be explained at all in the
Metapost manual.  Example code uses "input" but there is no definition
given for it.  To answer the original question, the token "input" appears
to be what Metapost calls a command in Metapost's own technical sense,
like addto, clip, closefrom... and the others listed as commands in the
table on on page 90 in Appendix B of the Metapost manual.  These are what
other languages might call primitives.  But "input" is not mentioned in
that table, nor in the formal syntax.  Maybe it's implemented at a lower
level in the parser than the others and was omitted for that reason.

-- 
Matthew Skala
mskala at ansuz.sooke.bc.ca                 People before tribes.
https://ansuz.sooke.bc.ca/


More information about the metapost mailing list.