# How to preview images generated by MetaPost?

I use the old MetaPost program to create graphics.

My problem is that, to see the result of my MetaPost figures, I have to compile first the .mp file and then the .tex file where I've inserted them. I find it quite boring.

I've found the online MetaPost Previewer but I have to copy and paste my code there and this is even a further waste of time.

I've also tried to add the lines

prologues:=3;
filenametemplate "%j-%3c.eps";


in my .mp files to generate .eps files, but, when I open them with GSView, sometimes I'm able to see my proper figures and sometimes not.

So, the question is: does anyone knows a program which is able to open the output generated by MetaPost? Or any package that can let me generate viewable images? Or any other trick that make me save some time (even an editor feature)?

If this can help, I use MiKTeX 2.9 in Windows XP. My main editor is TeXMaker (v4), but I have WinEdt (v8) installed as well.

-
Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format. – Heiko Oberdiek Jun 8 '13 at 7:56
Did you setup GSview to clip pictures to the bounding box as described in section 14.2.1 in the MetaPost manual? – Stephan Hennig Jun 8 '13 at 10:21

This solution won't let you open your MetaPost images with a double-click of the mouse, but probably will let you save a lot of time.

Since you have WinEdt 8 installed, download the add-on MetaPreview (choose the right version) and install it in WinEdt.

Note that the add-on provides a previewer for MetaPost output as well as one for MetaFont output. You can choose only the first one if you are not interested in MetaFont preview.

Once you've installed it, you will find a drop-down button in the toolbar which contains two buttons, as shown in the following image

To have these buttons enabled, you have to compile your .mp file first.

Once you've compiled your .mp file, with the first command ('MetaPost Preview All') you will be able to preview all the generated images, while with the other ('MetaPost Preview One') you will be prompted to choose the image you want to preview (useful when you're working on an image and want to preview only that one).

Just one remark: the add-on works regardless of the prologue but needs the generated output to be in the format .1, .2, etc., so delete the line

filenametemplate "%j-%3c.eps";


from your .mp files.

## UPDATE

A new version of MetaPreview is available.

The limitation described above has been overcome. Now it can be used even when your .mp file contains

filenametemplate "%j-%c.mps";


or

outputtemplate := "%j-%c.mps";

-
Thanks for your answer. I'll try the add-on you've mentioned. – Sterry Jun 8 '13 at 8:23
I have to thank you very much. I've tried MPPreview as you suggested and it is just the tool I was looking for! Just have to click a button to preview my images in my favourite PDF viewer is awesome. Thanks again. – Sterry Jun 8 '13 at 14:41
The filenametemplate primitive is deprecated since MetaPost version 1.200. It has been replaced by a new internal variable outputtemplate. Please update your MetaPost distribution. – Stephan Hennig Jun 8 '13 at 16:52

If you're not opposed to compiling with lualatex, then you could make use of the luamplib package to bypass the preview step altogether and compile the images in-document:

\documentclass{article}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}

\end{mplibcode}
\end{document}

-
Does luamplib support TeX labels? – Aditya Jun 8 '13 at 18:16
@aditya It does now it seems. The patch pointed out here seems to have been implemented if the documentation is to be believed. The github listed as the homepage may no longer be where the actual package code resides. – Scott H. Jun 8 '13 at 18:20
Scott, @Aditya: The patch is in the CTAN version of luamplib, but I think it can't yet be in Texlive or Miktex. – Charles Stewart Jun 10 '13 at 20:00
@charlesstewart. Ah, thanks for pointing that out. Do you have an idea of how long it might take? – Scott H. Jun 11 '13 at 6:09
@ScottH. I tried the new luamplib in CTAN with TeX Live 2013/pretest and it seems to work with labels. We have only to wait for the release and the package will be updated soon. – egreg Jun 11 '13 at 10:30

If you have ConTeXt standalone installed, then you can just run:

context filename.mp


which will generate filename.pdf.

-

The MetaPost manual describes various ways for previewing MetaPost graphics. To quote from a section titled Previewing PostScript Output:

Previewing MetaPost’s PostScript output is not difficult, but there are some catches that one should know about. This section deals with the following questions: How can graphics be clipped to their true bounding box in the PostScript viewer application? Why are my text labels rendered with an ugly font (or not at all) and how to avoid that? How can several graphics be combined into a multi-page document that can be previewed within one instance of the viewer application?

