# Reference to listing not working [duplicate]

I have the following code:

\documentclass{article}

\usepackage{hyperref}
\usepackage{minted}

\begin{document}

A nice paper has the following things:

\begin{listing}
data RCPoint = RCPoint {
_rcp_si :: Integer
}
\end{minted}
\label{ch1}
\caption{The RCPOINT Listing}
\end{listing}

$$\label{eq:eqn1} x^2 - 2 x$$

I like my equation in \ref{eq:eqn1}.

I also like my code in Listing \ref{ch1}.

\end{document}


which compiles normally, to produces::

The reference to the listing in the final line is missing. I get a warning for the final line of output:

(/usr/share/texmf-dist/tex/latex/amsfonts/umsb.fd)

Package hyperref Warning: Suppressing empty link on input line 55.

[1] (./nice-org-paper.aux) )
Output written on nice-org-paper.pdf (1 page).
Transcript written on nice-org-paper.log.


How do I get the reference to work?

• You forget your question, in my point of view – user31729 Mar 9 '14 at 9:12
• I see who I could have clarified that. – stevejb Mar 9 '14 at 20:09
• I see now why it is marked as duplicate, but I realize that I did not even know the question to ask. I did not know about the ordering issue. – stevejb Mar 9 '14 at 20:10
• @stevejb: You did have the question in the post, but it was only in the alt text of the image: ![The question was in these square brackets](image link). This shows up in screen-reading software, or if the image can’t be loaded, but some (most?) people won’t see that. You can find out more on the Markdown help page. – alexwlchan Mar 9 '14 at 20:23

The problem is that your \label occurs before your \caption. You should always put your \caption before \label.

The number for the listing gets assigned by \caption, so you should swap the two lines

\label{ch1}
\caption{The RCPOINT Listing}


to

\caption{The RCPOINT Listing}
\label{ch1}


and then your code should produce what you expect.

You can find out more about this problem in egreg’s answer to Why does an environment's label have to appear after the caption?

• What's the design choice behind \caption always coming before \label? – rafaelvalle Feb 24 '17 at 6:22