In a LaTeX document, I use a number of packages, many different commands created for the convenience of typing, non-standard fonts. I'd like to use it all inside MetaPost. The problem is that the text of the labels (including in btex
–etex
) is rendered during the work of the mpos
command, and not pdflatex
. Is it possible to somehow postpone the rendering of text, formulas to the compilation stage of a LaTeX document? I know of a method using verbatimtex
–etex
, but it requires duplicating the text from LaTeX to PostScript (this is not exactly deferred rendering, but it allow to have the necessary commands, fonts, etc. at the time of label rendering).
UPD: in a comment, Marijn wrote: Could you post a small example document and the necessary compile chain to illustrate your issue? What I would like to achieve looks something like this:
pic.mp
:
outputtemplate := "%j-%3c.mps";
beginfig(1);
label(btex $x$ etex, (0, 0));
endfig;
end;
main.tex
:
\documentclass{article}
% All kinds of usepackage, def, etc.
\usepackage{stix}
\begin{document}
\includegraphics{pic-001.mps}
\end{document}
And so that after calling the mpost pic
and pdflatex main.tex
commands, I get a file in which $x$
appears, typed in stix font. In addition, I want a solution in which the preamble text does not have to be duplicated in the MetaPost file, since it is almost impossible to maintain two identical pieces of text.
The ability to render text during the compilation of a LaTeX document, and not at the time of creating the image, I saw in this article.
Best Answer
Integrating Metapost figures into your LaTeX environment is simple, if you can switch to
lualatex
instead ofpdflatex
. For example:Compile this with
lualatex
to get this:Documentation is here, and in Section 12 of my own Drawing with Metapost document.
...but lualatex is really slow!
Actually it really isn't any more. Even on my old 2011 Mac Mini, it only takes a few seconds to compile my benchmark document with 120 pages of complex graphics in-line. See
pww-2.tex
in this project.... but my document is huge!
OK, my Drawing-with-Metapost document referenced above is also huge, and in that case I have a sort-of hybrid workflow.
I use
lualatex
to compile the main document -- and it happily includes all of my old.mps
graphics with no changes.And now I am gradually migrating my old
.mp
source to standaloneluamplib
graphics.This is the template I use:
Except that for DwM I vary that a bit, and I have the common code for the main document in a separate local style file called
dwmcommon.sty
. Just before\begin{document}
in the main document and in these standalone graphics I have\usepackage{dwmcommon}
to load all the common definitions, so that I only have to maintain them in one place.And of course I have to compile these updated MP files with
lualatex
instead ofmpost
. This produces.pdf
output files directly instead of.mps
or.eps
, which saves one more process in the step.The advantages of this hybrid approach are that:
I did see a Vi syntax file for Context that attempted to define MP ranges and TeX ranges, but it was not a great success. So what I do (in Vim) is just switch between them manually, using
set ft=mp
orset ft=tex
as needed.