# Why doesn't my code draw the Feynman diagram?

\documentclass{report}
\usepackage[english,russian]{babel}
\usepackage[utf8]{inputenc}

\begin{document}

\usepackage{tikz}

\begin{tikzpicture}

\feynmandiagram [horizontal=a to b] {
i1 -- [fermion] a -- [fermion] i2,
a -- [photon] b,
f1 -- [fermion] b -- [fermion] f2,
}

\end{tikzpicture}

\end{document}


The above is my code. The program stops at \usepackage{tikz}. What's the problem here?

• you need to put \usepackage{tikz} before \begin{document} and presumably also load the tikz-feynman package. – user121799 May 13 '17 at 20:59
• latex doesn't just "stop" it shows the line with \usepackage and shows the error message ! LaTeX Error: Can be used only in preamble. which by latex standards is fairly accurate message. – David Carlisle May 13 '17 at 21:04
• @marmot I did that, now I get a different error. Undefined control sequence \feynmandiagram[horizontal=a to b] { – Danny Nelson May 13 '17 at 21:06
• As @marmot said (and the answer states), you need tikz-feynman. – Torbjørn T. May 13 '17 at 21:07

Packages can only be loaded in the preamble, i.e. all lines with \usepackage must be before \begin{document}.

In your case, \usepackage{tikz} should thus be with the other \usepackages. Furthermore, since you want to draw a Feynman diagram, you should load the tikz-feynman package instead of plain tikz, since otherwise the \feynmandiagram command is not defined. Also, a semicolon needs to be added after \feynmandiagram.

With these three things changed, your code becomes:

\documentclass{report}
\usepackage[english,russian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz-feynman}

\begin{document}

\begin{tikzpicture}

\feynmandiagram [horizontal=a to b] {
i1 -- [fermion] a -- [fermion] i2,
a -- [photon] b,
f1 -- [fermion] b -- [fermion] f2,
};

\end{tikzpicture}

\end{document}

• when I run this the code stops at l. 16 \end{tikzpicture} followed by ? . do you know how to fix this? – Danny Nelson May 13 '17 at 21:09
• When LaTeX gives errors, you can just press enter repeatedly to still finish compiling the document. The error is given because a semicolon was missing, this is fixed now in the answer above. – cp.fe.cp May 13 '17 at 21:13