1

I would like to use \pageref's for a range of pages ie. at one point in my file I have something along the lines of

See pages \pageref{ref1}--\pageref{ref1_end}.

Where ref1 points to some section, and I would like ref1_end to point to the end of that section:

\section{section 1}\label{ref1}
a lot of text.\label{ref1_end} % label does not work: Reference `ref1_end' on page X undefined on input line Y.

\section{section 2}\label{ref2}
more text \label{ref2_end}

Using \pageref{ref1}--\pageref{ref1_end} would be wrong if there is a pagebreak between the two sections.

How can I add a label to the end of the text. Is there some way of doing this using some 0-size environment or phantom section?

Note: this question should be quite related to this and this, but differs in that I want to know what I can attach the label to without the output text and spacing having to look any different.

1 Answer 1

1

You need to mark the end of the section with a label. I adopted your scheme of adding _end for the final label.

\documentclass{article}
\usepackage{refcount}

\usepackage{lipsum}

\newcommand{\pagerefrange}[1]{%
  \ifnum\getpagerefnumber{#1}=\getpagerefnumber{#1_end}\relax
    page~\pageref{#1}%
  \else
    pages~\pageref{#1}--\pageref{#1_end}%
  \fi
}

\begin{document}

See \pagerefrange{ref1} and \pagerefrange{ref2}.

\section{A long section}\label{ref1}

\lipsum[1-10]
End.\label{ref1_end}

\section{A short section}\label{ref2}

\lipsum[1]
End.\label{ref2_end}

\end{document}

The macro compares the two page references, if they're equal “page n” is issued, otherwise “pages m–n”.

enter image description here

2
  • This looks really nice. You were right in that I could add a label straight in the text. It seems I had a typo.
    – M.T
    Commented Mar 15, 2020 at 17:33
  • Great code, but It doesn't work for roman numerals. Could you adapt it to this case?
    – yannis
    Commented Dec 4, 2021 at 15:50

You must log in to answer this question.

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