# Package for typesetting Feynman diagrams (efficiency of feynmp and tikz)

This question led to a new package:
TikZ-Feynman

Is there any good package for typesetting lots of Feynman diagrams?

My question arise because Feynmp: Circle with three vertices lead me take a look at the documentation for the feynmf/feynmp package, and didn't like the package very much. In my opinion this package has several draw-backs: it doesn't work with pdflatex unless you get help from @egreg (How to use kile with feynmf or feynmp?), the notation is not very intuitive (what do left=.5 mean?), etc.

I thought that there must be some better package for typesetting Feynman diagram (e.g. something that builds on TikZ rather than metapost), but CTAN gave me only this.

So do you know something better than feynmp?

EDIT:

Since @cmhughes comment suggest to use tikz rather than feynmp (and no one has suggested any other packages), the questions are this:

• Which package is most efficient for typesetting Feynman diagrams, feynmp or tikz?

• Which package produces the best (this means the most beautiful) results?

In order for tikz to really compete with feynmp (on this particular battlefield), the code must be approximately as simple as with feynmp, and the results must be at least as good. Below you will find some examples of Feynman diagrams made using feynmp. How can you typeset diagrams as simple as possible using tikz?

Code:

\documentclass{memoir}

\usepackage{feynmp}

\begin{document}
\unitlength=1mm
\centering

\begin{fmffile}{photon} % Tree-level interaction
\begin{fmfgraph*}(40,25)
\fmfkeep{photon}
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,i2}
\fmf{fermion}{o1,v2,o2}
\fmf{photon}{v1,v2}
\end{fmfgraph*}
\end{fmffile}
~
\begin{fmffile}{gluon}
\begin{fmfgraph*}(40,25)
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,i2}
\fmf{fermion}{o1,v2,o2}
\fmf{gluon}{v1,v2}
\end{fmfgraph*}
\end{fmffile}

\plainbreak{1}

\begin{fmffile}{self} % Self-interaction
\begin{fmfgraph*}(40,25)
\fmfleft{i}
\fmfright{o}
\fmf{plain}{i,v1}
\fmf{fermion}{v1,v2}
\fmf{plain}{v2,o}
\fmf{photon,left}{v1,v2}
\end{fmfgraph*}
\end{fmffile}
~
\begin{fmffile}{loop} % Loop correction
\begin{fmfgraph*}(40,25)
\fmfkeep{loop}
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,i2}
\fmf{fermion}{o1,v4,o2}
\fmf{photon}{v1,v2}
\fmf{photon}{v3,v4}
\fmf{fermion,right,tension=.2}{v2,v3,v2}
\end{fmfgraph*}
\end{fmffile}

\begin{fmffile}{penguin} % Penguin?
\begin{fmfgraph*}(70,80)
\fmftop{t1,t2}
\fmfbottom{b1,b2}
\fmf{fermion}{t1,v1}
\fmf{fermion}{v2,t2}
\fmf{fermion,tension=.5}{v1,v3,v2}
\fmf{boson,tension=.5}{v1,v2}
\fmf{gluon}{v3,v4}
\fmf{fermion}{b1,v4,b2}
\end{fmfgraph*}
\end{fmffile}

\end{document}


Please, note that Feynman Diagrams with tikz and texample: Feynman diagram do not answer this question.

• I'd use tikz or PSTricks :) Dec 12 '12 at 4:31
• It should be possible to use only Lua for those pictures which require it using the external library from TikZ. You’ll probably need to adjust the preamble a little bit (encoding, fonts, etc.). Jun 29 '13 at 19:40
• @Qrrbrbirlbel - The point should be to avoid Lua completely, since most physicists, I know, don't use Lualatex at all. Actually, I (hardly) never use it myself, and frankly I have no desire to do so, if I can avoid it (call me stubborn). Jun 30 '13 at 9:17
• I actually prefer feynmp over tikz since the feynmp diagrams look nicer (in my opinion). The syntax and compiling procedure is certainly annoying (Makefiles are good for this), but it works fine with pdflatex and lualatex.
– xvtk
Jun 30 '13 at 14:51
• @xvtk have you ever heard about feynmp-auto package? Jun 30 '13 at 16:55

The CVS version of TikZ contains a graph layout library, which works surprisingly well for this. Here are a couple of your examples. The syntax could be shortened (f instead of fermion, for example), this is just to show the idea:

