LaTeX forum ⇒ GeneralTwo pagerefs that possibly refer to the same page Topic is solved

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

Two pagerefs that possibly refer to the same page

Postby Yfke » Sat Apr 27, 2013 5:27 pm

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:
  1. \documentclass{article}
  2. \begin{document}
  3. Two formula's:
  4.  
  5. \label{f1} The first formula
  6.  
  7. \label{f2} The second formula
  8.  
  9. A lot of other text here.
  10.  
  11. % code that facilitates the following:
  12. % The formulas on page(s) \pageref{f1} (and \pageref{f2}) ...
  13. \end{document}


Is there a way to achieve this? Thanks in advance! :)

Tags:

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

Postby Namrod » Sat Apr 27, 2013 6:10 pm

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

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


Thus with this code :

  1. \documentclass{article}
  2.  
  3. \usepackage[utf8]{inputenc}
  4. \usepackage[T1]{fontenc}
  5. \usepackage{ifthen}
  6.  
  7. \newcommand{\pageeq}[2]{\ifthenelse{\pageref{#1}=\pageref{#2}}{page \pageref{#1}}{pages \pageref{#1} and \pageref{#2}}}
  8.  
  9. \begin{document}
  10.  
  11. \begin{equation}
  12. e=mc^2
  13. \label{eq1}
  14. \end{equation}
  15.  
  16. \begin{equation}
  17. (a+b)^2=a^2+2ab+b^2
  18. \label{eq2}
  19. \end{equation}
  20.  
  21. ... formulas \pageeq{eq1}{eq2}
  22.  
  23. \end{document}


you will get ... formulas page 1

whereas this code :


  1. \documentclass{article}
  2.  
  3. \usepackage[utf8]{inputenc}
  4. \usepackage[T1]{fontenc}
  5. \usepackage{ifthen}
  6.  
  7. \newcommand{\pageeq}[2]{\ifthenelse{\pageref{#1}=\pageref{#2}}{page \pageref{#1}}{pages \pageref{#1} and \pageref{#2}}}
  8.  
  9. \begin{document}
  10.  
  11. \begin{equation}
  12. e=mc^2
  13. \label{eq1}
  14. \end{equation}
  15. \newpage
  16. \begin{equation}
  17. (a+b)^2=a^2+2ab+b^2
  18. \label{eq2}
  19. \end{equation}
  20.  
  21. ... formulas \pageeq{eq1}{eq2}
  22.  
  23. \end{document}


will produce ... formulas pages 1 and 2

Yfke
Posts: 4
Joined: Mon Aug 30, 2010 9:10 pm

Postby Yfke » Sat Apr 27, 2013 7:20 pm

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 :)


Return to “General”

Who is online

Users browsing this forum: No registered users and 10 guests