# How to insert (if possible) an image inside a Metapost figure? (in plain LaTeX environment)

I'd like to insert an image into a Metapost figure (I'm using MikTeX/LaTeX, no Metafun). I hopefully tried (also thanks to Aditya's suggestion) the following Metapost code (compiled with -tex=pdftex option):

verbatimtex
%&latex
\documentclass{minimal}
\usepackage{graphicx}
\begin{document}
etex

beginfig(1);
picture pct;
pct:=btex \includegraphics{myimage.jpg} etex;
%pct:=btex \includegraphics{myimage.eps} etex;
draw pct;
endfig;
end

but it does not work, giving error ! Unable to make mpx file. if I use jpg format, or no error but neither image if I use eps format. Any clue?

UPDATE: trying the Metafun way without ConTeXt

I was told (see egreg comment below) that I can use the externalfigure command of Metafun (I use LaTeX, so I do not deal with Metafun, which, if I understand, is ConTeXt specific), using the mpost -mem=metafun compiling option of Metapost, but my system (Windows 7, MikTeX/LaTeX) seems to be unaware of Metafun and tells me Sorry, I can't find the 'metafun' preload file; will try 'plain'., the actual error being ! Isolated expression. referred to externalfigure. How can I install Metafun inside MikTeX?

Here is my Metapost code compiled with -mem=metafun and -tex=pdftex options:

verbatimtex
\documentclass{minimal}
\begin{document}
etex

beginfig(1);
externalfigure "myimage.jpg";
endfig;
end
-
–  Claudio Fiandrino Apr 13 '13 at 10:54
With metafun you can use external images (see Metapost external figure), but, according to the Metafun manual (linked in the answer), it's not possible to store an image in a picture variable. –  egreg Apr 13 '13 at 10:57
I guess I can't use Metafun with MikTeX/LaTeX, isn't it? –  mmj Apr 13 '13 at 11:02
Why not? mpost -mem=metafun –  egreg Apr 13 '13 at 11:06
To prevent any confusion: ConTeXt MkII and mptopdf use the externalfigure syntax without draw. ConTeXt MkIV requires the draw externalfigure syntax. –  Marco Apr 13 '13 at 11:06

Note I deleted my previous answer, because this is a working solution:

Use the following MP file

beginfig(1);
externalfigure "hacker.jpg" scaled 3cm;
endfig;
end;

and compile it using

mptopdf --metafun filename

This will generate the following filename.1 file:

%%BoundingBox: 0 0 86 86
%%HiResBoundingBox: 0 0 85.03935 85.03935
%%Creator: MetaPost 1.504
%%CreationDate: 2013.04.14:1210
%%Pages: 1
%%BeginProlog
%%BeginResource: procset mpost
/bd{bind def}bind def
/hlw{0 dtransform exch truncate exch idtransform pop setlinewidth}bd
/vlw{0 exch dtransform truncate idtransform setlinewidth pop}bd
/l{lineto}bd/r{rlineto}bd/c{curveto}bd/m{moveto}bd/p{closepath}bd/n{newpath}bd
/C{setcmykcolor}bd/G{setgray}bd/R{setrgbcolor}bd/lj{setlinejoin}bd/ml{setmiterlimit}bd
/lc{setlinecap}bd/S{stroke}bd/F{fill}bd/q{gsave}bd/Q{grestore}bd/s{scale}bd/t{concat}bd
/sd{setdash}bd/rd{[] 0 setdash}bd/P{showpage}bd/B{q F Q}bd/W{clip}bd
%%EndResource
%%EndProlog
%%Page: 1 1
%%MetaPostSpecials: 2.0 123 1000
%%MetaPostSpecial: 9 85.03935 0 0 85.03935 0 0 hacker.jpg 1 10
0.123 0.012 0.001 R
n 0 0 m
0 0 l
0 0 l
0 0 l
p F
P
%%EOF

and a filename-1.pdf file that includes the image.

-
Thanks, I confirm that your code doesn't show any error but neither the image insertion. Anyway, if I can't get to this with LaTeX/Metapost, I'll go with tikz. –  mmj Apr 14 '13 at 7:59
Tentative upvote… –  Marco Apr 14 '13 at 8:49
What about the Metafun way? Can you help me to understand how to install Metafun on LaTeX? –  mmj Apr 14 '13 at 10:26
To use metafun, simply add input mp-tool.mpii. But this will notmwork with latex as it needs supports from the macro package, and LaTeX dors not provide that. –  Aditya Apr 14 '13 at 13:55
So I have to install ConTeXt (or what else) to run your Metapost code with mptopdf and --metafun option? –  mmj Apr 15 '13 at 13:22