\graph [spring layout, nodes=coordinate, horizontal'=c to d]
{
c -- [fermion] a,
b --[fermion] c -- [photon] d,
e -- [fermion] d -- [fermion] f;
};


\graph [spring layout, nodes=coordinate, horizontal'=c to d]
{
c -- [fermion] a,
b --[fermion] c -- [gluon] d,
e -- [fermion] d -- [fermion] f;
};


\graph [spring layout, anchor at={(0,-4)}, nodes=coordinate, horizontal'= b to d]
{
a -- [fermion] b -- [fermion] c,
b -- [photon] d -- [left, fermion] e -- [left, fermion] d,
e -- [photon] f -- [fermion] g,
h -- [fermion] f;
};


\graph [spring layout, anchor at={(0,-5)}, nodes=coordinate, vertical= e to f]
{
a -- [fermion] b -- [photon] c -- [fermion] d,
b -- [fermion] e -- [fermion] c,
e -- [gluon]  f,
h -- [fermion] f -- [fermion] i
};


There are some issues with decorating curved paths using waves, and the coils decoration sometimes ends with a straight path segment, but that could be fixed.

Here's the complete code (requires lualatex).

\documentclass[a4paper,12pt, border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations, decorations.markings, decorations.pathmorphing, arrows, graphs, graphdrawing, shapes.geometric, snakes}
\usegdlibrary{trees,force, layered}

\pgfdeclaredecoration{complete sines}{initial}
{
\state{initial}[
width=+0pt,
next state=sine,
persistent precomputation={\pgfmathsetmacro\matchinglength{
\pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
\setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
}] {}
\state{sine}[width=\pgfdecorationsegmentlength]{
\pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
\pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
\pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
\pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
\state{final}{}
}

\tikzset{
photon/.style={
decoration={complete sines, amplitude=0.15cm, segment length=0.2cm},
decorate
},
fermion/.style={
decoration={
markings,
mark=at position 0.5 with {\node[transform shape, xshift=-0.5mm, fill=black, inner sep=1pt, draw, isosceles triangle]{};}
},
postaction=decorate
},
gluon/.style={
decoration={coil, aspect=0.75, mirror, segment length=1.5mm},
decorate
},
left/.style={
bend left=90,
looseness=1.75
}
}

\begin{document}%
\begin{tikzpicture}
\graph [spring layout, nodes=coordinate, horizontal'=c to d]
{
c -- [fermion] a,
b --[fermion] c -- [photon] d,
e -- [fermion] d -- [fermion] f;
};

\graph [spring layout, anchor at={(0,-2)}, nodes=coordinate, horizontal'=c to d]
{
c -- [fermion] a,
b --[fermion] c -- [gluon] d,
e -- [fermion] d -- [fermion] f;
};

\graph [spring layout, anchor at={(0,-4)}, nodes=coordinate, horizontal'= b to d]
{
a -- [fermion] b -- [fermion] c,
b -- [photon] d -- [left, fermion] e -- [left, fermion] d,
e -- [photon] f -- [fermion] g,
h -- [fermion] f;
};

\graph [spring layout, anchor at={(0,-5)}, nodes=coordinate, vertical= e to f]
{
a -- [fermion] b -- [photon] c -- [fermion] d,
b -- [fermion] e -- [fermion] c,
e -- [gluon]  f,
h -- [fermion] f -- [fermion] i
};
\end{tikzpicture}
\end{document}


• Thanks, okay I guess I'll just wait for the next release of tikz and use manual labour until then. I was able to create a diagram using code like here. However, I failed to create a diagram with a semicircle over a straight line. Like here. I tried arc, but it doesn't connect to the correct node. Do you know a good reference for that? I don't have too much time in the moment. But, otherwise I'll make a proper question of that issue when I have time. Apr 26 '13 at 8:03
• @Jake, when I try to run your example I get a memoization table filled incorrectly error from Path.lua. Any idea what the problem is? Apr 10 '14 at 15:42
• I've also tried with latest in TeXLive and CVS and it doesn't work. Something about using node=coordinate breaks it (although not using this option looks ugly). Sep 15 '14 at 9:40
• Looking further into it, it seems to be a result of the following bug: sourceforge.net/p/pgf/bugs/319 Sep 29 '14 at 18:35
• I have found a fix for this bug, and I have created a small package based on this answer if anyone is interested: tikz-feynman. PS: I have tried contacting the developers behind TikZ in many different ways, but none of them have responded regarding this fix... May 3 '15 at 12:55

The answer by Jake inspired me to write TikZ-Feynman, a dedicated package that uses TikZ' graph drawing library in order to draw Feynman diagrams. It has just recently been uploaded to CTAN and you can view more information over on the project page.

Below is the code to draw three diagrams of increasing complexity which showcase some of the feature of TikZ-Feynman.

% A simple tree-level diagram
\feynmandiagram [horizontal=a to b] {
i1 -- [fermion] a -- [fermion] i2,
a -- [photon] b,
f1 -- [fermion] b -- [fermion] f2,
};


% Penguin diagram with momentum arrows
\feynmandiagram [large, vertical=e to f] {
a -- [fermion] b -- [photon, momentum=$$k$$] c -- [fermion] d,
b -- [fermion, momentum'=$$p_{1}$$] e -- [fermion, momentum'=$$p_{2}$$] c,
e -- [gluon]  f,
h -- [fermion] f -- [fermion] i;
};


% Meson mixing and decay
\begin{tikzpicture}
\begin{feynman}
\vertex (a1) {$$\overline b$$};
\vertex[right=1cm of a1] (a2);
\vertex[right=1cm of a2] (a3);
\vertex[right=1cm of a3] (a4) {$$b$$};
\vertex[right=1cm of a4] (a5);
\vertex[right=2cm of a5] (a6) {$$u$$};

\vertex[below=2em of a1] (b1) {$$d$$};
\vertex[right=1cm of b1] (b2);
\vertex[right=1cm of b2] (b3);
\vertex[right=1cm of b3] (b4) {$$\overline d$$};
\vertex[below=2em of a6] (b5) {$$\overline d$$};

\vertex[above=of a6] (c1) {$$\overline u$$};
\vertex[above=2em of c1] (c3) {$$d$$};
\vertex at ($(c1)!0.5!(c3) - (1cm, 0)$) (c2);

\diagram* {
{[edges=fermion]
(b1) -- (b2) -- (a2) -- (a1),
(b5) -- (b4) -- (b3) -- (a3) -- (a4) -- (a5) -- (a6),
},
(a2) -- [boson, edge label=$$W$$] (a3),
(b2) -- [boson, edge label'=$$W$$] (b3),

(c1) -- [fermion, out=180, in=-45] (c2) -- [fermion, out=45, in=180] (c3),
(a5) -- [boson, bend left, edge label=$$W^{-}$$] (c2),
};

\draw [decoration={brace}, decorate] (b1.south west) -- (a1.north west)
node [pos=0.5, left] {$$B^{0}$$};
\draw [decoration={brace}, decorate] (c3.north east) -- (c1.south east)
node [pos=0.5, right] {$$\pi^{-}$$};
\draw [decoration={brace}, decorate] (a6.north east) -- (b5.south east)
node [pos=0.5, right] {$$\pi^{+}$$};
\end{feynman}
\end{tikzpicture}
\end{document}


All that is needed in the preamble to generate these is \usepackage{tikz-feynman}. So in the case of the first diagram:

\documentclass[tikz]{standalone}
\usepackage{tikz-feynman}
\begin{document}
\feynmandiagram [horizontal=a to b] {
i1 -- [fermion] a -- [fermion] i2,
a -- [photon] b,
f1 -- [fermion] b -- [fermion] f2,
};
\end{document}

• This is beautiful! Jan 28 '16 at 15:07
• I copied and paste your examples. The resulting layout is weird and different from the figures you present. Nov 30 '17 at 19:42
• @yarchik Instead of down voting my response, perhaps consider checking whether the error is on your side. Have you checked for any warnings in your log file? In all likelihood, you have the same issue as presented here. If not, then consider asking a new question on this site. Nov 30 '17 at 23:42
• The voting is for a particular answer, which I find can be improved by adding important details, such as that it has to be compiled with lualatex, not with pdflatex, not with xelatex etc. In the manual to your package you mention about less steep learning curve as compared to other possibilities. May well be, but if such a hurdle is faced just at the beginning, less persistent people may give up. I revert my down vote, but I have to admit that there are several other examples, even from the manual or this site that are not directly copy-pasteable. Even here, edge labels must be in braces. Dec 1 '17 at 8:05
• Sorry for this message. Can I have some suggestions for your packages? I have sent you also an email. Thank you very much. Aug 22 '19 at 13:46