# Inline purified PostScript (MPS)

I just learned how to inline PDF code using the \pdfliteral command and that purified Encapsulated PostScript generated by METAPOST or purifyeps can be converted to PDF code by pdfLaTeX.

However, I have been unable to find the actual conversion code I need for embedding the transformed EPS via \pdfliteral.

I would like to have something like this:

\documentclass{minimal}
\begin{document}
\begin{purifiedeps}
%%BoundingBox: 0 0 33 31
%%HiResBoundingBox: 0 0 32.0781 30.9844
%%Creator: MetaPost 1.803
%%CreationDate: 2014.04.30:1514
%%Pages: 1
%%BeginProlog
%%EndProlog
%%Page: 1 1
0 0 0 setrgbcolor
newpath 28.9375 30.1875 moveto
16.0312 7.76563 lineto
3.14063 30.1875 lineto
2.82813 30.6406 2.23438 30.9844 1.64063 30.9844 curveto
0.84375 30.9844 0 30.3906 0 29.5312 curveto
0 1.5 lineto
0 0.64063 0.73438 0 1.64063 0 curveto
2.57813 0 3.28125 0.64063 3.28125 1.5 curveto
3.28125 23.3594 lineto
7.0625 16.8281 10.7969 10.3125 14.5469 3.78125 curveto
14.7969 3.28125 15.3906 2.98438 16.0312 2.98438 curveto
16.7344 2.98438 17.125 3.39063 17.5312 3.78125 curveto
21.2656 10.3125 25 16.8281 28.7812 23.3594 curveto
28.7812 1.5 lineto
28.7812 0.64063 29.4844 0 30.4375 0 curveto
31.3281 0 32.0781 0.64063 32.0781 1.5 curveto
32.0781 29.4844 lineto
32.0781 30.3281 31.2344 30.9844 30.4375 30.9844 curveto
29.8281 30.9844 29.2344 30.7344 28.9375 30.1875 curveto
closepath fill
showpage
%%EOF
\end{purifiedeps}
\end{document}


where the purifiedeps environment still has to be defined.

The EPS code yields the METAPOST-M:

NOTE: I know that it's possible to \includegraphics{somepurifiedeps.mps} but I am searching for an inline solution not requiring an external file.

EDIT: I think I gathered the relavant pieces from the ConTeXt package supp-pdf:

\def\convertMPtoPDF % #1#2#3%
{\bgroup
\defineMPtoPDFfallbacks
\ifx\pdfdecimaldigits\undefined\else \pdfdecimaldigits=5 \fi % new
\setbox\scratchbox\vbox\bgroup
\xdef\MPheight{0pt}%
\xdef\MPwidth {0pt}%
\forgetall
\offinterlineskip
\startMPresources
\doprocessMPtoPDFfile} %

\def\doprocessMPtoPDFfile#1#2#3% file xscale yscale
{\setMPspecials
\setMPextensions
\the\everyMPtoPDFconversion
\catcode\^^M=\@@endofline
\startMPscanning
\let\do\empty
\xdef\MPxscale{#2}%
\xdef\MPyscale{#3}%
\xdef\MPxoffset{0}%
\xdef\MPyoffset{0}%
\xdef\MPyshift{0pt}%
\donefalse
\let\handleMPsequence\dohandleMPsequence
\message{[MP to PDF]}% was: [MP to PDF #1] but there is a (#1) anyway
\input#1\relax}


\convertMPtoPDF{test.mps}{0.5}{0.5} will include the purified EPS file test.mps scaled by a factor of 0.5. To build an inline version of this command, I would need to replace \input#1 in \doprocessMPtoPDFfile with a macro containing the definition of the EPS code.

Unfortunatly, I don't know how to define such a macro since I need to preserve EPS comments (%) as well as line breaks. Hence, the macro definition needs to contain the verbatim (unprocessed) EPS code. How could I do that?

• Look in pdftex.def (the log of the latex run shows you where that is) that inputs some of Hans' code from context do to this \InputIfFileExists{supp-pdf.mkii}{}{% \InputIfFileExists{supp-pdf}{}{}% }% Apr 30 '14 at 13:38
• Is environment filecontents an option? Apr 30 '14 at 15:06

You need supp-pdf.mkii, see its comments for details.

The hard part is to get the MPS file into a macro (or token register). Macro \savemps saves the MPS code as macro. Care need to be taken for the percent characters and the line ends should be preserved.

Macro \includemps is then quite easy, it calls \convertMPtoPDF with a redefined \input that rereads the image stored in a macro as file via e-TeX's \scantokens:

\documentclass{article}

\input{supp-pdf.mkii}
\makeatletter
\newcommand*{\savemps}[1]{%
\begingroup
\@sanitize % verbatim catcodes
\catcode\ =10 % except space
\catcode\^^A=14 % new comment char
% keep lines:
\@makeother\^^J% Line feed as character
\endlinechar=\^^J % end lines with line feed
\savemps@{#1}%
}
\newcommand*{\savemps@}[2]{%
\endgroup
\def#1{#2}%
% catch odd new line
\futurelet\@let@token\savemps@gobbleLF
}
\newcommand*{\savemps@gobbleLF}{%
\ifx\@let@token\includemps@J
\expandafter\@gobble
\fi
}
\begingroup
\lccode\9=\^^J %
\lowercase{\endgroup
\let\includemps@J=9%
}
\newcommand*{\includemps}[3]{%
\begingroup
\def\input##1\relax{\scantokens\expandafter{##1}}%
\convertMPtoPDF{#1}{#2}{#3}%
\endgroup
}
\makeatother

\savemps\mympsimage{
%%BoundingBox: 0 0 33 31
%%HiResBoundingBox: 0 0 32.0781 30.9844
%%Creator: MetaPost 1.803
%%CreationDate: 2014.04.30:1514
%%Pages: 1
%%BeginProlog
%%EndProlog
%%Page: 1 1
0 0 0 setrgbcolor
newpath 28.9375 30.1875 moveto
16.0312 7.76563 lineto
3.14063 30.1875 lineto
2.82813 30.6406 2.23438 30.9844 1.64063 30.9844 curveto
0.84375 30.9844 0 30.3906 0 29.5312 curveto
0 1.5 lineto
0 0.64063 0.73438 0 1.64063 0 curveto
2.57813 0 3.28125 0.64063 3.28125 1.5 curveto
3.28125 23.3594 lineto
7.0625 16.8281 10.7969 10.3125 14.5469 3.78125 curveto
14.7969 3.28125 15.3906 2.98438 16.0312 2.98438 curveto
16.7344 2.98438 17.125 3.39063 17.5312 3.78125 curveto
21.2656 10.3125 25 16.8281 28.7812 23.3594 curveto
28.7812 1.5 lineto
28.7812 0.64063 29.4844 0 30.4375 0 curveto
31.3281 0 32.0781 0.64063 32.0781 1.5 curveto
32.0781 29.4844 lineto
32.0781 30.3281 31.2344 30.9844 30.4375 30.9844 curveto
29.8281 30.9844 29.2344 30.7344 28.9375 30.1875 curveto
closepath fill
showpage
%%EOF
}

\begin{document}
\setlength{\fboxsep}{1pt}
\setlength{\fboxrule}{.1pt}
\fbox{%
\includemps{\mympsimage}{1}{1}%
}
\end{document}


• Fantastic! I already started to fiddle around with the catcodes but I could not get it working! Apr 30 '14 at 16:18
• ... and redefining \input locally is even more elegant then copying code from supp-pdf.mkii. Apr 30 '14 at 16:20