GeneralPage numbers to refs: Adding the word 'page' to pagebackref

LaTeX specific issues not fitting into one of the other forums of this category.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Page numbers to refs: Adding the word 'page' to pagebackref

Post by gmedina »

Hi,

the following code:

Code: Select all

Code, edit and compile here:
\documentclass{article}
\usepackage{natbib}
\usepackage[pagebackref=true]{hyperref}
\hypersetup{ pdfborder={0 0 0},
colorlinks = false}
\renewcommand*{\backreflastsep}{, }
\renewcommand*{\backreftwosep}{, }
\renewcommand*{\backref}[1]{}
\renewcommand*{\backrefalt}[4]{%
\ifcase #1 %
No citations.% use \relax if you do not want the "No citations" message
\or
(page #4).%
\else
(pages #4).%
\fi%
}
\begin{document}
\citep{a,b,c}
\newpage
\citep{a,b,c}
\begin{thebibliography}{9}
\bibitem{a} Dummy bib-reference one
\bibitem{b} Dummy bib-reference two
\bibitem{c} Dummy bib-reference three
\end{thebibliography}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
works OK for me. The three references get the back references (I attach the pdf file obtained after compilation of the code). Thus, if you are having troubles, we will need to see a minimal working example that allow us to reproduce the problem that you mentioned.
Attachments
test.pdf
(16.05 KiB) Downloaded 573 times
1,1,2,3,5,8,13,21,34,55,89,144,233,...

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
TikZ book
pvmeel
Posts: 3
Joined: Fri Jul 17, 2009 11:53 pm

Page numbers to refs: Adding the word 'page' to pagebackref

Post by pvmeel »

Hi gmedina,

Thx for your reply. If figured out what causes the problem: it's the addition of the option "sort&compress" for the natbib package (\usepackage[sort&compress,comma, numbers]{natbib}). It makes multiple refs [1,2,3] appear as [1-3]. Since 2 isn't on the page, it doesn't get backrefs. 1 and 3 are fine (page(s) still works like a charm) but 2 is skipped since it is not displayed. If I remove the option, all backrefs appear as they should.

I haven't been able to find a way around this. Can you think of one? Except for the very obvious removal of the sort&compress option; refs like [6, 7, 8, 9, 10, 11] just look awful…

For completion here's the code I used:

Code: Select all

Code, edit and compile here:
\documentclass[12pt,a4paper]{report}
\usepackage[sort&compress, comma, numbers]{natbib}
\usepackage[pagebackref={true}]{hyperref}
\renewcommand*{\backreflastsep}{, }
\renewcommand*{\backreftwosep}{, }
\renewcommand*{\backref}[1]{}
\renewcommand*{\backrefalt}[4]{%
\ifcase #1 %
\relax %
\or
(page #4).%
\else
(pages #4).%
\fi%
}
\begin{document}
\citep{Hahn,Purcell, CarrPurcell}
\newpage
\citep{Hahn,Purcell, CarrPurcell}
\bibliographystyle{unsrtnat}
\bibliography{Biblio}{}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
And the biblio:

Code: Select all

@ARTICLE{Hahn,
AUTHOR = {E.L. Hahn},
TITLE = {Spin Echoes},
JOURNAL = {Physical Review},
YEAR = {1950},
volume = {80},
pages = {580-594},
}
@ARTICLE{Purcell,
AUTHOR = {E.M. Purcell and H.C. Torrey and R.V. Pound},
TITLE = {Resonance absorption by nuclear magnetic moments in a solid},
JOURNAL = {Physical Review},
YEAR = {1946},
volume = {69},
pages = {37-38},
}
@ARTICLE{CarrPurcell,
AUTHOR = {H.Y. Carr and E.M. Purcell},
TITLE = {Effects of diffusion on free precession in nuclear magnetic resonance experiments},
JOURNAL = {Physical Review},
YEAR = {1954},
volume = {94},
pages = {630-638},
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
pvmeel
Posts: 3
Joined: Fri Jul 17, 2009 11:53 pm

Page numbers to refs: Adding the word 'page' to pagebackref

Post by pvmeel »

Hi all,

I still haven't found a solution to the problem I prviously described. I have however found some sort of work around - placing the missing refs outside the page - although it seem pretty ugly to me. But hej, it works....

Code: Select all

Code, edit and compile here:
blablabla about \citep{a, b, c, d}. Blablabla
\begin{minipage}[b]{40cm} %separate citations outside page to obtain correct backref's in the biblio
\hspace{20cm}
\citep{b}citep{c}
\end{minipage}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I have a new question though: how to make a ref that appears twice on a page get only one pagebackref. Now it looks like: (pages, 3, 5, 13, 13).

Thx in advance!!
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Page numbers to refs: Adding the word 'page' to pagebackref

Post by gmedina »

Hi,
pvmeel wrote:...
I have a new question though: how to make a ref that appears twice on a page get only one pagebackref. Now it looks like: (pages, 3, 5, 13, 13)...
Change the corresponding part of my previous code to

Code: Select all

Code, edit and compile here:
\renewcommand*{\backreflastsep}{, }
\renewcommand*{\backreftwosep}{, }
\renewcommand*{\backref}[1]{}
\renewcommand*{\backrefalt}[4]{%
\ifcase #1 %
No citations.% use \relax if you do not want the "No citations" message
\or
(page #2).%
\else
(pages #2).%
\fi%
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1,1,2,3,5,8,13,21,34,55,89,144,233,...
randerson
Posts: 1
Joined: Tue Aug 18, 2009 5:49 am

Page numbers to refs: Adding the word 'page' to pagebackref

Post by randerson »

pvmeel wrote:
If figured out what causes the problem: it's the addition of the option "sort&compress" for the natbib package (\usepackage[sort&compress,comma, numbers]{natbib}). It makes multiple refs [1,2,3] appear as [1-3]. Since 2 isn't on the page, it doesn't get backrefs. 1 and 3 are fine (page(s) still works like a charm) but 2 is skipped since it is not displayed. If I remove the option, all backrefs appear as they should.

I haven't been able to find a way around this. Can you think of one? Except for the very obvious removal of the sort&compress option; refs like [6, 7, 8, 9, 10, 11] just look awful…
A dirty approach that worked for me:
1) compile (bibtex + pdflatex) without the sort&compress option of natbib, running pdflatex as many times as necesasary for the backrefs to appear as desired.
2) compile again (bibtex + pdflatex) with the sort&compress option of natbib, but only enough times for the references to appear compressed, e.g. [1-4]. At this point, the backrefs should remain intact (i.e. correct page numbers from step 1), as an additional execution of pdflatex is required to change the backrefs (precisely what you don't want to do in this case).

Alternatively, use the cite package by Donald Arseneau (instead of natbib), which is now compatible with both hyperref and backref, as explained here:
http://groups.google.de/group/comp.text ... 4fdf129d98
andrewdickinson
Posts: 1
Joined: Sat Jul 16, 2011 11:25 pm

Re: Page numbers to refs: Adding the word 'page' to pageback

Post by andrewdickinson »

I registered just to say thanks for this. I looked all over for a solution and this did everything I needed.

Thanks again.
thrynae
Posts: 1
Joined: Mon Mar 20, 2023 4:46 pm

Page numbers to refs: Adding the word 'page' to pagebackref

Post by thrynae »

I also created an account just to post on this thread.

I adapted the hack suggested by pvmeel to avoid having to add a minipage every time I cite something. The intention is to redefine the cite command so it creates text outside of the page for pagebackref to see.

Code: Select all

Code, edit and compile here:
\let\oldcite\cite % avoid a recursion by creating a copy of the command
\renewcommand{\cite}[1]{%
\oldcite{#1}%
\makebox[0pt]{% make a box that doesn't take up any space
\hspace{3\textwidth}% move the contents of this box off the page
\citet{#1}% make sure sort&compress can't compress the cites here
}%
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I also put an mbox around oldcite, because I don't want a line break on the hyphen in [1,3-5].
Post Reply