6

Currently, within my thesis, I am numberings theorems (and remarks, etc.) with a global counter. This makes it difficult in the printed version to find the theorem if a reference is too far away from the label. Is there any simple way I can modify my reference commands to add a "on page XXX" in case of having lets say a 3 page gap between ref and label? Do you have any suggestions or experiences?

As far as I understood the documentations, packages such as varioref only support a one-page-diff condition; but even this I was not able to activate.

Minimal working example:

\documentclass{scrreprt}

\usepackage{blindtext} % lore ipsum...
\usepackage{amsthm}
\newtheorem{thm}{Theorem}

\begin{document}
  % example theorem:
  \begin{thm}\label{thm:foo}
    The future belongs to those who believe in their dreams.
  \end{thm}
  % generate more than 3 pages of text:
  \blindtext[20] 
  % reference:
  Reference to theorem \ref{thm:foo}. Here an automatic
  ``on page \pageref{thm:foo}'' would be nice.
\end{document}

MWE for Mico (see discussion below):

\documentclass{scrreprt}
\usepackage{blindtext} % lore ipsum...
\usepackage{amsmath}
\usepackage{ntheorem}
\usepackage{varioref}
\usepackage{hyperref}
\newtheorem{thm}{Theorem}

\begin{document}
  % theorem foo
  \begin{thm}\label{thm:foo}
      The future belongs to those who believe in their dreams, see (\vref{eqn:bar})
  \end{thm}

  % some pages of text...
  \blindtext[15]

  % cross-reference to theorem within equation which is labeled itself (same works for figures)
  \begin{align}\label{eqn:bar}
    a+b=c\quad\text{(cf. Theorem \vref{thm:foo})}
  \end{align}
\end{document}

Leads to

  • ! Package amsmath Error: Multiple \label's: label 'eqn:bar' will be lost.
  • ! Package amsmath Error: Multiple \label's: label '4@xvr' will be lost.
  • several further warnings...

(Partial) Solution (based on Christians idea) After playing around with Christians suggestion I came up with a solution (it SEEMS to work). I have to store the manipulated \ref command to \vref before hyperref is loaded. Then, I can use \ref as standard ref and \vref as optional "on page" version. For some strange reason this only works together with babel. It is important to add a "%" in order to avoid wrong spacing.

\documentclass{scrreprt}
\usepackage{blindtext} % lore ipsum...
\usepackage{amsmath}    
\usepackage[english]{babel} % < for some strange reason HAS to be loaded
%% --- ref manipulations as suggested by Christian Hupfer:
\makeatletter
\let\latex@@ref\ref

\newcounter{pagegaptreshhold}
\setcounter{pagegaptreshhold}{2}

\newcounter{tmpcntr}

