Background
The latexmk tool can be used to automatically process your LaTeX files enough times to generate the output. As discussed here and here, latexmk now supports setting out_dir which passes the -output-directory to pdflatex and places build products in this alternate location. Unfortunately, this makes it difficult to use latexmk with feynmp.
Question
How can I configure latexmk to call mpost for processing Feynman diagrams generated by feynmp? Especially using the new out_dir configuration?
Partial Answer 1
One can try to add a custom rule to the latexmkrc file like
add_cus_dep('mp', '1', 0, 'mpost');
sub mpost {
system("mpost $_[0]");
}
but this fails in three ways: 1) if there are multiple images, then the extensions .1 changes and it is not clear how to specify all the dependences. 2) I don't know how to tell latexmk that pdflatex needs to be run again after the figure is generated. 3) Even if out_dir is specified, latexmk still runs the mpost command in the source directory, thereby missing the generated .mp files.
Partial Answer 2
As suggested in this answer, one can use the -shell-escape option of pdflatex to allow the figures to be processed by the call to pdflatex. Unfortunately, the call to mpost still takes place in the top-level directory. Is there some way of using -output-directory to ensure that shell commands are executed in the appropriate place?
Here is a MWE demonstrating this second attempt:
latexmkrc
mkdir _build;
$out_dir = '_build';
$pdflatex="pdflatex -shell-escape -interaction=nonstopmode %O %S";
tst.tex
\documentclass{scrbook}
\usepackage{feynmp}
\usepackage{etoolbox}
\DeclareGraphicsRule{*}{mps}{*}{}
\makeatletter
\show\endfmffile
\preto{\endfmffile}{\let\the@fmffile\thefmffile}
\appto{\endfmffile}{
\ifnum\pdfshellescape=\@ne
\immediate\write18{mpost \the@fmffile}%
\else
\message{
Run pdf(la)tex with -shell-escape to generate feynmp diagrams}
\fi
\let\the@fmffile\relax
}
\makeatother
\begin{document}
\begin{fmffile}{title}
\begin{fmfgraph}(40,25)
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,o1}
\fmf{fermion}{i2,v2,o2}
\fmf{photon}{v1,v2}
\end{fmfgraph}
\end{fmffile}
\end{document}
mpostignores the--output-directoryoption. – egreg Dec 04 '11 at 23:11\write18{cd _build;mpost \the@fmffile}. Is there any way to get the argument to--output-directoryprogramatically? – mforbes Dec 04 '11 at 23:21"\def\outdir{_build}\input{%S}"instead of%S(possibly escaping backslashes and braces) and saying\write18{cd \outdir;mpost \the@fmffile}. – egreg Dec 04 '11 at 23:36