4

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 \theorem@separator]}%
{\item[\hskip\labelsep \theorem@headerfont ##1\ ##2\ \sffamily{##3}  \theorem@separator]}
\makeatother
\theoremstyle{plain}
\theorembodyfont{\normalfont\sffamily}
\theoremheaderfont{\bfseries}
\newtheorem{theorem}{Theorem}[section]
% ------------here ends the problem part------------
\begin{document}
\begin{equation}
\rho\left(\mathbf{r}\right)\;=\;\sum_{k=1}^{N}\: q_k \delta\left(\mathbf{r}\;-\;\mathbf{r}_k\right)
\label{eq:pointcd}
\end{equation}
However, one should alert for the unphysical divergence in other quantities associated with \eqref{eq:pointcd}.
\end{document}

In your answer...

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
SHY.John
  • 427
  • Do you run pdflatex twice on your document? –  Aug 24 '14 at 09: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 09:01
  • @ChristianHupfer I always do that, even today still remains as the same situation. – SHY.John Aug 24 '14 at 09:01
  • @Mico What is the difference of different compiling paths? – SHY.John Aug 24 '14 at 09:02
  • Your example isn't an example of the problem so doesn't really help. (1) is inconsistent, if you are using pdflatex (almost certainly) you get pdf directly not dvi-ps. So most likely reasons are (2) and (5) but without an example showing what you did it's hard to say what you did wrong. – David Carlisle Aug 24 '14 at 09:03
  • For one, direct compilation to pdf is quicker than the roundabout way. Second, direct compilation works a whole lot better if you want to use packages such as hyperref. Separately: If I augment your code to insert a cross-reference, I don't experience any problems. Did you maybe have a typo in the \ref statement? – Mico Aug 24 '14 at 09:05
  • @DavidCarlisle see updated version, this is one of the corrupted example in my document, sorry did not specify. – SHY.John Aug 24 '14 at 09:09
  • @Mico see updated version, the corresponding \ref statement is there now. – SHY.John Aug 24 '14 at 09:10
  • There is no warning on the second run, which is always needed, as Christian indicated in the first comment – David Carlisle Aug 24 '14 at 09:11
  • so the answer appears to be (5) the manual (and the terminal output of the first run) should tell you that you need to run latex twice to resolve cross references. – David Carlisle Aug 24 '14 at 09:12
  • Running pdflatex to generate pdf output directly, I get no errors or warnings if I run your augmented code twice. Do you maybe have a setting that deletes all auxiliary files immediately after each run? – Mico Aug 24 '14 at 09:13
  • @DavidCarlisle I always compile twice, so it is weird that some did show up the cross-reference but the one above (for example) did not. – SHY.John Aug 24 '14 at 09:13
  • I mean in the long document I have written. – SHY.John Aug 24 '14 at 09:14
  • 3
    @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 09:16
  • @DavidCarlisle Can I specify the location of document in my Google Drive? Would that be better? – SHY.John Aug 24 '14 at 09:17
  • 3
    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 09:19
  • Please allow me to produce the problematic MWE, I am sorry to delay on replying everyone. – SHY.John Aug 24 '14 at 09:24
  • @DavidCarlisle Please see update, this is what I found from the long document. – SHY.John Aug 24 '14 at 09:50
  • thats a better example:-) will look.... – David Carlisle Aug 24 '14 at 10:04
  • 3
    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

1 Answers1

10

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}
Mico
  • 506,678
  • 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
  • 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
  • 1
    @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
  • @SHY.John :-)... – David Carlisle Aug 24 '14 at 10:42