4

I'm trying to add a symbol (like an arrow) behind a reference, editing the \ref command, something like that:

\documentclass[a4paper,12pt]{report}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{pifont}
\newcommand{\newref}[1]{\ref{#1}\,\ding{230}}

\begin{document}
As we can see in figure \newref{photo} ...

\begin{figure}[H]
\includegraphics[]{./figures/photo.jpg}
\caption{} \label{photo}
\end{figure}
\end{document}

In the PDF, it works clicking in the number "1" of the text "As we can see in figure 1 (arrow) ...", but I would like to achieve that clicking in the arrow brings me also to the place where the photo is. Is that possible?

Clément
  • 5,591
Mayo
  • 55
  • 2
    Welcome to TeX.SX! Good MWE! But avoid the [H] option. ;-) – egreg May 19 '15 at 21:01
  • (A quick solution that I thought of was incorrect. Then, I thought this question was a duplicate of http://tex.stackexchange.com/a/102724/34551 . They are related, but OP wants something slightly different, so I removed my comments.) – Clément May 19 '15 at 21:27
  • I need to put the [H] option, otherwise the figure goes to another page, to the previous chapter. I put all the figures in an annex at the end of the document. – Mayo May 20 '15 at 14:40

2 Answers2

2
\newcommand{\newref}[1]{\hyperref[{#1}]{\ref*{#1}\,\ding{230}}}
  • \hyperref{<label>}{<free text>} creates a link similar to \ref{<label>} using the same label <label>, but the second argument takes a free text.

  • The star form \ref* creates a reference without link. This avoids a nested link, the reference is already inside the link, created by \hyperref.

  • I recommend package caption or hypcap to move the link target to the beginning of the figure environment. Otherwise the link points to the caption.

Heiko Oberdiek
  • 271,626
  • Why do you protect the first argument of \hyperref with [{#1}]? – Clément May 19 '15 at 21:25
  • 2
    @Clément TeX only checks the matching of curly braces (characters with category code 1 and 2). If #1 contains a closing ] outside of groups, then this is found instead of the outer ], because it comes first. – Heiko Oberdiek May 19 '15 at 21:32
  • Indeed, without that protection, a label phot]o produces a Hyper referencephot' on page 1 undefined`. – Clément May 19 '15 at 21:39
  • Works perfect. Only an insignificant comment: for adding the word "Figure" in the link (red box), I need to add it in the definition command: \newcommand{\newautoref}[1]{\hyperref[{#1}]{Figure\,\autoref*{#1}\,\ding{230}}} – Mayo May 20 '15 at 14:36
  • I guess you wanted to comment on my answer, and not on the one from Heiko, who do not mention autoref. If this is the case, just try to compile the code I'm giving: the word "Figure" should be inside the link when using \newcommand{\newautoref}[1]{\hyperref[#1]{\autoref*{#1}\,\ding{230}}}. Please edit your question if you have a more specific problem. – Clément May 20 '15 at 21:14
  • @Clément Yes, I wanted to comment your answer but I didn' know where to place the comment and I thought it would be better for the thread to comment in the most recent answer because Heiko also cites the line you wrote -sorry if I did bad-. In fact, with two solutions you provide -\newautoref and \newref-, I need to put the word inside the definition command, may be due to the language I use, spanish... So, both:\newcommand{\newref}[1]{\hyperref[{#1}]{fig.\,\ref*{#1}\,\ding{230}}} \newcommand{\newautoref}[1]{\hyperref[{#1}]{fig.\,\autoref*{#1}\,\ding{230}}} work and the problem is solved. – Mayo May 21 '15 at 08:10
2

You can achieve this thanks to the \ref* command, which, according to hyperref's manual, create a reference with the correct number, but without link.

I also enclosed a solution with autoref, which I recommend (it automatically "guess" the type of the reference you are making, e.g., Theorem, Lemma, Figure, Chapter, etc.).

\documentclass{article}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{pifont}

\newcommand{\newref}[1]{\hyperref[#1]{\ref*{#1}\,\ding{230}}}

\newcommand{\newautoref}[1]{\hyperref[#1]{\autoref*{#1}\,\ding{230}}}

\begin{document}
As we can see in figure \newref{photo}

But in \autoref{photo}, or in \newautoref{photo}

\begin{figure}
Toto
\caption{My caption} \label{photo}
\end{figure}
\end{document}

enter image description here

Clément
  • 5,591