I've used metapost for generating figures for my latex documents for some time, but I've always managed them as separate documents, and included the resulting graphics into the latex document using \includgraphics{figure.mps}
. However, I decided to try the emp
package to incorporate the metapost definitions directly in my main document.
Here's a working example (working.mp) of the metapost code I'm trying to generate:
verbatimtex
\documentclass [12pt]{article}
\newcommand \stack [1]{\vbox {\halign {\hfil ##\hfil \cr #1\crcr }}}
\begin {document}
etex;
input expressg;
beginfig(1);
LaTeX_unitlength := 1.0pt;
w := 40*LaTeX_unitlength;
h := 40*LaTeX_unitlength;
z0 = origin;
drawroundedbox(0, 2.5cm, 3cm, 5mm)(btex \stack{Domain\cr Name\cr Service} etex);
endfig;
verbatimtex
\end{document}
etex;
end.
endinput;
I can invoke TEX=latex mpost working.mp
and it successfully process this file.
Here's an MWE of the latex file (test.tex) I'm trying to use to generate the same metapost figure:
\documentclass[letterpaper,12pt, openbib]{article}
\usepackage[utf8]{inputenc}
\usepackage{emp}
\empaddtoTeX{\newcommand\stack[1]{\vbox{\halign{\hfil##\hfil\cr#1\crcr}}}}
\empprelude{input expressg}
\DeclareGraphicsRule{*}{mps}{*}{}
\title{Emp Package Test}
\author{Author}
\begin{document}
\maketitle
\begin{empfile}[\jobname-figs]
\section{Emp Package Test}
This is a test trying to get a macro properly added to the verbatimtex section
of the generated metapost file.
\verb+\newcommand\stack[1]{\vbox{\halign{\hfil##\hfil\cr#1\crcr}}}+
The macro above is what we are trying to add. However, things aren't working
out the way I want them to. Instead, what's showing up in the MetaPost file is:
\begin{verbatim}
verbatimtex
\documentclass [12pt]{article}
\newcommand \stack [1]{\vbox {\halign {\hfil ####\hfil \cr ##1\crcr }}}
\begin {document}
etex;
\end{verbatim}
\begin{figure}
\begin{center}
\begin{emp}(40,40)
z0 = origin;
drawroundedbox(0, 2.5cm, 3cm, 5mm)(btex \stack{Domain\cr Name\cr Service} etex);
\end{emp}
\end{center}
\caption{\label{fig:emp}Example using embeded MetaPost}
\end{figure}
\end{empfile}
\end{document}
Running pdflatex on example.tex produces example-figs.mp. Running the same mpost command on example-figs.mp fails with this output:
--(0)> TEX=latex mpost test-figs.mp
This is MetaPost, version 1.803 (kpathsea version 6.2.0dev)
(mpost.mp (/usr/share/texlive/texmf-dist/metapost/base/plain.mp
Preloading the plain mem file, version 1.004) ) (./test-figs.mp
(/usr/share/texlive/texmf-dist/metapost/expressg/expressg.mp
>> "expressg.mp version 1.61, 2004/03/17")fatal: Command failed: latex --parse-first-line --interaction=nonstopmode mpm0nOLA.tex; see mpxerr.log
>> test-figs.mp
>> test-figs.mpx
! ! Unable to make mpx file.
l.13 drawroundedbox(0, 2.5cm, 3cm, 5mm)(btex
\stack{Domain\cr Name\cr
Transcript written on test-figs.log.
Examining the metapost file the emp package created (test-figs.mp) reveals that it is identical to the working.mp file I showed at the beginning except for the definition of the \stack macro added in the verbatimtex … etex block at the start:
verbatimtex
\documentclass [12pt]{article}
\newcommand \stack [1]{\vbox {\halign {\hfil ####\hfil \cr ##1\crcr }}}
\begin {document}
etex;
This was added by the \empaddtoTeX
macro that is in the preamble of test.tex.
I have two related questions:
- How can I get
\empaddtoTeX
from doubling all the '#' characters in the argument passed to it. - I really want a
%&latex
added to theverbatimtex
block so I can invoke metapost as justmpost test-figs.mp
instead of the way I described, but\empaddtoTex{%&latex}
doesn't work because everything after the '%' is treated as a somment, and pdflatex complains about the missing '}'. Can this be done?
Best Answer
Possibly the same trick can work also with
emp
, but, for obvious reasons, I prefergmp
.;-)
It needs running the file with
-shell-escape
(it's possible that a new version will avoid it, as nowmpost
is in the list of “safe” programs for the restricted shell escape).However, the simplest workaround is to create a small
.tex
file, saympmacros.tex
within it and simply doing
On the other hand, you can simply avoid this with