2

I will try to describe my question as clear as possible. First a working example.

\documentclass[french,11pt]{article}

\usepackage[french]{babel}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
 \usepackage{kpfonts}
 \usepackage{geometry}
 \geometry{ a4paper, total={170mm,257mm}, left=20mm, top=20mm}
 \usepackage{blindtext}

\setlength{\parskip}{1.2ex} \setlength{\parindent}{0em} \clubpenalty = 10000 \widowpenalty = 10000

 \usepackage{graphicx,bm,url, amsfonts, latexsym, verbatim, xspace, setspace,mathrsfs,marvosym,wasysym,amsmath, amssymb}
 \usepackage{caption}
 \usepackage[caption=false]{subfig}
 \usepackage{epstopdf}

\usepackage[colorlinks=true, linkcolor=red, urlcolor=blue, pdftitle={article}, pdfauthor={DSA}]{hyperref} 

\usepackage{authblk}

\begin{document}

\title{BlaBlaBla} \author[,1]{Dimitrios\thanks{\texttt{\url{dimitrios.@X.fr}}}} %

\date{}

\clearpage\maketitle \thispagestyle{empty}

\begin{itemize}
\item \blindtext 

See reference \ref{foo3}.
\item \blindtext

See reference \ref{foo1}.

\item \blindtext 

See reference \ref{foo2}. 
\end{itemize}

\newpage

\blindtext

\begin{enumerate}
\item\label{foo1} \textbf{The foo1 reference}
\item\label{foo2} \blindtext 

\textbf{The foo2 reference}

\item\label{foo3} \textbf{The foo3 reference}.
\end{enumerate}


\end{document} 

When I click to the link, I move to the associated reference. What I want to achieve is by clicking to an small triangle or a vertical-point-upwards arrow next to the reference to move backwards to the text that the link is. That is, like the forth-and-back effect that wikipedia articles have (see Figure below).

enter image description here

  • If you are looking for a bibliography solution and can use biblatex you could have a look at tex.stackexchange.com/q/396713/35864. – moewe Mar 4 at 10:30
  • @moewe Thanks, but I don't want a bibliography solution. It was just for provide insight to I want to achieve that I include the image from wikipedia. Sorry for the misunderstanding. – Dimitris Mar 4 at 10:33
  • This is a bibliographies - related question and not about the cross-referencing between documents or within section/equation numbers. The backref option of hyperref should do what you request – user31729 Mar 4 at 10:34
  • @ChristianHupfer See my comment above. (Thanks for your feedback). – Dimitris Mar 4 at 10:35
  • @ChristianHupfer I rolled back your edit, because the OP confirmed that while the example screenshot was of a bibliography, the intended use case is not a backref for \cite but for the normal \label-\ref mechanism. – moewe Mar 4 at 12:36
3

Here is an etoolbox-based implementation of a backref-able \ref.

Use \backrefref{<label>} to create a \reference to a <label> that can will be linked back to. Use \backreflabel{<label>} to set a label and output the backref marks. Note that \backreflabel should be called directly where the <label> anchor attaches. Otherwise you may end up with incorrect link targets.

The solution works by writing info to the .aux file, so it may will need at least two LaTeX runs.

\documentclass[french,11pt]{article}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{blindtext}

\usepackage{etoolbox}

\usepackage[colorlinks]{hyperref}

\newcommand*{\backrefmarker}{$\uparrow$}
\newcommand*{\backrefmultformat}{\textsuperscript}

\makeatletter
\newcommand*{\br@currlabels}{}
\newcommand*{\br@linklabels}{}

