I am trying to use the TiKZ-Feynman package to create Feynman diagrams for a report. However when I try to refer to them by their figure number, it is not generating the number correctly in some cases. It may be something to do with the section or subsection in which the diagram is contained. I am using TiKZ-Feynman 1.1.0 and compiling with LuaLaTeX on sharelatex.
\documentclass[a4paper]{article}
% packages
\usepackage[margin=2.75 cm]{geometry} % margins
\usepackage{tikz}
\usepackage[compat=1.1.0]{tikz-feynman}
\begin{document}
\section{Section 1}
This is the first section, including correctly-numbered figure \ref{fig:first}.
% Figure 1
\begin{figure}[h]
\label{fig:first}
\centering
\includegraphics[width=0.8\textwidth]{figures/firstfigure.png}
\caption{Caption correctly numbered.}
\end{figure}
\section{Section 2}
\subsection{Subsection 2.1}
Reference to a tikz-feynman diagram with incorrect figure number \ref{fig:second}.
\begin{figure}[h]
\centering
\label{fig:second}
\feynmandiagram [horizontal=a to b] {
i1 [particle=\(e^{-}\)] -- [fermion] a -- [fermion] i2 [particle=\(e^{+}\)],
a -- [photon, edge label=\(\gamma\), momentum'=\(k\)] b,
f1 [particle=\(\mu^{+}\)] -- [fermion] b -- [fermion] f2 [particle=\(\mu^{-}\)],
};
\caption{Caption gives the correct numbering}
\end{figure}
\end{document}
Specifically, it labels the first as 'Figure 1' both in the text and in the caption. For the second, however, the caption generates correctly ('Figure 2') while the reference in the text says 'Figure 2.1'.
\labelalways has to come after or within the\caption. – Torbjørn T. Aug 31 '17 at 10:34tikz-feynmanreally (remove that diagram as and the image in the firstfigureand try again). The first cross-reference isn't actually correct, it just looks correct because it likely picks up section 1, whereas the second figure picks up subsection 2.1. – Torbjørn T. Aug 31 '17 at 10:37