6

How can I obtain information about the location of a label relative to other text?

  • Whether the label is on a previous page or the current page (i.e. appears in some previous lines, but on the same page).
  • If the label is on a previous page; how many pages prior? E.g. 1 page before, 5 pages before.

It would be helpful to obtain some value, such as 0 for current page, 1 for the previous page, 2 for two pages previous, as that could be placed into an if-then.

1
  • Would you be able to supply a use case for your conditioning in the form of an example, even if it does not compile? The reason being that two different answers have been given using two different approaches, possibly due to two different questions contained in your post.
    – Werner
    Nov 22 '11 at 4:00
3

This, perhaps is also a partial answer, since it addresses your first bullet.

The varioref package provides \vref and \vpageref that "decorates" a page reference, depending on its location relative to the reference. Here's an example:

enter image description here

\documentclass{article}
\usepackage{varioref}% http://ctan.org/pkg/varioref
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\begin{document}
\section{First section} \lipsum[1] \label{sec:first} \lipsum[2-3]
\section{Second section} \lipsum[4-5] \label{sec:second} \lipsum[6-8]
\section{Third section} \lipsum[9-12] \label{sec:third} \lipsum[13]
\section{Fourth section} \label{sec:fourth} \lipsum[14]
\section{Last section} \label{sec:last} \lipsum[15-17] \label{doc:end}
See the reference \vpageref{sec:last} or \vpageref{sec:second}.
\end{document}
2

Here is a partial solution: use \pageref to get the page number of where the \label was defined.

enter image description here

\documentclass{book}
\usepackage{lipsum}

\newcounter{pagedelta}
\newcommand{\pagerefdelta}[1]{%
    \setcounter{pagedelta}{\the\value{page}}%
    \addtocounter{pagedelta}{-\pageref{#1}}%
    \the\value{pagedelta}%
}%

\newcommand{\ShowRef}[1]{Section #1 is on Page~\pageref{#1}\par}%
\begin{document}

\chapter{One}
\section{One-One}\label{One-One}
\lipsum[1-4]\label{One-Two}

\chapter{Two}
\section{Two-One}\label{Two-One}
\section{Two-Two}\label{Two-Two}
\section{Two-Three}\label{Two-Three}

\chapter{Three}
\section{Three-One}\label{Three-One}
\lipsum[1-9]

\newpage
\ShowRef{One-One}
\ShowRef{One-Two}
\ShowRef{Two-One}
\ShowRef{Two-Two}
\ShowRef{Two-Three}
\ShowRef{Three-One}
\end{document}
3
  • 2
    @cmhughes: It gives a solution for the second bullet point.
    – Caramdir
    Nov 22 '11 at 3:45
  • It does however have problems when TeX decides to move the reference to a new page while typesetting (as the pagecounter will be incorrect).
    – Caramdir
    Nov 22 '11 at 3:46
  • I wonder whether zref's \zpageref is required here...?
    – Werner
    Nov 22 '11 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.