# Faster Metapost SVG Output with TeX?

I am generating some diagrams with METAPOST. I need the output in SVG format. My labels are generated by calls to LaTeX via Tex.mp and its TEX METAPOST definition (also called a macro).

My complaint: it is pretty slow to constantly write stuff to a file and fire off an instance of latex.

Is there a faster way to achieve the same result? (As I recall, I can use METAPOST inside a TeX document with the package luamplib, which is fast, but it generates PDF, not SVG.)

Minimal example:

prologues:=3;
input TEX;

outputformat := "svg";
outputtemplate:="%j.%{outputformat}";

string texpre;
% texpre = "%&latex" & char(10) &
texpre =
"\documentclass{article}" & char(10) &
"\begin{document}  " & char(10);

TEXPRE(texpre);
TEXPOST("\end{document}");

beginfig(1)
draw fullcircle scaled 1in;
string l;
l = "Foo";
dotlabel.bot(TEX("Label is " & l), origin);
endfig;
end

• Could you make a small example where you use TEX.mp? – egreg Sep 23 '13 at 15:15
• @egreg, better? – Colin Fraizer Sep 23 '13 at 15:30
• Well, the output I get is completely mangled. – egreg Sep 23 '13 at 15:40
• @egreg, I am using MetaPost 1.802 (TeX Live 2013/W32TeX) with command line: mpost --numbersystem=double --file-line-error --halt-on-error --tex=latex. – Colin Fraizer Sep 23 '13 at 15:44
• I too, but the output gets mangled, at least when opened with Firefox or Safari. Oh, and the same if I use PS output and convert to PDF – egreg Sep 23 '13 at 15:51

Are you looking for the latexmp package? To quote:

The MetaPost pack­age la­texMP im­ple­ments a user-friendly in­ter­face to ac­cess LaTeX-based type­set­ting ca­pa­bil­i­ties in MetaPost. The text to be type­set is given as string. This al­lows even dy­namic text el­e­ments, for ex­am­ple coun­ters, to be used in la­bels. Com­pared to other im­ple­men­ta­tions it is much more flex­i­ble, since it can be used as di­rect re­place­ment for btex..etex, and much faster, com­pared for ex­am­ple to the so­lu­tion pro­vided by tex.mp

• It seems not to work well with svg output, though. – cfr May 2 '14 at 1:02

The slowness here is nothing to do with the SVG output format. You get the same slowness with the TEX() macro with regular PostScript output. The problem is the implementation of the TEX() macro. It works by writing a string out to a file, and then closing the file, and reading the file back in. This lets you concatenate regular MP strings before you pass them to TeX; something you can not do with btex ... etex.
It is slow because it does this for every label.

But if you unwrap the macro a bit, it is possible to use the write-to-file-close-and-then-read-back technique directly using the MP primitives. Then you can write all the labels to one file, and process it in a single pass through TeX (or LaTeX). This is much faster if you have many labels.

Here is an example (in SVG format):

prologues := 3;
outputtemplate := "%j%c.svg";
outputformat := "svg";

beginfig(1);

n:=26;
R:=100;

path p;
p = for t=0 step 360/n until 360: right scaled R rotated t -- endfor cycle;
draw p;

for i=0 upto n-1:
fill fullcircle scaled dotlabeldiam shifted point i of p;
write "label(btex $" & char (65+i) & "_0$ etex, ("
& decimal (1.12 xpart (point i of p)) & ","
& decimal (1.12 ypart (point i of p)) & "));" to ".mplabels";
endfor
write EOF to ".mplabels";
input .mplabels;

endfig;
end.


This produces this image (converted to PNG for display here...)

MP has produced 26 separate dynamic labels, but it runs just as fast as if I was just doing one label.

• Note the slightly fussy syntax: write "string" to "file" needs the file as a proper string, but input just wants an identifier without the " marks.

• EOF is a built in value that writes an EOF marker to a file in order to close it.

Please note that METAPOST has added explicit support for outputting SVGs since v1.8 (see the manual for details).

• How does this answer the original question? – Thruston May 9 '19 at 22:24
• It points out that the original limitation of METAPOST not directly making SVGs is no longer present. – WillAdams May 10 '19 at 1:41
• I think the question was about why calling TEX() repeatedly is slow. The svg output is beside the point. And anyway I think the op was using a version of mp that already supported svg natively. – Thruston May 10 '19 at 6:39