11

I don't know if the following issue is related to any other question already asked. My issue and minimal looks very simple.

\documentclass{book}

\usepackage{hyperref}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem{theorem}

\begin{document}

\ldots \\[18cm] \ldots

% Insert another \ldots here.... dots remain on previous page, page number for theorem is corrent
% \ldots

\begin{theorem}
\label{th:1}
\ldots
\end{theorem}

Page of theorem: \pageref{th:1}.

\end{document}

As you will see, \pageref references to the wrong page (1 instead of 2). If including the commented \ldots, the reference is correct. Maybe some expert knowledge will help me understanding what's going on here.

Thanks in advance!

8
  • Try re-runing it one more time. The page references are stored in auxiliary files that are read upon a subsequent run and hence if things change may require an additional run. Commented Dec 29, 2012 at 2:32
  • Putting \clearpage after the commented out \ldots corrects the number.
    – user11232
    Commented Dec 29, 2012 at 2:41
  • I expected that I don't have to comment that my doc has 400+ pages and too many theorems (and other environments), so putting any formatting hints (clearpage) manually cannot be accepted as solution :( LaTeX has to do that work for me. Commented Dec 29, 2012 at 3:38
  • Re-running does not help (I know that general workflow and already tried before) Commented Dec 29, 2012 at 3:39
  • 2
    I can reproduce the problem and it has nothing to do with thmtools or hyperref, just the ordinary \newtheorem provided by LaTeX. It just seems that you're the first one using \pageref for theorems. This definitely is a misfeature that happens when a theorem starts when some space is available on the page, but at page breaking TeX decides to move the theorem to the next page. It shouldn't happen.
    – egreg
    Commented Dec 29, 2012 at 10:38

2 Answers 2

7

This shouldn't happen. However it seems to happen and it's definitely a misfeature.

How to remedy to it? Don't type the label just next to \begin{theorem}, but after the first word in the statement. I know this is a bad workaround.

\documentclass{book}

\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem{theorem}
\usepackage{hyperref}

\begin{document}

\ldots \\[18cm] \ldots

\begin{theorem}
Text\label{th:1} of the theorem
\end{theorem}

Page of theorem: \pageref{th:1}.

\end{document}

enter image description here

5
  • 1
    Or you can write \begin{theorem}\hspace*{0pt}\label{th:1} Text... Commented Dec 29, 2012 at 12:01
  • @AndrewSwann Without a space before "Text", I guess.
    – egreg
    Commented Dec 29, 2012 at 12:02
  • Your are right, there should be no space before "Text".. Commented Dec 29, 2012 at 12:08
  • A workaround, but still a better idea than manual layouting. Thank you! Commented Dec 29, 2012 at 21:58
  • There is this thread in the LaTeX bugs repository where something of this kind is touched.
    – egreg
    Commented Dec 29, 2012 at 22:14
2

The situation (what's happenin with LaTeX)

While LaTeX is deciding where the best place is to make a page break, your reference to the page gets set.

LaTeX usually reads through a goodly portion of what will fall on the next page before deciding where the page break should be set. But this means that the tokens, which you imagine belong to the next page, are being read and expanded before the expanded text/formatting is put on the page. In essence, the token for referencing is read earlier than the making of the page break.

This process results in optimal page breaks but occasionally not so good page references.

The good news is that this usually only applies to a pageref that occurs close to the top of the page (close enough that it gets considered for the previous page when the reference is written the to aux file). To get around this you force a page break.

While this may seem suboptimal, it is something you can correct when your document is in its final stages and you're certain of where the page breaks are going to occur.

I have too many potential page refs to manually fix on my own

This I'll have to think about. But as I see it there a few ways to finesse LaTeX into assigning the page ref better.

  1. You could provide a wrap-around for \label which checks how much space is left on the page and forces a pagebreak before the end of the page. I wouldn't recommend this merely to get page numbering references correct; you risk breaking a lot of other features and preempting more optimal solutions.
  2. You could dissect the LaTeX code for \label and do some tricks like when nailing down the correct page number for chapters and sections for the TOC. This requires using \noexpand and \save. I'm not sure I'm up to writing something like this.
  3. Learn better about how TeX and LaTeX make decisions about pages. You need to convince LaTeX that a good page break occurs before the theorem. This you can do as an author. For example, in the situation you outlined above, you could add one more line \vspace{\fill}\par\hspace*{\fill} before your theorem. That's enough to trick LaTeX into thinking it's approach a good point for a page break. Clearly, this is not what you want to do before every paragraph.

I would go with approach 3. Initially you might not like it. But, if you provided a MWE which closer resembles the context in which you believe LaTeX is waiting too long to decide where the page break should fall, I (or others) could point out how better to write your code to work around the issue.

Try running this version of your document:

\documentclass{book}
\usepackage[showframe,textheight=550pt,textwidth=345pt]{geometry}
\usepackage{hyperref}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem{theorem}
\usepackage{lipsum}
\usepackage{blindtext}
\begin{document}

\Blindtext
\Blindtext
\Blindtext
\Blindtext
\Blindtext

\begin{theorem}
\label{th:1}
\ldots
\end{theorem}

Page of theorem: \pageref{th:1}.

\end{document}

This doesn't entirely look like your document, but I've set up the textwidth and textheight to match what you were doing. So, the decisions about how and when to make page breaks should be the same. Notice that on page 6 there's a little bit of space left at the bottom of the page. This is your culprit. LaTeX sees this and decides that there's space left on the page to get filled. It's not until after it's read your \label that it decides it's just got to live with that white space.

Other pages on this site you might want to look at are

  1. Avoiding page breaks shortly after section/subsection headings
  2. Can I set an adjustable line spacing to avoid orphans and widows?

I'm sure that @DavidCarlisle (amongst others) will have some better suggestions than I when he sees this.

1
  • According to your last sentence the usual (or only?) solution to this problem is layouting manually? Commented Dec 29, 2012 at 3:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .