[Tex/LaTex] How to use kile with feynmf or feynmp

automationbuild-systemfeynmanfeynmfkile

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?

Best 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
Related Question