[Tex/LaTex] Feynman Diagrams with feynMF and feynMP

feynmf

Im triying to learn how to draw Feynman diagrams by using feynMF which I installed from this link and compiled. however even in this simple example which I took from manual it gave me ! I detected a fmfgraph environment outside of fmffile. ! I detected a fmfgraph environment outside of fmffile. these errors. how can I fix this problem? thanks

\documentclass[11pt]{article}    
\usepackage{feynmf}
\begin{document}
\begin{fmfgraph}(40,25)
\fmfpen{thick}
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,v3,o1}
\fmf{fermion}{o2,v4,v2,i2}
\fmf{photon}{v1,v2}
\fmf{photon}{v3,v4}
\fmfdotn{v}{4}
\end{fmfgraph}
\end{document}

Best Answer

Nowadays it's better using feynmp rather than feynmf, because it's easier to produce the pictures. feynmp also has the distinct feature of using Metapost, which produces EPS vector graphics, while FeynMF uses Metafont, which produces bitmaps. According to Thorsten Ohl, the creator of this package, feynmf is outdated.

In any case you have to tell the package the name of the auxiliary file where the Metapost code is to be written, which is done by opening a fmffile environment.

\documentclass[11pt]{article}
\usepackage{feynmp}
\DeclareGraphicsRule{*}{mps}{*}{} % for being able to read the produced file

\begin{document}

\begin{fmffile}{afilename}% choose something better!
\begin{fmfgraph}(40,25)
\fmfpen{thick}
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,v3,o1}
\fmf{fermion}{o2,v4,v2,i2}
\fmf{photon}{v1,v2}
\fmf{photon}{v3,v4}
\fmfdotn{v}{4}
\end{fmfgraph}
\end{fmffile}

\end{document}

After running LaTeX, you have to run mpost afilename and then recompile. If you have a fairly recent TeX distribution, you can use feynmp-auto:

\documentclass[11pt]{article}
\usepackage{feynmp-auto}

\begin{document}

\begin{fmffile}{afilename}% choose something better!
\begin{fmfgraph}(40,25)
\fmfpen{thick}
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,v3,o1}
\fmf{fermion}{o2,v4,v2,i2}
\fmf{photon}{v1,v2}
\fmf{photon}{v3,v4}
\fmfdotn{v}{4}
\end{fmfgraph}
\end{fmffile}

\end{document}

and you'll just have to compile twice (the second LaTeX run is needed only if the code for a Feynman diagram has changed or some new diagram has been added).