\newcommand*{\backrefref}[1]{%
  \listgadd{\br@currlabels}{#1}%
  \ifltxcounter{br@curr@#1}
    {}
    {\newcounter{br@curr@#1}
     \csgdef{thebr@curr@#1}{\alph{br@curr@#1}}}%
  {\refstepcounter{br@curr@#1}%
   \label{br@#1@\the\value{br@curr@#1}}}%
  \ref{#1}}


\newcommand*{\backreflabel}[1]{%
  \label{#1}%
  \backreflabel@display{#1}}

\newcommand*{\backreflabel@display}[1]{%
  \ifltxcounter{br@link@#1}
    {\ifnumequal{\value{br@link@#1}}{1}
       {\hyperref[br@#1@1]{\backrefmarker}}
       {\ifnumgreater{\value{br@link@#1}}{1}
          {\backrefmarker
           \backrefmultformat{\backreflabel@display@loop{#1}}}
          {\textbf{??}}}}
    {\textbf{??}}}

\newcounter{br@label@loop}

\newcommand*{\backreflabel@display@loop}[1]{%
  \setcounter{br@label@loop}{0}%
  \whileboolexpr{test {\ifnumless{\value{br@label@loop}}{\value{br@link@#1}}}}
    {\stepcounter{br@label@loop}%
     \ref{br@#1@\the\value{br@label@loop}}}}


\newcommand*{\br@aux@readlabel}[2]{%
  \ifinlist{#1}{\br@linklabels}
    {}
    {\listgadd{\br@linklabels}{#1}}%
  \ifltxcounter{br@link@#1}
    {}
    {\newcounter{br@link@#1}}%
  \setcounter{br@link@#1}{#2}}

\newcommand*{\br@writelabel}[1]{%
  \ifltxcounter{br@link@#1}
    {\ifnumequal{\value{br@curr@#1}}{\value{br@link@#1}}
       {}
       {\@latex@warning@no@line{%
          Label(s) may have changed.
          Rerun to get cross-references right}}}
    {\@latex@warning@no@line{%
       Label(s) may have changed.
       Rerun to get cross-references right}}%
  \immediate\write\@mainaux{\string\br@aux@readlabel{#1}{\the\value{br@curr@#1}}}}

\AtEndDocument{%
  \forlistloop{\br@writelabel}{\br@currlabels}}
\makeatother

\begin{document}

\begin{itemize}
\item \blindtext

See reference \backrefref{foo3}.
\item \blindtext

See reference \backrefref{foo1}.

\item \blindtext

See reference \backrefref{foo2}.
\end{itemize}

\clearpage

\blindtext

\begin{enumerate}
\item\backreflabel{foo1} \textbf{The foo1 reference}
\item\backreflabel{foo2} \blindtext

\textbf{The foo2 reference}

\item\backreflabel{foo3} \textbf{The foo3 reference}.
\end{enumerate}

\clearpage

See reference \backrefref{foo1}.
See reference \backrefref{foo2}.

\clearpage

See reference \backrefref{foo2}.
\end{document}

Wikipedia-like backrefs for <code>\ref</code>--<code>\label</code>

  • Wow! Exactly what I wanted it! Thank you very much! Bravo! – Dimitris Mar 4 at 12:57
0

This is more an extended comment than a real answer.

I agree with previous comments for the easy way to do that with backref package or more precisely the backref option passed to hyperref at load time. But it could be interesting to extend it to \label and not only to \cite. In my answer to question Back link all headings with respective TOC entry I have provided a solution that could be used as a starting point.

But this raises a problem of ambiguity : the reference to a label is unique, but the reverse direction is not, as you could reference a single label several times.

Furthermore, the future references are not known at creation of the label, and even if you decide to fix the ambiguity by choosing to back link to last (or first) reference, you will have to write it to .aux file and read it back at the next compilation, which becomes much more involved.

If you really need that, and left me a delay of a few days, I could extend the above quoted solution to labels.

By the way the requested feature is implemented out of the box in several popular pdf viewers, as they can keep a trace of browsing history, thus lifting the ambiguity problem.

  • Thanks for this answer. I understand from your words and the comments that this is not trivial (as I thought:-)!). Is there an easier way to achieve such a forth-and-back behavior? I saw with backref option. Is there an example somewhere ? And the upwards arrow in wiki manner? – Dimitris Mar 4 at 11:11
  • What do you mean by "an easier way" ? Is the pdf viewer feature not sufficient ? For which subject are you looking for an example ? – Jhor Mar 4 at 11:18
  • I apologize for not being clear. If my understanding is correct the easiest way to achieve the requested feature with a common pdf viewer? backref option was a couple of times mentioned. Where can I found an example of its usage in the current context? Thanks again for your time! – Dimitris Mar 4 at 11:24
  • Sorry but backref can not help "in the current context" as it works only for bibliography. It produces beside each bibliogaphic item the list of section numbers (or pages if you use pagebackref) where the reference has been \cited. – Jhor Mar 4 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.