# How to use kile with feynmf or feynmp?

Does somebody know how to teach kile to automatically do the additional steps for compiling Latex documents that contain Feynman diagrams produced with feynmf or feynmp?

For what I know I have to run latex, then run mf title (or for some reason mf '\mode:=localfont;' input title; mpost for feynmp) for every Feynman graph defined with

\begin{fmffile}{title}
% the actual diagram definition
\end{fmffile}


and then run latex again. Can this conveniently be automized in kile?

-

## 1 Answer

The following works with pdflatex and feynmp:

\usepackage{feynmp}
\DeclareGraphicsRule{*}{mps}{*}{}

\makeatletter
\def\endfmffile{%
\fmfcmd{\p@rcent\space the end.^^J%
end.^^J%
endinput;}%
\if@fmfio
\immediate\closeout\@outfmf
\fi
\ifnum\pdfshellescape=\@ne
\immediate\write18{mpost \thefmffile}%
\fi}
\makeatother


If you now call pdflatex with the -shell-escape option (-enable-write18 for MiKTeX), the Metapost file is compiled right after the end of the environment and will be used at the next compilation.

NOTE TeX Live 2012 lists mpost among the programs that can be run with "restricted shell escape", so the test above can be

  \ifnum\pdfshellescape>\z@


and -shell-escape is not needed any more. Note though that MiKTeX 2.9 doesn't allow mpost in restricted shell escape.

For feynmf it's just the same:

\usepackage{feynmf}

\makeatletter
\def\endfmffile{%
\fmfcmd{\p@rcent\space the end.^^J%
end.^^J%
endinput;}%
\if@fmfio
\immediate\closeout\@outfmf
\fi
\ifnum\pdfshellescape=\@ne
\immediate\write18{mf "\noexpand\mode:=localfont;input \thefmffile"}%
\fi}
\makeatother


Adding -shell-escape to the call of (pdf)latex should be easy in Kile.

I advise to use feynmp that's easier to manage and produces resolution independent output. Moreover it supports color.

Another way, for older systems, is to open an output stream where to write the necessary shell commands.

\usepackage{feynmf}

\makeatletter
\newwrite\@feynmfwr
\immediate\openout\@feynmfwr=\jobname.out
\def\endfmffile{%
\fmfcmd{\p@rcent\space the end.^^J%
end.^^J%
endinput;}%
\if@fmfio
\immediate\closeout\@outfmf
\fi
\immediate\write\@feynmfwr{mf "\noexpand\mode:=localfont;input \thefmffile"}%
}
\makeatother


At the end of the LaTeX run you'll have a file called as your main file, with extension .out that contains the necessary shell commands to run. If the file is main.tex all you have to do is to run from the shell

sh main.out

-
Is there any way to have this work with both pdflatex and latex at the same time? This neat trick results in latex complaining "Can not include graphics of type: mps." (If mpost has already run) – Tobias Kienzler May 2 '13 at 12:06
@TobiasKienzler Can you try with feynmp-auto instead of feynmp? This should work without the need of any trick: the feynmp-auto package is just an implementation of the trick and loads feynmp automatically. – egreg May 2 '13 at 16:39
Works like a charm, thanks! (It didn't show up in MikTeX since I forgot to synchronize the repo...) In MikTeX, the -enable-write18 switch does the trick for both pdflatex and latex, I think you mention that only in this post but not in the package documentation. – Tobias Kienzler May 3 '13 at 7:57
Minor bug-report: If one has run LaTeX (but not mpost) before switching to feynmp-auto, the already existing .mp file will not be considered "changed" and therefore mpost won't ever be run. A workaround is deleting all .mp files when switching to feynmp-auto. Maybe the package can check for the existence of processed diagrams as well? – Tobias Kienzler May 3 '13 at 8:04
@TobiasKienzler Version 1.1 is on its way to CTAN – egreg May 3 '13 at 13:26