# Creating a “\label” that, if necessary, spans over several pages

What I want to do is the following:

I want to refer to an earlier block of text. I could do this by using the \label and the \ref commands like so:

\label{x} Lorem ipsum dolor sit amet, consectetur adipiscing elit.

See page \ref{x}.


This would create something like "See page 1" out of the second line of code.

However, let's say the sentence above, in the first line of code, spans over more than one page. Then I would like the readout to be "See pages 1-2". Now I could solve this by doing the following:

\label{x} Lorem ipsum dolor sit amet, consectetur adipiscing elit. \label{y}

See pages \ref{x}-\ref{y}.


This would create something like "See pages 1-2" out of the second line of code.

However, this is not flexible enough. If the sentence doesn't span over several pages, I would end up with something like "See pages 1-1", something I definitely wouldn't want.

So, I guess you can see my dilemma here. Is there any way for me to do what I want?

-
a tip: you can use backticks  to mark your inline code as I did in my edit. – Hendrik Vogt Mar 16 '11 at 8:52
Thanks for the tip! – Speldosa Mar 16 '11 at 12:02

This would be a solution using the varioref package, as Martin mentioned:

\documentclass{article}
\usepackage{blindtext}
\usepackage{varioref}
\usepackage[english]{babel}
\begin{document}

\label{text1start}\blindtext[5]\label{text1stop}
\label{text2start}\blindtext[2]\label{text2stop}

See \vpagerefrange{text1start}{text1stop} and \vpagerefrange{text2start}{text2stop}

\end{document}


It's output in this case:

-
This was great. I'll use this for the time being and switch over to Martin's suggestion if I need to have greater control over my output. – Speldosa Mar 18 '11 at 15:07
@Speldosa: Note that there seem to be some problems when the varioref package is used together with the hyperref package. To quote the hyperref ReadMe: “Perhaps you are lucky and some of the features of varioref work […]” – mhp Sep 1 '11 at 8:17
@mhp: Thanks for the heads up. I'll be on the lookout for problems. – Speldosa Sep 2 '11 at 7:29

The standard \labels are normally only for sectioning commands and captions etc., not for pieces of text. Also its implementation is redefined by packages like hyperref so it is difficult to use them and ensuring 100% compatibility with existing packages.

Instead you could define an own label macro like \textlabel and then \textref which takes two arguments and checks if both refer to the same page. The following code does this. A possible improvement would be to non hard-code the 'page' text but use a macro which can be changed, e.g. to a different language.

\documentclass{article}
\usepackage{hyperref}
\usepackage{lipsum}
\makeatletter
\newcommand*{\newtextlabel}[1]{\global\@namedef{textlabel@#1}}
\newcommand*{\textlabel}[1]{%
% Write label which holds current page
\@bsphack
\protected@write\@auxout{}%
{\string\newtextlabel{#1}{\thepage}}%
\@esphack
% Also add a normal label to be able to use hyperrefs \pageref later
\label{#1}%
}
\newcommand*{\textref}[2]{%
\begingroup
% Get saved label values:
\expandafter\let\expandafter\labela\csname textlabel@#1\endcsname
\expandafter\let\expandafter\labelb\csname textlabel@#2\endcsname
\ifx\labela\labelb
page~\pageref{#1}%
\else
pages~\pageref{#1}--\pageref{#2}%
\fi
\endgroup
}
\makeatother

\begin{document}
\section{Test}\label{test}
\textlabel{a}
\lipsum[1]
\textlabel{b}
\lipsum[2]
\textlabel{x}
\lipsum
\textlabel{y}
\lipsum
See \textref{a}{b} and \textref{x}{y}.

\end{document}


Edit: Added use of \pageref in the final output. The varioref package might also be of interest.

-
Why do you define a new type of label instead of simply comparing the two original \pageref's? E.g. \ifthenelse{\equal{\pageref{x}}{\pageref{y}}}{see page \ref{x}}{see page \ref{x} to \ref{y}} – Ulrike Fischer Mar 16 '11 at 9:04
@Ulrike: I do this to be independent of redefinitions of \pageref e.g. by hyperref. Because its \pageref isn't fully expandable \ifthenelse doesn't work (for me). I like to access the page number directly without any typesetting or hyperref material. – Martin Scharrer Mar 16 '11 at 9:44
I don't have any problems with the \ifthenelse even if I load hyperref. – Ulrike Fischer Mar 16 '11 at 11:33
@Ulrike: Yes, you are right, \ifthenelse works fine with \pageref. My bad, I had two minor errors in my test file which caused the trouble. I must look on the source code of ifthen.sty to learn how it expands \pageref successfully. A simple \edef fails. You should post your code as an answer (but replace \ref with \pageref`). – Martin Scharrer Mar 16 '11 at 12:05
I prefer meep.meep's sollution because of its simpleness but your's seem great if I want to customize the output further. – Speldosa Mar 18 '11 at 15:08