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.
- 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.
- 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.
- 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
- Avoiding page breaks shortly after section/subsection headings
- 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.
\clearpage
after the commented out\ldots
corrects the number.clearpage
) manually cannot be accepted as solution :( LaTeX has to do that work for me.thmtools
orhyperref
, 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.