Difference between revisions of "Latex sample metapost"

From UCB Math Wiki
Jump to: navigation, search
(Initial creation)
 
(Updated to use "filenametemplate")
Line 7: Line 7:
 
  \usepackage{graphicx}
 
  \usepackage{graphicx}
 
  \begin{document}
 
  \begin{document}
  Hi there:  \includegraphics{file.1}
+
  Hi there:  \includegraphics{file1.mps}
 
  \end{document}
 
  \end{document}
  
 
And, here is the MetaPost file '''file.mp'''.  It contains an arrow and
 
And, here is the MetaPost file '''file.mp'''.  It contains an arrow and
 
the letter "A" just above the middle of the arrow.
 
the letter "A" just above the middle of the arrow.
 +
filenametemplate "%j%c.mps";
 
  beginfig(1);
 
  beginfig(1);
 
   
 
   
Line 36: Line 37:
  
 
===Notes:===
 
===Notes:===
* The file '''file.1''' produced by '''mpost''' is not really a PostScript file; therefore you cannot use '''gv''' to view it.
+
* The file '''file1.mps''' produced by '''mpost''' is not really a PostScript file; therefore you cannot use '''gv''' to view it.
 
* For the same reason, '''xdvi''' will not show '''file.dvi'''.
 
* For the same reason, '''xdvi''' will not show '''file.dvi'''.
  
 
==Creating ''pdf'' Files==
 
==Creating ''pdf'' Files==
  
If you want to create a ''pdf'' file, modify the ''tex'' file to read as follows:
+
If you want to create a ''pdf'' file, just use the following commands:
\documentclass{article}
 
\usepackage{graphicx}
 
\usepackage{ifpdf}
 
 
\ifpdf
 
  \DeclareGraphicsRule{*}{mps}{*}{}
 
\fi
 
 
\begin{document}
 
Hi there:  \includegraphics{file.1}
 
\end{document}
 
 
 
(You may also use this version of the file to produce a ''dvi'' file.)
 
 
 
Then use the following commands to produce the ''pdf'' file:
 
 
  mpost file
 
  mpost file
 
  pdflatex file
 
  pdflatex file

Revision as of 22:31, 1 June 2010

A Quick MetaPost Example

This document gives a quick example of how to use MetaPost for graphics in LaTeX.

The Files

First of all, here are the contents of file.tex:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
Hi there:  \includegraphics{file1.mps}
\end{document}

And, here is the MetaPost file file.mp. It contains an arrow and the letter "A" just above the middle of the arrow.

filenametemplate "%j%c.mps";
beginfig(1);

z1=(0,0);
z2=(10mm,10mm);

drawarrow(z1--z2);
label.ulft(btex $A$ etex, .5[z1,z2]);

endfig;

bye

Creating and Viewing dvi Files

To create a dvi file, use the following commands. You don't need to run mpost a second time if you just update the tex file.

mpost file
latex file

To view the dvi file, use the commands:

dvips -o file.ps file
gv file.ps

Notes:

  • The file file1.mps produced by mpost is not really a PostScript file; therefore you cannot use gv to view it.
  • For the same reason, xdvi will not show file.dvi.

Creating pdf Files

If you want to create a pdf file, just use the following commands:

mpost file
pdflatex file

Again, it is not necessary to run mpost if it has already been run since file.mp was last updated.