It then discusses these tools

• GSview
• mproof.tex
• mpsproof.tex

and mentions

Side note: I happen to have written the work-flow and preview related stuff in the MetaPost manual. If you think there's anything missing or unclear and could be improved, please drop me a line.

-
Finally I'm able to see my MetaPost graphics in GSView! I did it by activating 'EPS Clip' in Options. Thanks! However I prefer to see them in my PDF viewer thanks to karlkoeller's suggestion. – Sterry Jun 8 '13 at 14:35
That is really easy with the mpsproof.tex script, which is part of the MetaPost distribution. Use it like pdftex mpsproof *.mps after setting the file extension of MetaPost output to mps by putting the line outputtemplate := "%j-%c.mps"; in the preamble of the source file. – Stephan Hennig Jun 8 '13 at 16:49
@StephanHennig So you are involved in MetaPost development. I'm using MiKTeX 2.9 and the MetaPost version that is shipped with it is still 1.504... This version does not even contain mpsproof.tex and most of the things you've written in this post are not even contemplated in the 1.504 manual. Any chance you convince C.Schenk to update MetaPost in MiKTeX distribution? – karlkoeller Jun 9 '13 at 7:16
The MetaPost binary in MiKTeX 2.9 Portable seems to be of version 1.504, which is OK, but the manal is of version 1.005. Please complain to the distributor. The mpsproof.tex script has been added to the MetaPost distribution in version 1.100. You can find it at CTAN. – Stephan Hennig Jun 9 '13 at 12:11
Or try another TeX distribution. TeX Live works well on Windows. – Stephan Hennig Jun 9 '13 at 12:21

You can use gmp:

\documentclass{article}
\usepackage[shellescape]{gmp}
\begin{document}
\begin{mpost}
%beginfig(1};
a=3cm; b=2cm;
z0=(0,0);
z1=-z3=(a,0);
z2=-z4=(0,b);
draw z1..z2..z3..z4..cycle;
draw z1--z0--z2;
label.top(btex $a$ etex, .5[z0,z1]);
label.lft(btex $b$ etex, .5[z0,z2]);
dotlabel.bot(btex $(0,0)$ etex, z0);
%endfig;
\end{mpost}
\end{document}


Just comment out the beginfig and endfig lines and compile with the -shell-escape option active.

-

I always used ghostview when the picture didn't contain any text. Otherwise, I'd convert the mp source to pdf and use a pdf viewer such as acroread. This was in the olden days when the clockspeeds were low. Nowaday's I'd use the latter approach, i.e. converting mp to pdf and view it with a pdf viewer.

FWIW here's my MPTOPDF script, which requires some sort of unix OS.

#!/bin/sh

# This will only work if mptopdf.fmt exists.
# Use pdftex --ini mptopdf'' to create it.
# It should be in the web2c directory.

SCRIPTNAME=$0 if [$# = 0 ]; then
echo "Usage: ${SCRIPTNAME} filin [filout]" 1>&2 exit 1 elif [$# = 1 ]; then
FILIN=$1 BASE=echo${FILIN} \
| sed -e 's/^$$.*$$\.[^.]*$/\1/' \  EXTN=echo${FILIN} \
| sed -e 's/^.*\.$$[^.]*$$$/\1/' \  FILOUT=${BASE}${EXTN}.pdf elif [$# = 2 ]; then
FILIN=$1 FILOUT=$2
else
echo "Usage: ${SCRIPTNAME} filin [filout]" 1>&2 exit 1 fi RESULT=echo${FILIN} \
| sed -e 's/^$$.*$$\.[^.]*$/\1\.pdf/' \  echo calling: pdftex '&mptopdf \relax'${FILIN}

pdftex '&mptopdf \relax' ${FILIN} mv${RESULT} \${FILOUT} 2> /dev/null

exit 0

-
Thanks for your answer, but I'm working in Windows... Anyway you're right about pictures that don't contain text. I'm able to preview them correctly. The problem arises when they do contain text! – Sterry Jun 8 '13 at 8:21
IIRC that's because the generated dvi files only contain information about the bounding boxes of the glyphs. Turning the file into a proper ps/pdf also embeds the fonts. – Marc van Dongen Jun 8 '13 at 9:00