User Tools

Site Tools


latex:feynman_file

Generating Feynman diagrams in a LaTeX file

You may need to compile the LaTeX file twice with -shell-escape.

If you use TeXShop for 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

However, for Mac users however, feynmp in LaTeXiT is a better option to quickly build of Feynman diagrams as standalone images and use \includegraphics.

Method 1: feynmp-auto

Load the feynmp-auto package, documented here.

\usepackage{feynmp-auto}
feynman_auto.tex
% !TEX program = pdflatexmk
% !TEX parameter = -shell-escape
% https://wiki.physik.uzh.ch/cms/latex:feynman_file#method_1feynmp-auto
% https://www.overleaf.com/learn/latex/Feynman_diagrams#Other_packages_for_drawing_Feynman_diagrams
\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 need to compile twice: once to compile and save the Feynman diagrams, twice 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}

Method 2: feynmp-auto (standalone)

To create a standalone Feynman diagram, use the standalone class:

feynman_standalone.tex
% !TEX program = pdflatexmk
% !TEX parameter = -shell-escape
% Author: Izaak Neutelings (February 2023)
\documentclass[10pt,border=2pt]{standalone}
\usepackage{amsmath}
\usepackage{feynmp-auto}
 
\begin{document}
 
\fmfframe(0,15)(0,15){ % padding (LTRB)
\begin{fmffile}{feynman-compton}
  \begin{fmfgraph*}(110,80)
    \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}
}
 
\end{document}

To create one multiple PDF with one Feynman diagram per page, add the option multi=page,crop to the standalone class and wrap each fmffile around the page environment:

% !TEX program = pdflatexmk
% !TEX parameter = -shell-escape
% Author: Izaak Neutelings (February 2023)
\documentclass[10pt,border=2pt,multi=page,crop]{standalone}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{feynmp-auto}
 
\begin{document}
 
\begin{page}
\fmfframe(0,15)(0,15){ % padding (LTRB)
\begin{fmffile}{feynman-compton}
  \begin{fmfgraph*}(110,80)
    \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}
}
\end{page}
 
\begin{page}
\fmfframe(0,15)(0,15){ % padding (LTRB)
\begin{fmffile}{feynman-scat}
  \begin{fmfgraph*}(110,80)
    \fmfleft{i1,i2}
    \fmfright{o1,o2}
    \fmflabel{$\mu^-$}{i1}
    \fmflabel{$e^-$}{i2}
    \fmflabel{$\mu^-$}{o1}
    \fmflabel{$e^-$}{o2}
    \fmf{fermion}{i1,v1,o1}
    \fmf{fermion}{i2,v2,o2}
    \fmf{photon,label=$\gamma$}{v1,v2}
  \end{fmfgraph*}
\end{fmffile}
}
\end{page}
 
\end{document}

Method 3: Macro

This is a handy macro that allows you to compile Feynman graphs in LaTeX documents, without the need for extra scripts (source).

\usepackage{feynmp}
 
% macro to compile Feynman graphs without extra scripts
\DeclareGraphicsRule{*}{mps}{*}{}
\makeatletter
\def\endfmffile{
  \fmfcmd{\p@rcent\space the end.^^J end.^^J endinput;}
  \if@fmfio
    \immediate\closeout\@outfmf
  \fi
  \ifnum\pdfshellescape>\z@
    \immediate\write18{mpost \thefmffile}
  \fi}
\makeatother

Simply include the macro in the preamble. (Tested only in TeXShop on a Mac.)

feynman_macro.tex
% !TEX program = pdflatexmk
% !TEX parameter = -shell-escape
% https://wiki.physik.uzh.ch/cms/latex:feynman_file#method_1feynmp-auto
% https://www.overleaf.com/learn/latex/Feynman_diagrams#Other_packages_for_drawing_Feynman_diagrams
\documentclass[a4paper,12pt]{article}
\usepackage[margin=2.4cm]{geometry} % margins
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{feynmp}
 
% macro to compile Feynman graphs without extra scripts
\DeclareGraphicsRule{*}{mps}{*}{}
\makeatletter
\def\endfmffile{
  \fmfcmd{\p@rcent\space the end.^^J end.^^J endinput;}
  \if@fmfio
    \immediate\closeout\@outfmf
  \fi
  %\ifnum\pdfshellescape=\@ne
  \ifnum\pdfshellescape>\z@
    \immediate\write18{mpost \thefmffile}
  \fi}
\makeatother
 
\begin{document}
 
This is a test file for Feynman diagrams. You need to compile twice: once to compile and save the Feynman diagrams, twice 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}

Output

latex/feynman_file.txt · Last modified: 2023/02/04 16:45 by iwn