This page shows various methods, tips, and tricks to compile LaTeX code of Feynman diagrams with the feynmp
package.
- Compiling standalone diagrams
- Compiling multiple standalone diagrams (multipage)
- Compiling diagrams as figures to a text (on-the-fly)
- Compiling diagrams inside equations
- Compiling standalone with LaTeXiT (macOS)
Compiling standalone diagrams
Standalone compilation is useful to create a single .tex
file that creates a single PDF that you can then import into other projects.
A simple example:
\documentclass[11pt,border=4pt]{standalone} \usepackage{feynmp-auto} \begin{document} \begin{fmffile}{feyngraph} \begin{fmfgraph*}(100,70) % dimensions (WH) \fmfleft{i2,i1} \fmfright{o2,o1} \fmf{fermion}{i2,v1,i1} \fmf{boson}{v1,v2} \fmf{fermion}{o1,v2,o2} \fmfv{decor.shape=circle,decor.filled=full,decor.size=4, foreground=(0,,0,,0.8)}{v1,v2} \end{fmfgraph*} \end{fmffile} \end{document}
You can change the default margins by changing border=4pt
in \documentclass
. One can further control the whitespace around the Feynman diagram, by wrapping the fmffile
or fmfgraph
with \fmfframe
. This command takes two pairs of floats as arguments: \fmfframe(L,T)(R,B)
, where L
, T
, R
, and B
are float values of the left, top, right, and bottom margin, respectively. Unfortunately, this needs to be fine-tuned sometimes, especially if you add the text labels to the external vertices.
For example:
\documentclass[11pt,border=4pt]{standalone} \usepackage{feynmp-auto} \begin{document} \begin{fmffile}{feyngraph} \fmfframe(-5,12)(-5,12){ % padding (L,T)(R,B) \begin{fmfgraph*}(100,70) % dimensions (WH) \fmfleft{i2,i1} \fmfright{o2,o1} \fmf{fermion}{i2,v1,i1} \fmf{boson}{v1,v2} \fmf{fermion}{o1,v2,o2} \fmfv{decor.shape=circle,decor.filled=full,decor.size=4, foreground=(0,,0,,0.8)}{v1,v2} \end{fmfgraph*} } % close \fmfframe \end{fmffile} \end{document}
The feynmp-auto
package has made it a lot easier to compile these diagrams than in the past. Still, you may need to compile twice with the -shell-escape
flag. The first compilation creates the Feynman diagrams and saves them as external MetaPost files. The second run includes these external files in the typeset PDF file.
If you compile the LaTeX file in the command line, you may need to run the following twice in the terminal:
pdflatex -shell-escape mydiagram.tex
If you use the TeXShop editor on macOS, you can include the following at the top of your document to automatically compile with pdflatexmk -shell-escape
:
% !TEX program = pdflatexmk
% !TEX parameter = -shell-escape
Compiling multiple standalone diagrams (multipage)
If you add more than one fmffile
or tikzpicture
environment in a standalone LaTeX document, the compiled PDF will contain multiple diagrams. This can be handy if you like everything in one place, where it is easier to maintain (all the code in one LaTeX file, all the diagrams in one PDF), create variations, and use some common settings like line or color style. For more information on that, see the tutorial for using color in feynmp
.
To import a particular page from a multipage PDF into a LaTeX, you can use something like
\includegraphics[width=0.25\textwidth,page=2]{fig/myfeyn.pdf}
To actually produce a multipage PDF document with each Feynman diagram on a separate page, you need to explicitly wrap the fmffile
in an page
environment as follows:
\documentclass[11pt,border=4pt,multi=page,crop]{standalone} \usepackage{feynmp-auto} \begin{document} % DIAGRAM 1 \begin{page}% to create standalone page \begin{fmffile}{feynmp-diagram1} \fmfframe(-6,10)(-6,10){ % padding (L,T)(R,B) \begin{fmfgraph*}(100,60) % dimensions (WH) \fmfleft{i2,i1} \fmfright{o2,o1} \fmf{fermion}{i1,v1,o1} \fmf{boson,tension=0.7}{v1,v2} % t-channel \fmf{fermion}{o2,v2,i2} \end{fmfgraph*} }% close \fmfframe \end{fmffile} \end{page} % DIAGRAM 2 \begin{page}% to create standalone page \begin{fmffile}{feynmp-diagram2} \fmfframe(-6,10)(-6,10){ % padding (L,T)(R,B) \begin{fmfgraph*}(100,60) % dimensions (WH) \fmfleft{i2,i1} \fmfright{o2,o1} \fmf{fermion}{i2,v1,i1} \fmf{boson,tension=0.7}{v1,v2} % s-channel \fmf{fermion}{o1,v2,o2} \end{fmfgraph*} }% close \fmfframe \end{fmffile} \end{page} \end{document}
Once you start adding more common settings, this can get a bit cumbersome to maintain. It might be useful to define a custom environment. In the following, a new environment called fmfpicture
is defined with \NewEnviron
. It takes three arguments: The first two set the padding with \fmfframe
, and the third one passes a name to fmffile
for the external MetaPost files.
\documentclass[11pt,border=4pt,multi=page,crop]{standalone} \usepackage{feynmp-auto} % DEFINE fmfpicture ENVIRONMENT \usepackage{environ} % for \NewEnviron \NewEnviron{fmfpicture}[3]{% \begin{page}% to create standalone page \fmfframe(#1)(#2){% padding (L,T)(R,B) \begin{fmffile}{feynmp-#3}% auxiliary files (use unique name!) \BODY % main code \end{fmffile} }% close \fmfframe \end{page} } \begin{document} % DIAGRAM 1 \begin{fmfpicture}{-2,10}{-2,10}{diagram1} % padding (LTRB) \begin{fmfgraph*}(100,60) % dimensions (WH) % external vertices \fmfleft{i2,i1} \fmfright{o2,o1} % main \fmf{fermion}{i1,v1,o1} \fmf{boson,tension=0.7}{v1,v2} % t-channel \fmf{fermion}{o2,v2,i2} \end{fmfgraph*} \end{fmfpicture} % DIAGRAM 2 \begin{fmfpicture}{-2,10}{-2,10}{diagram2} % padding (LTRB) \begin{fmfgraph*}(100,60) % dimensions (WH) % external vertices \fmfleft{i2,i1} \fmfright{o2,o1} % main \fmf{fermion}{i2,v1,i1} \fmf{boson,tension=0.7}{v1,v2} % s-channel \fmf{fermion}{o1,v2,o2} \end{fmfgraph*} \end{fmfpicture} % DIAGRAM 3 \begin{fmfpicture}{-2,10}{-2,10}{diagram3} % padding (LTRB) \begin{fmfgraph*}(100,60) % dimensions (WH) % external vertices \fmfleft{i2,i1} \fmfright{o2,o1} % main \fmf{fermion}{i1,v1} \fmf{fermion}{v2,i2} \fmf{fermion,tension=0.7}{v1,v2} % t-channel \fmf{boson}{v1,o1} \fmf{boson}{o2,v2} \end{fmfgraph*} \end{fmfpicture} \end{document}
Note the placement of %
behind some LaTeX code without any white space in-between. This is to prevent LaTeX from adding spurious spaces that increase the whitespace on the left and right of the figures.
Compiling diagrams as figures to a text (on-the-fly)
You can add the feynmp
code directly to your LaTeX document so it is compiled together with your text. Note however that for large documents with many figures this can increase the compilation time. It might be preferable to produce the figures standalone as PDF images that are included (see previous section).
\documentclass[a4paper,12pt]{article} \usepackage[margin=2.4cm]{geometry} % margins \usepackage{amsmath} \usepackage{graphicx} \usepackage{feynmp-auto} \begin{document} This is a test file for Feynman diagrams. You may need to compile twice: first time to compile and save the Feynman diagrams, second time to include them in the typeset PDF file. \begin{figure}[h] \vspace{10mm} \centering \begin{fmffile}{feynman-compton} \begin{fmfgraph*}(150,100) \fmfleft{i1,i2} \fmfright{o1,o2} \fmflabel{$\gamma$}{i2} \fmflabel{$e^-$}{i1} \fmflabel{$\gamma$}{o1} \fmflabel{$e^-$}{o2} \fmf{photon}{i2,v2} \fmf{fermion}{i1,v1,v2,o2} \fmf{photon}{v1,o1} \end{fmfgraph*} \end{fmffile} \vspace{5mm} \caption{Feynman diagram for Compton scattering} \label{compton} \end{figure} \end{document}
Compiling inside equations
Feynman diagrams represent mathematical expressions that describe interactions of particles. It is possible to insert Feynman diagrams with feynmp
in LaTeX with something like the following.
\documentclass[a4paper,12pt]{article} \usepackage{feynmp-auto} \begin{document} An equation with a Feynman diagram: \begin{equation}\label{eq:higgs_mass} \begin{fmffile}{higgs_mass} \Delta m_H^2 \quad = \quad \parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmf{dashes,tension=1}{i,v1} \fmf{dashes,tension=1}{v2,o} \fmf{fermion,left,tension=0.4,label=t}{v1,v2,v1} \end{fmfgraph*}} \quad + \quad\ldots \end{fmffile} \end{equation} \end{document}
A full example, shown how the finetuning problem of the Higgs mass can be solved with loops containing new SUSY particles:
The code below shows how you can achieve this:
% !TEX program = pdflatexmk % !TEX parameter = -shell-escape % Author: Izaak Neutelings (July 2017) \documentclass[a4paper,12pt]{article} \usepackage[margin=2.4cm]{geometry} % margins \usepackage{amsmath} \usepackage{graphicx} \usepackage{feynmp-auto} \begin{document} \section{Typesetting equations with Feynman diagrams} This is an equation of the dominant correction to the Higgs mass squared, containing a Feynman diagram: \begin{equation}\label{eq:hierarchy_problem} \begin{fmffile}{higgs_mass_correction} \Delta m_H^2 \,\, = \,\,\parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmf{dashes,tension=1}{i,v1} % ,label=H,label.side=left \fmf{dashes,tension=1}{v2,o} \fmf{fermion,left,tension=0.4,label=t}{v1,v2,v1} \end{fmfgraph*}} \,\, + \,\,\ldots \,\, = \,\, -\frac{|\lambda_\mathrm{t}|^2}{8\pi}\Lambda^2 + \ldots \end{fmffile} \end{equation} % SUSY To avoid finetuning in Eq.~\eqref{eq:hierarchy_problem}, one can postulate supersymmetry (SUSY), which assumes each fermion has a scalar superpartner. SUSY will introduce an extra loop diagram: \vspace{5mm} \begin{equation}\label{eq:hierarchy_problem_SUSY} \begin{fmffile}{higgs_mass_correction_SUSY} \Delta m_H^2 \,\, = \,\,\parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmf{dashes,tension=1}{i,v1} \fmf{dashes,tension=1}{v2,o} \fmf{fermion,left,tension=0.4,label=t}{v1,v2,v1} \end{fmfgraph*}} \,\, + \,\,\parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmftop{m} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmflabel{$\widetilde{\mathrm{t}}$}{m} \fmf{dashes,tension=1}{i,v1} \fmf{dashes,tension=1}{v1,o} \fmf{dashes,right,tension=0}{v1,m,v1} \end{fmfgraph*}} \,\, + \,\,\ldots \end{fmffile} \end{equation} % VLQ Yet another solution to finetuning are models with vector-like quarks (VLQs): \vspace{5mm} \begin{equation}\label{eq:hierarchy_problem_VLQ} \begin{fmffile}{higgs_mass_correction_VLQ} \Delta m_H^2 \,\, = \,\,\parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmf{dashes,tension=1}{i,v1} \fmf{dashes,tension=1}{v2,o} \fmf{fermion,left,tension=0.4,label=t}{v1,v2,v1} \end{fmfgraph*}} \,\, + \,\,\parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmf{dashes,tension=1}{i,v1} \fmf{dashes,tension=1}{v2,o} \fmf{fermion,left,tension=0.4,label=t}{v2,v1} \fmf{fermion,left,tension=0.4,label=T}{v1,v2} \end{fmfgraph*}} \,\, + \,\,\parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmftop{m} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmflabel{T}{m} \fmf{dashes,tension=1}{i,v1} \fmf{dashes,tension=1}{v1,o} \fmf{fermion,right,tension=0}{v1,m,v1} \end{fmfgraph*}} \,\, + \,\,\ldots \end{fmffile} \end{equation} Notice that in the code, \verb|\,| was used to add extra space between the diagrams and math symbols. Alternatively, you can use the wider \verb|\quad|. Because the stop label $\widetilde{\mathrm{t}}$ and VLQ top partner T labels were sticking out, an extra \verb|\vspace{5mm}| was needed above the equation to prevent these label from overlapping with the line above.\\ Equations with \verb|feymp| diagrams might not compile in the \verb|align| environment. Instead, use \verb|aligned| within the \verb|equation| environment. For example: \begin{equation} \begin{aligned} \Delta m_H^2 \,\, &= \begin{fmffile}{higgs_mass_correction} \,\,\parbox{80pt}{ \begin{fmfgraph*}(80,60) \fmfleft{i} \fmfright{o} \fmfv{label=H,l.a=60}{i} \fmfv{label=H,l.a=120}{o} \fmf{dashes,tension=1}{i,v1} \fmf{dashes,tension=1}{v2,o} \fmf{fermion,left,tension=0.4,label=t}{v1,v2,v1} \end{fmfgraph*}} \,\, + \,\,\ldots \end{fmffile}\\ %[10pt] % to add extra vertical white space between the line &= \,\, -\frac{|\lambda_\mathrm{t}|^2}{8\pi}\Lambda^2 \,\, + \,\,\ldots \end{aligned} \end{equation} Another tip: use \verb|\\[10pt]| instead of just \verb|\\| at the end of a line in the \verb|aligned| environment to add extra vertical white space between two lines of equations. \end{document}
Compiling standalone with LaTeXiT (macOS)
LaTeXiT is a very nice macOS app that typesets LaTeX code as standalone images (JPG, PNG, PDF, …). This is extremely useful for preparing high-quality equations in PowerPoint presentations, and it can also allow you to create a standalone image of a Feynman diagram if you configure it correctly. It takes some steps, please refer to the instructions on this page by Taku Yamanaka.