\renewcommand{\ref}[1]{%
  \setcounter{tmpcntr}{\value{page}}%
  \addtocounter{tmpcntr}{-\getpagerefnumber{#1}}%
  \ifnum\value{tmpcntr} < 0 %
  \setcounter{tmpcntr}{\numexpr -1*\value{tmpcntr}}%
  \fi%
  \ifnum\value{tmpcntr} > \value{pagegaptreshhold}
  \latex@@ref{#1} on page \pageref{#1}%
  \else%
  \latex@@ref{#1}%
  \fi%
}
\makeatother
%% --- end cmhughes suggestion ---
\let\vref\ref
\usepackage{hyperref}

\newtheorem{thm}{Theorem}
\begin{document}
  % theorem foo
  \begin{thm}\label{thm:foo}
      The future belongs to those who believe in their dreams, see (\vref{eqn:bar})
  \end{thm}

  % some pages of text...
  \blindtext[30]

  % cross-reference to theorem within equation which is labeled itself (same works for figures)
  \begin{align}\label{eqn:bar}
    a+b=c\quad\text{(cf. Theorem \vref{thm:foo})}
  \end{align}
  In particular, we have now the choice between 
  \begin{itemize}
    \item ``on page''-if-too-far-away-style: Theorem \vref{thm:foo}
    \item classic style: Theorem \ref{thm:foo}
  \end{itemize}
\end{document}
matheburg
  • 1,279
  • A working example would be nice anyway –  Oct 31 '15 at 13:32
  • @ChristianHupfer Why did you delete your solution? I am still interested in that. – matheburg Oct 31 '15 at 14:25
  • Because it does not work with hyperref –  Oct 31 '15 at 14:39
  • But it provides the option to adapt the page difference, even if I cannot use it ;-) – matheburg Nov 01 '15 at 07:57
  • Thanks for providing a second MWE. Do check out the posting Lost labels using intertext with varioref and hyperref. The recommended solution is to load the cleveref package as well (after hyperref), as doing so fixes a minor but long-standing incompatibility between varioref and hyperref. – Mico Nov 01 '15 at 18:29
  • @Mico Thanks again! I had already seen this solution, but cleveref changes so many things (adding extra "theorem" etc.) and throws further warnings ("destination with the same identifier ... duplicate ignored")... :-/ Can't I use just "the fix" from cleveref without introducing all the other inconveniences? – matheburg Nov 02 '15 at 06:09
  • @matheburg - I'm afraid I'm not sure I understand what you mean. cleveref doesn't throw away warnings and error messages -- it actually fixes the problems that give rise to the warnings and error messages. If you need both varioref and hyperref, loading cleveref as well is -- for now at least -- the way to go. I use the macros of the cleveref package extensively in my own papers and can thus wholeheartedly recommend its use. – Mico Nov 02 '15 at 06:22
  • @Mico Oh, that's a misunderstanding: cleveref does not "throw away" errors and warnings, but it "throws new warnings"; but my major problem is that it introduces "theorem" before a ref of kind "thm:foo"; do you know how to deactivate this behaviour? – matheburg Nov 02 '15 at 07:34
  • @matheburg: I've undeleted my solution and try to improve it, but at the moment, I don't recommend its usage –  Nov 05 '15 at 09:20

2 Answers2

7

(Updated answer after the OP posted a second, more elaborate MWE.)

The varioref package implements the following decision rule with regard to generating page call-outs:

  • If the item being cross-referenced and the cross-reference are on the same page, no page-related affix is produced;

  • If the item being cross-referenced and the cross-reference are on adjoining pages, either "on the previous page" or "on the following page" is affixed to the cross-reference.

  • If the item being cross-referenced and the cross-reference itself are two or more pages apart, a "on page x" affix is generated.

If you know in advance that there will be several pages between the cross-reference and the item being cross-referenced, you may also use the instruction \fullref, which is simpler (uses fewer machine cycles...) than \vref.

If you use both varioref and hyperref, you should load the cleveref package as well: Doing so fixes a (mild) conflict between the former two packages. If you load cleveref, the items' names ("equation", "theorem", etc) will be prefixed automatically to the cross-reference call-outs.

\documentclass{scrreprt}
\usepackage{blindtext}    % lorem ipsum...
\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}

\usepackage{varioref}     % for \vref and \fullref macros
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\newtheorem{thm}{Theorem}

\begin{document}
\setcounter{chapter}{1} % just for this example

% theorem foo, with cross-reference to an equation
\begin{thm}\label{thm:foo}
The future belongs to those who believe in their dreams (see \vref{eqn:bar}). 
\end{thm}

% some pages of text...
\blindtext[15]

% equation bar, with cross-reference to a theorem 
\begin{align}\label{eqn:bar}
    a+b=c\quad\text{(cf.\ \fullref{thm:foo})}
\end{align}
\end{document}
Mico
  • 506,678
  • Thanks for that answer. Unfortunately, there are (at least) three issues: (1) Whenever I use a ref within an equation or figure which is labeled it throws errors and warnings; (2) I cannot change the behaviour (e.g. how much pages diff lead to the pageref); (3) I cannot use my old Lemma 1a) reference anymore..... (1), ofc., is the major problem! – matheburg Nov 01 '15 at 10:56
  • @matheburg - Thanks for these additonal explanations. Just to make sure I understand the issue in point (1): You're trying to create cross-references from equation and figure environments to other, labelled items? Please confirm. – Mico Nov 01 '15 at 12:09
  • Confirmed: little dummy example: \begin{align}\label{eqn:foo}a + b = c\quad\text(cf. \vref{eqn:bar})}\end{align} The same for complex figures which include references to lemmas, equations, etc. – matheburg Nov 01 '15 at 14:03
  • @matheburg - Your code snippet contains a syntax error. After I change the code to \text{cf. \vref{eqn:bar}}, I experience no problems with \vref. – Mico Nov 01 '15 at 14:41
  • Sorry for that, I did not properly check the syntax. I think the problem occurs in the combination with hyperref; I have added an MWE to my question :-) Many thanks for your efforts! – matheburg Nov 01 '15 at 17:48
1
\documentclass{article}

\usepackage{refcount}
\usepackage{blindtext}

\makeatletter
\let\latex@@ref\ref

\newcounter{pagegaptreshhold}
\setcounter{pagegaptreshhold}{2}

\newcounter{tmpcntr}

\renewcommand{\ref}[1]{%
  \setcounter{tmpcntr}{\value{page}}%
  \addtocounter{tmpcntr}{-\getpagerefnumber{#1}}%
  \ifnum\value{tmpcntr} < 0 %
  \setcounter{tmpcntr}{\numexpr -1*\value{tmpcntr}}%
  \fi%
  \ifnum\value{tmpcntr} > \value{pagegaptreshhold}
  \latex@@ref{#1} on page \pageref{#1}%
  \else%
  \latex@@ref{#1}%
  \fi%
}
\makeatother

\begin{document}

\section{first} 
In\ref{section::second} we will see that...
\blindtext[40]
\section{second} \label{section::second}
Here: \ref{section::second}

\end{document}
  • Sorry, but \setcounter{tmpcntr}{\value{page}} is not reliable: it can be off by one, because the \ref could appear in a part of the paragraph that's then moved on the next page. – egreg Oct 31 '15 at 13:47
  • @egreg: Yes, unfortunately :-( –  Oct 31 '15 at 13:51
  • Nevertheless, it seems to be nice work! Thanks! Before I try: Do you think this is compatible with hyperref? – matheburg Oct 31 '15 at 13:54
  • @matheburg: It should, but wait until I improved the code. It's a quick hack at the moment –  Oct 31 '15 at 13:55
  • @matheburg: Well, hyperref breaks this :-( –  Oct 31 '15 at 14:12
  • @ChristianHupfer For me it seems to work somehow, see post above. For some reason I have several small issues remaining: babel has to be loaded (no problem); the 2-page difference does not seem to work; I had to add a "%" to correct the spacing. – matheburg Nov 08 '15 at 10:42
  • @matheburg: I will look again –  Nov 08 '15 at 10:53
  • @ChristianHupfer For some reason in my thesis the 2-page-diff seems to work; maybe another strange package interaction? – matheburg Nov 08 '15 at 11:05
  • Finally, this did the job for me (see above). This is why I am accepting this answer. – matheburg Jan 13 '16 at 08:21