4

I'm trying to make a really basic reference.

Let's say I have theses lines in my document :

Lorem Ipsum ...
Lorem Ipsum ... [1] here ... Lorem Ipsum ...
Lorem Ipsum ...

I want to anywhere in my document make a reference to [1] here.

I thought it would be simple to find out but I searched for too long and I got confused with all the sets and making a simple reference now seems to be like rocket-science to me.

I want the following result :

enter image description here

When clicking on the colored number 1, the line [1] here get at the top of my document viewer so the user feels easy to find the line directly.

1 Answer 1

7

The hyperref package provides the pair of commands

\hypertarget{<name>}{<text>}
\hyperlink{<name>}{<text>}

for the creation of cross-references that are independent from counters. They are analogues of \label and \ref, respectively. Here's an example:

\documentclass{article}

\usepackage[breaklinks=true]{hyperref}

\begin{document}

Go to the \hyperlink{lip}{second}, or  \hyperlink{lipsys}{third} paragraph.
\newpage
The first hyperlink brings you \hypertarget{lip}{here.}
\newpage
The second hyperlink brings you \hypertarget{lipsys}{here.}

\end{document}

On the other hand, if you wish associate cross-references to a counter, you can try using the following declarations:

\documentclass{article}

\usepackage{etoolbox}

\usepackage[breaklinks=true,colorlinks=true,linkcolor=red]{hyperref}

\newrobustcmd*{\foo}[1]{{\refstepcounter{bar}\label{#1}\thebar}}

\newcounter{bar}
\renewcommand{\thebar}{\textcolor{black}{[}\arabic{bar}\textcolor{black}{]}}

\begin{document}

Go to \ref{lipsys}, and \ref{lip}.
\newpage
The second hyperlink brings you \foo{lip} here.
\newpage
The first hyperlink brings you \foo{lipsys} here.

\end{document}

In the example above, \foo{<key>} (1) globally increments the counter bar, (2) maps the key to the current reference string, and (3) prints the value of bar. \textcolor{black}{...} is used to override the formatting applied by the hyperref package (otherwise it's typeset in the same colour as the counter).

enter image description here

2
  • perfect ! thank you, it was not that hard finally and now my document has the look I want, Regards~
    – vdegenne
    Commented Mar 2, 2013 at 21:05
  • and +1 for the counter solution
    – vdegenne
    Commented Mar 2, 2013 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .