# Back-referencing in LaTeX [duplicate]

Possible Duplicate:
Usage of table/figures shown in caption

Is there any way to make back-references, outside bibtex, so that by using something like \backref LaTeX would show where there are references to an item? For instance:

...
\begin{enumerate}
\item \label{1} Something. Referenced by [\backref].
\item \label{2} Something else. Referencing [\ref{1}].
\end{enumerate}
...


Outputting something like this:

1. Something. Referenced by [2].
2. Something else. Referencing [1].

Well, it will be useful for genealogical texts, where each item is a family, and each subitem a person. When the person marries, I can refer to what family he/she is from, but it would be useful to see to what family he/she belongs also. Perhaps an example will help ; ) :

\begin{enumerate}

\item \label{f:1} John and Mary had:
\begin{enumerate}
\item \label{p:1} James
\end{enumerate}

\item \label{f:2} Bruce and Alina had:
\begin{enumerate}
\item \label{p:2} Elisabeth
\end{enumerate}

\item \label{f:3} James [\ref{p:1}] and Elisabeth [\ref{p:2}] married.

\end{enumerate}


The \backref would make me able to show were James and Elisabeth married.

-

## migrated from stackoverflow.comDec 11 '11 at 16:02

This question came from our site for professional and enthusiast programmers.

## marked as duplicate by Marco Daniel, lockstep, Paulo Cereda, Joseph Wright♦Dec 12 '11 at 18:18

not sure why you want \backref here; in this example, \ref would seem to do as well since there's a clear \label to point to. \backref does make sense in a bibliography, where the pointer is to a \cite in the body of the text, and there's no other labeling mechanism. can you provide a more compelling example? –  barbara beeton Dec 11 '11 at 16:16
I've added an example. –  Parjánya Dec 11 '11 at 16:48
@barbara beeton: With the example, it looks like he wants to write in item p:1 that James (married \backref{p:1}) and in p:2 that Elisabeth (married \backref{p:2}). –  Ryan Reich Dec 11 '11 at 17:48
@RyanReich Reich: not quite. My first example is much more objective, I think : /. With \ref I can show James' parents, in my example, but if I want to know where his marriage is, the only way I know is to put /ref{f:3} in the item p:1 by hand, but it should be doable by a command like \backref... Marco Daniel spotted a very similar question, but only with page numbers. I'm trying to understand how it works in the example there. –  Parjánya Dec 11 '11 at 18:18
@Parjánya: That's what I meant to say :) In any case, that's what my answer does, so tell me if it's what you're looking for. –  Ryan Reich Dec 11 '11 at 18:21

You can do this by patching the existing \ref command to remember the context in which it was called. This new version works even if \ref is called multiple times on the same label, producing a comma-separated list (no spaces) of numbers. It has some limitations; for example, the list will produce numbers that look the same no matter what kind of label they belonged to (enumerate, section, and so on). I think one could add some kind of hook to \backref to process the list and improve this situation, but I won't do that unless you say you need it.

This does not work with hyperref, for which I apologize. If you want it, I will go unravel what it does.

This is a complete document; you should copy everything between \documentclass{article} and \begin{document} into your preamble.

\documentclass{article}
\usepackage{etoolbox}

\makeatletter

\def\@newbackl@bel#1#2#3{%
\@ifundefined{#1@#2}%
{\global\@namedef{#1@#2}{#3}}
{\cs@g@appendpair{#1@#2}{#3}}%
}
% #1 and #2 are each of the form {a}{b}, and the a and b groups are to be concatenated independently.
\def\cs@g@appendpair#1#2{{%
\edef\first{\csname #1\endcsname}%
\csxdef{#1}%
{{\expandafter\@firstoftwo\first,\@firstoftwo#2}%
{\expandafter\@secondoftwo\first,\@secondoftwo#2}}%
}}

% Patching into the existing commands
\def\newbacklabel{\@newbackl@bel B}
\let\backlabel=\label
\patchcmd\backlabel{\newlabel}{\newbacklabel}{}{}
\let\backref=\ref
\patchcmd\backref{r@#1}{B@#1}{}{}
\let\pagebackref=\pageref
\patchcmd\pagebackref{r@#1}{B@#1}{}{}
\pretocmd\ref{\backlabel{#1}}{}{}

\makeatother

\begin{document}
\begin{enumerate}
\item \label{e:1} Part 1, referenced by part(s) \backref{e:1} on page(s) \pagebackref{e:1}.
\item \label{e:2} Part 2, referencing part \ref{e:1}
\item \label{e:3} Part 3, referencing part \ref{e:1}.
\end{enumerate}
\newpage
\section{A section}
\label{s:1}
Here is another reference to part \ref{e:1}.

\end{document}

-
This works perfectly, Ryan, thank you a lot! I don't need anything more than this, actually. I think it's a very useful feature, perhaps it deserves to be a package... –  Parjánya Dec 11 '11 at 19:00