7

I have read several questions related to metapost and latex here on TEX SE, but I still cannot solve my own problem.

So it is a very basic problem. I am trying to include a metapost graphic in my .tex file. What I did is I save another .mp file that has this code

beginfig (1);  
  % draw a line  
  draw (1cm,2cm) -- (3cm,5cm);  
endfig;  
end.

Then in my .tex code, I use the package graphicx, then write \includegraphics{trial.mp}, where trial is the name of my .mp file.

But it did not work.

Next I tried to change the code to \includegraphics{trial.mps}, it printed the diagram but with a file name (trial) on top. And I don't want that, I just want the picture.

  1. My question is what is the difference between .mp and .mps?

Not satisfied, I tried to write the code immediately on my .tex file by using the package mpgraphics

\begin{mpdisplay}  
draw (1cm,2cm) -- (5cm,3cm) ;  
\end{mpdisplay}

But it did not work.

  1. My next question is, is there any way I can write the metapost code inside my .tex code so that I don't have to import from other file?

Many many thanks for the help!

7

I hope not coming too late… About your second question, I use two solutions regularly to integrate MetaPost code directly into a LaTeX document.

The first method is using the gmp package already mentioned by dgoodmaniii.

\documentclass{article}
\usepackage[shellescape]{gmp}
\begin{document}
\begin{mpost*}
beginfig(1);
$ := 1;
for i = -.3cm step .6cm until 3.6cm:
  % draw a line  
  draw (1cm,2cm) -- (3cm,5cm); 
endfor;
endfig;          
\end{mpost*}
\end{document}

You must run either LaTeX, pdfLaTeX, XeLaTeX or LuaLaTeX on it with the shell-escape option, if you want to produce your drawing directly, like with this example (pdflatex):

pdflatex --shell-escape mypic.tex

In this case, a MetaPost file mypic+mp0001.mp is created, then compiled to produce a mypic+mp0001.mps file which is eventually included in your LaTeX program. All this "on the fly".

Otherwise (without the shellescape option for the gmp package, or without the --shell-escape option of the pdflatex command), the first run will only produce the MetaPost file mypic+mp0001.mp. You will have to process it and then include the resulting .mps image, all this by yourself. Or you may execute a shell script to take care of these steps: mypic+mp.sh, which is also created during the first run.

You can see that the shell-escape way is by far preferable :-)

The other way I know of to integrate MetaPost code in a LaTeX program is to use LuaLaTeX and its luamplib package.

\documentclass{article}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
$ := 1;
for i = -.3cm step .6cm until 3.6cm:
  % draw a line  
  draw (1cm,2cm) -- (3cm,5cm); 
endfor;
endfig;          
\end{mplibcode}
\end{document}

This is the most direct and straightforward solution: no importing, no shell-escape option needed. But it is LuaLaTeX-specific, contrary to gmp which can be used on any engine (LaTeX+DVI, pdfLaTeX, XeLaTeX, LuaLaTeX…).

Either way, your picture will be nicely drawn in your document:

enter image description here

5

Metapost is a declarative graphics language; the program describes the image but is not the image itself. You have to compile the mp file to produce the mps file which pdftex includes. Depending on your OS, something like this should do it:

mpost image.mp

This will produce a file image.1, which you rename image.mps to include it with pdftex. (There are ways to make Metapost produce a file ending in .mps, and ways to make pdftex accept files with .1, but I leave these as an exercise in reading the documentation.)

For including Metapost source within your TeX source, look into the gmp package.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.