6

Background

I'm trying to make a nice SVG out of Knuth's Dangerous Bend symbol based on his own code, for which the obvious tool to use is MetaPost. However, I'd prefer to still be able to process the file with Metafont so I can more easily see exactly what differences I might introduce in the shape. Since this seems like it would benefit from cutdraw, my current plan is for my dbend.mf to work with both Metafont's plain.mf base and MetaPost's mfplain.mp base.

I already have code that works fine in with both bases, but I want to do a couple of extra things if running under MetaPost, like colored fills and redefining pt as equal to bp.

Question

Is there a nice way to determine whether your code is running under Metafont or MetaPost, or do I just have to use e.g. known red?

  • I would use something more complex than red, for instance outputtemplate, but I can't think to another method. – egreg Apr 9 '14 at 15:33
8

I use

boolean metapost;
if known color Wolfgang Amadeus Mozart: 
  metapost:=true;
else: 
  metapost:=false;
fi

The explanation: under Metafont, color Wolfgang Amadeus Mozart will be an unknown numeric since color is not a Metafont keyword. But in MetaPost, Wolfgang Amadeus Mozart will be an unknown numeric and therefore not a color. Thus color Wolfgang Amadeus Mozart will be a false boolean, which is a known value. The reason for using Wolfgang Amadeus Mozart is to pick something very unlikely to be previously defined. I sometimes use one of the Bachs, or the title of some obscure Romanian folk dance, but my favorite is Maurits Cornelis Escher.

Sometimes I put let color = numeric under Metafont (so that some code can run in both MetaPost and Metafont), but I do that after setting the metapost boolean and only inside if not metapost:...fi.

  • Hmm, I wonder why there is not a package for this. – SamB Apr 9 '14 at 19:11
  • @SamB Probably because the people who try to write code useful in both MetaPost and Metafont are a rare breed. So far I only know three: myself, you and Thorsten Ohl (maintainer of the feynmf package). What I do is maintain a .dtx file (grafbase.dtx in the mfpic package) that generates two slightly different files. (There is also a feynmf.dtx). You really do need two files anyway, as mpost and mf have nonoverlapping search paths. I use my test only to warn the user if the wrong file is accidentally input. – Dan Apr 10 '14 at 18:00
3

I've come up with a different idea so simple I might describe it as stupid; I just make two files, a .mf and a .mp, and let kpathsea do the rest (names subject to change pending your suggestions):

% ismetapost.mf: Companion to ismetapost.mp

% If we were MetaPost, we would have read the other file, so ...
boolean is_metapost;
is_metapost := false;
% ismetapost.mp: Companion to ismetapost.mf

% Only MetaPost will see this file, so ...
boolean is_metapost;
is_metapost := true;
% Test/example driver

input ismetapost

if is_metapost:
  message "Running under MetaPost";
else:
  message "Running under Metafont";
fi

end

Hardest part of the whole thing: remembering to spell it as input, not include.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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