[Tex/LaTex] Feynman diagrams with loops using TikZ

tikz-pgf

I've been doing Feynman diagrams with TikZ for quite some time now, but I have never needed to make loops. Now I'm forced to deal with them, and here comes the issue. I've played around a little and have been inspired by other questions to use the calc package in order to draw nice loops.

Here's the best I can do. Please note how in the first diagram, the circle is messed up, while in the second (without the additional labels) it's fine. Ideally, the semicircles should carry one individual label each, the position of which should be adjustable (or should auto-adjust, depending on the orientation of the spline).

\documentclass{article}
\usepackage{pgfplots}

\usetikzlibrary{positioning,arrows,patterns}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc} 

\tikzset{
  photon/.style={decorate, decoration={snake}, draw=black},
  fermion/.style={draw=black, postaction={decorate},decoration={markings,mark=at position .55 with {\arrow{>}}}},
  vertex/.style={draw,shape=circle,fill=black,minimum size=3pt,inner sep=0pt},
}

\newcommand{\semiloop}[4][]{%
        \draw[#1] let \p1 = ($(#3)-(#2)$) in (#3) arc (#4:({#4+180}):({0.5*veclen(\x1,\y1)});)
}         

\begin{document}
\begin{tikzpicture}[node distance=1cm and 1cm]
\coordinate[vertex] (v1);
\coordinate[vertex, right=of v1] (v2);
\coordinate[vertex, right=of v2] (v3);
\coordinate[vertex, right=of v3] (v4);
\coordinate[above right=of v4,label=right:$f$] (f1);
\coordinate[below right=of v4,label=right:$f$] (f2);
\coordinate[above left =of v1,label=left :$e^+$] (e1);
\coordinate[below left =of v1,label=left :$e^-$] (e2);
\draw[fermion] (e1) -- (v1);
\draw[fermion] (v1) -- (e2);
\draw[photon] (v1) -- (v2) node[midway,above=0.1cm] {$Z$};
\draw[photon] (v3) -- (v4) node[midway,above=0.1cm] {$Z$};
\draw[fermion] (f1) -- (v4);
\draw[fermion] (v4) -- (f2);
% the next two lines are causing the mess
\semiloop[fermion]{v2}{v3}{0}  node[midway,above=0.1cm] {$f$};
\semiloop[fermion]{v3}{v2}{180} node[midway,above=0.1cm] {$\bar{f}$};
\end{tikzpicture}

\begin{tikzpicture}[node distance=1cm and 1cm]
\coordinate[vertex] (v1);
\coordinate[vertex, right=of v1] (v2);
\coordinate[vertex, right=of v2] (v3);
\coordinate[vertex, right=of v3] (v4);
\coordinate[above right=of v4,label=right:$f$] (f1);
\coordinate[below right=of v4,label=right:$f$] (f2);
\coordinate[above left =of v1,label=left :$e^+$] (e1);
\coordinate[below left =of v1,label=left :$e^-$] (e2);
\draw[fermion] (e1) -- (v1);
\draw[fermion] (v1) -- (e2);
\draw[photon] (v1) -- (v2) node[midway,above=0.1cm] {$Z$};
\draw[photon] (v3) -- (v4) node[midway,above=0.1cm] {$Z$};
\draw[fermion] (f1) -- (v4);
\draw[fermion] (v4) -- (f2);
% the next two lines behave fine, but don't have any labels
\semiloop[fermion]{v2}{v3}{0};
\semiloop[fermion]{v3}{v2}{180};
\end{tikzpicture}

\end{document}

If anybody can tell me what's going wrong here or can show me a more elegant way of drawing circles through two points while being able to use predefined line styles, I would be very happy.

Edit

Following an issue that has arisen from the accepted answer, this is the output of \listfiles

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
pgfplots.sty    2013/10/03 v1.9 Data Visualization (1.9)
    tikz.sty    2010/10/13 v2.10 (rcs-revision 1.76)
     pgf.sty    2008/01/15 v2.10 (rcs-revision 1.12)
  pgfrcs.sty    2010/10/25 v2.10 (rcs-revision 1.24)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v2.10 (rcs-revision 1.7)
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
graphics.sty    2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2010/04/23 v1.9 graphics configuration of TeX Live
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
  pgfsys.sty    2010/06/30 v2.10 (rcs-revision 1.37)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2008/07/18  (rcs-revision 1.7)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v2.10 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v2.10 (rcs-revision 1.1)
  pgffor.sty    2010/03/23 v2.10 (rcs-revision 1.18)
 pgfkeys.sty    
 pgfkeys.code.tex
  pgffor.code.tex
    tikz.code.tex
  xparse.sty    2013/12/31 v4634 L3 Experimental document command parser
   expl3.sty    2014/01/07 v4646 L3 Experimental code bundle wrapper
 l3names.sty    2014/01/04 v4640 L3 Namespace for primitives
l3bootstrap.sty    2014/01/04 v4640 L3 Experimental bootstrap code
    etex.sty    1998/03/26 v2.0 eTeX basic definition package (PEB)
l3basics.sty    2014/01/04 v4642 L3 Basic definitions
 l3expan.sty    2014/01/04 v4642 L3 Argument expansion
    l3tl.sty    2013/12/27 v4625 L3 Token lists
   l3seq.sty    2013/12/14 v4623 L3 Sequences and stacks
   l3int.sty    2013/08/02 v4583 L3 Integers
 l3quark.sty    2013/12/14 v4623 L3 Quarks
   l3prg.sty    2014/01/04 v4642 L3 Control structures
 l3clist.sty    2013/07/28 v4581 L3 Comma separated lists
 l3token.sty    2013/08/25 v4587 L3 Experimental token manipulation
  l3prop.sty    2013/12/14 v4623 L3 Property lists
   l3msg.sty    2013/07/28 v4581 L3 Messages
  l3file.sty    2013/10/13 v4596 L3 File and I/O operations
  l3skip.sty    2013/07/28 v4581 L3 Dimensions and skips
  l3keys.sty    2013/12/08 v4614 L3 Experimental key-value interfaces
    l3fp.sty    2014/01/04 v4642 L3 Floating points
   l3box.sty    2013/07/28 v4581 L3 Experimental boxes
l3coffins.sty    2013/12/14 v4624 L3 Coffin code layer
 l3color.sty    2012/08/29 v4156 L3 Experimental color support
l3luatex.sty    2013/07/28 v4581 L3 Experimental LuaTeX-specific functions
l3candidates.sty    2014/01/06 v4643 L3 Experimental additions to l3kernel
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
supp-pdf.mkii
pdftexcmds.sty    2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
epstopdf-base.sty    2010/02/09 v2.5 Base part for package epstopdf
  grfext.sty    2010/08/19 v1.1 Manage graphics extensions (HO)
kvdefinekeys.sty    2011/04/07 v1.3 Define keys (HO)
kvoptions.sty    2011/06/30 v3.11 Key value format for package options (HO)
kvsetkeys.sty    2012/04/25 v1.16 Key value parser (HO)
etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
 ***********

Best Answer

The node won't work because you have used ; in your \semiloop definition. Actually

\semiloop[fermion]{v2}{v3}{0}  node[midway,above=0.1cm] {$f$};

will expand to

\draw[fermion] let \p1 = ($(v3)-(v2)$) in (v3) arc (0:({0+180}):({0.5*veclen(\x1,\y1)});) node[midway,above=0.1cm] {$f$};

i.e., node without \ (\node). You can redefine the command like this with the help of xparse package:

\NewDocumentCommand\semiloop{O{black}mmmO{}O{above}}
{%
\draw[#1] let \p1 = ($(#3)-(#2)$) in (#3) arc (#4:({#4+180}):({0.5*veclen(\x1,\y1)})node[midway, #6] {#5};)
}
%% Syntax
%\semiloop[fermion][<draw options>]{<first node>}{<second node>}{<angle>}[<label>][<below, default: above>];

Full code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{xparse}

\usetikzlibrary{positioning,arrows,patterns}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}

\tikzset{
  photon/.style={decorate, decoration={snake}, draw=black},
  fermion/.style={draw=black, postaction={decorate},decoration={markings,mark=at position .55 with {\arrow{>}}}},
  vertex/.style={draw,shape=circle,fill=black,minimum size=3pt,inner sep=0pt},
}

\NewDocumentCommand\semiloop{O{black}mmmO{}O{above}}
{%
\draw[#1] let \p1 = ($(#3)-(#2)$) in (#3) arc (#4:({#4+180}):({0.5*veclen(\x1,\y1)})node[midway, #6] {#5};)
}
%% Syntax
%\semiloop[fermion][<draw options>]{<first node>}{<second node>}{<angle>}[<label>][<below, default: above>];


\begin{document}
\begin{tikzpicture}[node distance=1cm and 1cm]
\coordinate[vertex] (v1);
\coordinate[vertex, right=of v1] (v2);
\coordinate[vertex, right=of v2] (v3);
\coordinate[vertex, right=of v3] (v4);
\coordinate[above right=of v4,label=right:$f$] (f1);
\coordinate[below right=of v4,label=right:$f$] (f2);
\coordinate[above left =of v1,label=left :$e^+$] (e1);
\coordinate[below left =of v1,label=left :$e^-$] (e2);
\draw[fermion] (e1) -- (v1);
\draw[fermion] (v1) -- (e2);
\draw[photon] (v1) -- (v2) node[midway,above=0.1cm] {$Z$};
\draw[photon] (v3) -- (v4) node[midway,above=0.1cm] {$Z$};
\draw[fermion] (f1) -- (v4);
\draw[fermion] (v4) -- (f2);
% the next two lines are causing the mess
\semiloop[fermion]{v2}{v3}{0}[$f$];
\semiloop[fermion]{v3}{v2}{180}[$\bar{f}$][below];
\end{tikzpicture}

\begin{tikzpicture}[node distance=1cm and 1cm]
\coordinate[vertex] (v1);
\coordinate[vertex, right=of v1] (v2);
\coordinate[vertex, right=of v2] (v3);
\coordinate[vertex, right=of v3] (v4);
\coordinate[above right=of v4,label=right:$f$] (f1);
\coordinate[below right=of v4,label=right:$f$] (f2);
\coordinate[above left =of v1,label=left :$e^+$] (e1);
\coordinate[below left =of v1,label=left :$e^-$] (e2);
\draw[fermion] (e1) -- (v1);
\draw[fermion] (v1) -- (e2);
\draw[photon] (v1) -- (v2) node[midway,above=0.1cm] {$Z$};
\draw[photon] (v3) -- (v4) node[midway,above=0.1cm] {$Z$};
\draw[fermion] (f1) -- (v4);
\draw[fermion] (v4) -- (f2);
% the next two lines behave fine, but don't have any labels
\semiloop[fermion]{v2}{v3}{0};
\semiloop[fermion]{v3}{v2}{180};
\end{tikzpicture}

\end{document}

enter image description here

It looks like you need an update for your tex distribution. Add \listfiles and compare yours (you can find it in your log file at the end) with following:

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
pgfplots.sty    2013/10/03 v1.9 Data Visualization (1.9)
    tikz.sty    2013/12/13 v3.0.0 (rcs-revision 1.142)
     pgf.sty    2013/12/18 v3.0.0 (rcs-revision 1.14)
  pgfrcs.sty    2013/12/20 v3.0.0 (rcs-revision 1.28)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v3.0.0 (rcs-revision 1.7)
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
graphics.sty    2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2007/01/18 v1.5 graphics configuration of teTeX/TeXLive
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
  pgfsys.sty    2013/11/30 v3.0.0 (rcs-revision 1.47)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2013/09/09  (rcs-revision 1.9)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v3.0.0 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v3.0.0 (rcs-revision 1.1)
  pgffor.sty    2013/12/13 v3.0.0 (rcs-revision 1.25)
 pgfkeys.sty    
 pgfkeys.code.tex
 pgfmath.sty    
 pgfmath.code.tex
  pgffor.code.tex
    tikz.code.tex
  xparse.sty    2013/12/31 v4634 L3 Experimental document command parser
   expl3.sty    2014/01/07 v4646 L3 Experimental code bundle wrapper
 l3names.sty    2014/01/04 v4640 L3 Namespace for primitives
l3bootstrap.sty    2014/01/04 v4640 L3 Experimental bootstrap code
    etex.sty    1998/03/26 v2.0 eTeX basic definition package (PEB)
l3basics.sty    2014/01/04 v4642 L3 Basic definitions
 l3expan.sty    2014/01/04 v4642 L3 Argument expansion
    l3tl.sty    2013/12/27 v4625 L3 Token lists
   l3seq.sty    2013/12/14 v4623 L3 Sequences and stacks
   l3int.sty    2013/08/02 v4583 L3 Integers
 l3quark.sty    2013/12/14 v4623 L3 Quarks
   l3prg.sty    2014/01/04 v4642 L3 Control structures
 l3clist.sty    2013/07/28 v4581 L3 Comma separated lists
 l3token.sty    2013/08/25 v4587 L3 Experimental token manipulation
  l3prop.sty    2013/12/14 v4623 L3 Property lists
   l3msg.sty    2013/07/28 v4581 L3 Messages
  l3file.sty    2013/10/13 v4596 L3 File and I/O operations
  l3skip.sty    2013/07/28 v4581 L3 Dimensions and skips
  l3keys.sty    2013/12/08 v4614 L3 Experimental key-value interfaces
    l3fp.sty    2014/01/04 v4642 L3 Floating points
   l3box.sty    2013/07/28 v4581 L3 Experimental boxes
l3coffins.sty    2013/12/14 v4624 L3 Coffin code layer
 l3color.sty    2012/08/29 v4156 L3 Experimental color support
l3luatex.sty    2013/07/28 v4581 L3 Experimental LuaTeX-specific functions
l3candidates.sty    2014/01/06 v4643 L3 Experimental additions to l3kernel
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
supp-pdf.mkii
 ***********
Related Question