143

Is there a way to make a cross reference to a label of a header with custom text? That is, its text should differ from the Header text, or its number or page (not like the predefined formats in Lyx). Something like the the hyperlinks or bookmarks in MS word, and in such a way that I can write the different text for each cross-reference individually. I'm not looking to define a new format for the references.

For example, if the header text is "State and Path functions", I want to make a cross reference to it with the text "state function".

c32hedge
  • 143
TMS
  • 2,193

2 Answers2

205

With the package hyperref you can use the optional argument of \hyperref to reference a \label with arbitrary text:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{Hello World}
\label{sec:hello}
Reference to \hyperref[sec:hello]{this section}.
\end{document}

Result

Nils L
  • 9,716
Heiko Oberdiek
  • 271,626
  • Why wouldn't you just write "Reference to this section", then? The reference doesn't seem to do anything? – user56834 Dec 13 '17 at 07:28
  • 15
    @Programmer2134 Then, I would not have a short simple example that shows the usage of \hyperref with the optional argument. – Heiko Oberdiek Dec 13 '17 at 16:52
  • 2
    @user56834 when you are looking at a pdf of the document, referencing the section means the text is clickable. So the reference makes the text "this section" a button which, when pressed, takes the reader to that section. – Kraigolas Aug 05 '19 at 00:33
  • 1
    Is there a way to define the custom text once at the label rather than each time at the reference? – stefanbschneider Feb 22 '21 at 09:59
  • 4
    @CGFoX A label has already a text ("1" in the example above), that is shown by \ref. In case of section titles, packages nameref or titleref can be used to get a reference with the title instead. – Heiko Oberdiek Feb 23 '21 at 19:11
0

In LaTeX Beamer the hyperref usage gave me problems so I solved it using hyperlink.

To create a link to a label with a custom text for the clickable link, you can use the \hyperref package and the \hyperlink command. Here's an example:

\usepackage{hyperref}

...

\hyperlink{labelname}{clickable link text}

This will create a clickable link with the text "clickable link text" that will take the reader to the location of the label with the name "labelname".


Here is a full example with Beamer:

\documentclass{beamer}
\mode<presentation> {\usetheme{CambridgeUS}}
\usepackage{hyperref}

\begin{document} \begin{frame} \begin{figure}[!htbp] \centering \includegraphics[width=0.2\linewidth]{myimage.png} \caption{My Image} \label{fig:myimage} \end{figure} \end{frame}

\begin{frame}
    Example 1: Reference to \hyperlink{fig:myimage}{clickable link text}.\\~\\
    Example 2: Reference to \hyperlink{fig:myimage}{clickable link text at pag \pageref{fig:myimage}}.
\end{frame}

\end{document}

And this is the visual result: hyperlink_example

madx
  • 109
  • 2
    Welcome! That is indeed true. beamer has some peculiarities with regards to cross-referencing and disturbs hyperref's syntax in the process. But, how about you give a complete example, like the one in Heiko's answer? So that people are sure what you are talking about? – gusbrs Dec 30 '22 at 18:21
  • Sure, I have edited my answer and I added a complete example with Beamer. – madx Dec 30 '22 at 20:21
  • Great, thanks!. – gusbrs Dec 30 '22 at 20:54