0

I have the following code:

\documentclass{article}

\usepackage{hyperref}
\usepackage{minted}

\begin{document}

A nice paper has the following things:

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

\begin{equation}
  \label{eq:eqn1}
  x^2 - 2 x
\end{equation}

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

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

\end{document}

which compiles normally, to produces::

PDF - Notice no listing number

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?

alexwlchan
  • 5,417
stevejb
  • 453
  • You forget your question, in my point of view –  Mar 09 '14 at 09:12
  • I see who I could have clarified that. – stevejb Mar 09 '14 at 20:09
  • 1
    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 09 '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 09 '14 at 20:23

1 Answers1

3

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?

alexwlchan
  • 5,417