# How to check whether two labels fall on the same page?

I'd like to have something like 5--10 in the header of an article (my document is a journal issue) if it spans the pages 5-10, and 5 (as opposed to 5--5) if it is short and fits on one page. I use \labels in some hooks when including the individual articles which insert things like \label{\articlename:beginning} and \label{\articlename:ending}. Now the question is: how to check whether the two labels fall on the same page? Currently, I use something like

\pageref{\articlename:beginning}%
\expandafter\ifx\csname r@\articlename:beginning\expandafter\endcsname
\csname r@\articlename:ending\endcsname\else
--\pageref{\subjobname:end}%
\fi


(Quite obviously, this can be simplified using etoolbox's \ifcsequal – I'm in the process of moving to etoolbox and that's why I didn't use it before.)

My question is: is there a better way than using \csname r@ something\endcsname (or its equivalent with etoolbox), which seems to me a bit dirty-hackish? Maybe there's a ready-made package for this? (varioref comes to mind, which does similar things, but AFAIK it does not have any public interface like \iflabelsonthesamepage{label1}{label2}{true}{false}.)

-
I am using the refcount package for this task. –  Malipivo Apr 6 '14 at 9:26
@Malipivo: this is great! Can you write an answer about refcount? –  mbork Apr 6 '14 at 9:28
I can't do that right now, but I can do that tomorrow, well I am sure someone will be faster with answering this nice question. –  Malipivo Apr 6 '14 at 9:32

Here's a solution based on refcount and etoolbox. I made a new command called pagespan, which prints your output wherever placed:

\documentclass{article}

\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{refcount}

\newcommand{\pagespan}[2]{%
\ifnumequal{\getpagerefnumber{#1}}{\getpagerefnumber{#2}}{%
\pageref{#1}%
}%
{%
\pageref{#1}--\pageref{#2}%
}%
}

\begin{document}
\label{start}
\lipsum
\lipsum
\label{end}

Should be different (dash-dash expected): \pagespan{start}{end}

\newpage
\label{start2}
\lipsum[1]
\label{end2}

Should be the same (one number expected): \pagespan{start2}{end2}
\end{document}

-
You had several spurious spaces in your code. –  egreg Apr 6 '14 at 11:07