GeneralTwo pagerefs that possibly refer to the same page

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Yfke
Posts: 4
Joined: Mon Aug 30, 2010 9:10 pm

Two pagerefs that possibly refer to the same page

Post by Yfke »

Hello,

In my document I have two labeled formulas that I want to refer to elsewhere using a pageref. Since my text is likely to change in the future, I do not know yet whether or not the formulas will be on the same page.

Ideally, I would want my pageref to be such that if they are both on the same page, the text would read:
"... the formulas on page 5 ..."
If the second formula falls onto the second page, I would like it to read:
"... the formulas on pages 5 and 6 ..."

Of course, it would look stupid if it read "... the formulas on pages 5 and 5 ..." ;)

To provide a minimal working example of what I mean:

Code: Select all

\documentclass{article}
\begin{document}
Two formula's:

\label{f1} The first formula

\label{f2} The second formula

A lot of other text here.

% code that facilitates the following:
% The formulas on page(s) \pageref{f1} (and \pageref{f2}) ...
\end{document}
Is there a way to achieve this? Thanks in advance! :)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Namrod
Posts: 8
Joined: Fri Apr 26, 2013 9:10 pm

Two pagerefs that possibly refer to the same page

Post by Namrod »

You can create your own command which compare the page of the 2 formulas. For example, using the ifthen package :

Code: Select all

\newcommand{\pageeq}[2]{\ifthenelse{\pageref{#1}=\pageref{#2}}{page \pageref{#1}}{pages \pageref{#1} and \pageref{#2}}}
Thus with this code :

Code: Select all

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ifthen}

\newcommand{\pageeq}[2]{\ifthenelse{\pageref{#1}=\pageref{#2}}{page \pageref{#1}}{pages \pageref{#1} and \pageref{#2}}}

\begin{document}

\begin{equation}
e=mc^2
\label{eq1}
\end{equation}

\begin{equation}
(a+b)^2=a^2+2ab+b^2
\label{eq2}
\end{equation}

... formulas \pageeq{eq1}{eq2}

\end{document}
you will get ... formulas page 1

whereas this code :

Code: Select all

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ifthen}

\newcommand{\pageeq}[2]{\ifthenelse{\pageref{#1}=\pageref{#2}}{page \pageref{#1}}{pages \pageref{#1} and \pageref{#2}}}

\begin{document}

\begin{equation}
e=mc^2
\label{eq1}
\end{equation}
\newpage
\begin{equation}
(a+b)^2=a^2+2ab+b^2
\label{eq2}
\end{equation}

... formulas \pageeq{eq1}{eq2}

\end{document}
will produce ... formulas pages 1 and 2
Yfke
Posts: 4
Joined: Mon Aug 30, 2010 9:10 pm

Re: Two pagerefs that possibly refer to the same page

Post by Yfke »

Thank you Namrod, this was exactly the solution I was looking for. I had never heard of the ifthen-package before, I'll definitely delve into it! It looks like it'll provide a whole lot of new LaTeX-possibilities :)
Post Reply