# How to \label and get recognised?

This issue actually frustrates me for a while, but prior for finding the solution, I would like to know why this happens. In a nutshell: My TeXStudio does recognise ALL my \label, but the output does not recognise some labels.

## Problem

I am writing my notes on Physics, however, sometimes I got warnings of:

LaTeX Warning: Reference eq:pointcd' on page 196 undefined on input line 985.


This is just for example, I have other examples in the document that does not have \label recognised. There are also some weird situation where I have the same format of putting \label, but got recognised by the compiler.

## MWE

\documentclass[12pt,a4paper,headinclude,openright]{scrbook}
\usepackage{amsmath}
\usepackage{amssymb}
% ------------here comes the problem part------------
\usepackage[listings,theorems,skins,breakable]{tcolorbox}
\tcbuselibrary{breakable}
%======================== Ch.4 boxes =========================
\tcbset{exercisestyle04/.style={arc=0.5mm,coltitle=black,fonttitle=\sffamily\bfseries,colback=color04!50!,colframe=color04}}

\newtcolorbox[auto counter,number within=chapter,list inside=eg]%
{exercise04}[2][]{exercisestyle04,
title={Exercise ~\thetcbcounter: #2}}

\tcbuselibrary{theorems}
\usepackage[amsthm,thmmarks,hyperref]{ntheorem}
% theorem
\usepackage{regexpatch}
\makeatletter
\xpatchcmd*{\thm@@thmline@name}{2.3em}{5em}{}{}
\xpatchcmd*{\thm@@thmline@noname}{2.3em}{5em}{}{}
\makeatother
\makeatletter
\renewtheoremstyle{plain}%
{\item[\hskip\labelsep \theorem@headerfont ##1\ ##2\ \sffamily{##3}  \theorem@separator]}
\makeatother
\theoremstyle{plain}
\theorembodyfont{\normalfont\sffamily}
\newtheorem{theorem}{Theorem}[section]
% ------------here ends the problem part------------
\begin{document}
$$\rho\left(\mathbf{r}\right)\;=\;\sum_{k=1}^{N}\: q_k \delta\left(\mathbf{r}\;-\;\mathbf{r}_k\right) \label{eq:pointcd}$$
However, one should alert for the unphysical divergence in other quantities associated with \eqref{eq:pointcd}.
\end{document}


I am not specifying you of how to answer this question, this section is just my suspects on the issue. For example,

1. The pdf compiler is faulty? (I use Acrobat XI and with PdfLaTeX as compiler and using DVI -> PS -> PDF chain in TeXStudio)
2. My compiling procedures has done wrongly?
3. My format is done wrong (although I fairly think so because I changed my format according to many sources but no luck).
4. TeXStudio is faulty? (Just a suspect, but did happened with other IDEs before and I am using ver. 2.8.0)
5. I haven't read the manual carefully? (smile)

It turns out some code has obstructed the PdfLaTeX to recognise some \label.

## Update

While I was omitting the parts that I have some idea why it happens, however, it is neither of the five above.

1. While I deleted some of the \frontmatter, by delete a section (see MWE above) become fine to recognise the referencing.
2. I suspect the part is obstructing the compiler to recognise some \label.

## Solution

See Mico's answer, but I would like to append that it was actually because of

5. I haven't read the manual carefully

-
Do you run pdflatex twice on your document? – Christian Hupfer Aug 24 '14 at 9:00
Just out of curiosity: Since you have pdflatex, why don't you compile directly from tex to pdf? Put differently, why use the old, roundabout way via dvi and ps? – Mico Aug 24 '14 at 9:01
@SHY.John If you want help with a document where the cross references do not resolve, make an example of that problem not an example of a perfectly standard document that behaves as expected. – David Carlisle Aug 24 '14 at 9:16
No, it's better if you make an example small enough to post here, start with a copy of your original document and delete everything you can delete while still demonstrating th eproblem – David Carlisle Aug 24 '14 at 9:19
I do not believe this question should have been closed as a duplicate -- the linked posting certainly doesn't provide answers to what went wrong in this question. For the sake of completeness: The OP was experiencing problems because he/she had failed to specify an option when loading the package ntheorem, rather than for any of the reasons discussed in the posting that this is supposed to be a duplicate of. – Mico Aug 24 '14 at 13:02

Thanks for posting some code that produces the problem behavior you're looking to solve.

The problematic statement in your preamble turns out to be

\usepackage[amsthm,thmmarks,hyperref]{ntheorem}


and, more specifically, the option thmmarks. Remove this option, and the cross-referencing problem goes away.

Alternatively, since you load the amsmath package as well, simply add the option amsmath and the thmmarks option doesn't cause problems either. (Incidentally, this fix is mentioned explicitly in section 3.2.1 of the user guide of the ntheorem package.)

\usepackage[amsthm,amsmath,thmmarks,hyperref]{ntheorem}

-
One can also add the option amsmath, see 3.2. in the documentation of ntheorem. – Ulrike Fischer Aug 24 '14 at 10:17
@UlrikeFischer - thanks. I've added this observation to my answer. :-) – Mico Aug 24 '14 at 10:17
+1 you just beat me to it:-) – David Carlisle Aug 24 '14 at 10:19
I was thinking to add a thmmarks` at the end of theorems but ended up on changing the font of theorems instead. So the option become useless. When I omit the option, it recovered from conflict and no other aftermaths. You are going to receive a "tick" instead of a "thank you", sorry. – SHY.John Aug 24 '14 at 10:21
@DavidCarlisle Although you did not get a "tick", you taught me that experiment first rather just throw out a MWE is a good practice. Thank you. – SHY.John Aug 24 '14 at 10:24