6

I am looking for a solution to extend the hyperref link for a figure, a chapter, etc. to the word "Figure", (or "Chapter") beforehand and the picture subindex a, b, c, etc. present in the figure itself afterhand, using a single function that could be written for example \ref[Figure][a]{missing Figure}. Anyway to do this ?

Here is the MWE:

\documentclass[10pt,a4paper]{article}

\usepackage{todonotes}
\usepackage{hyperref}

\begin{document}

\begin{figure}[t]
   \missingfigure[figwidth=6cm]{- << Ciel, mon mari >> dit-elle !}
\caption{a: Missing figure, and b: still missing figure.}
\label{fig:missing Figure}
\end{figure}

Something in Figure \ref{fig:missing Figure} a and some other thing in Figure \ref{fig:missing Figure} b.

\end{document}
Heiko Oberdiek
  • 271,626
HcN
  • 1,274
  • 9
  • 25

2 Answers2

8

\hyperref with the optional argument can be used to link to labels with arbitray text:

\hyperref[<label>]{<arbitrary text>}

The star form \ref* sets a reference without additional link, since the link is already set by \hyperref.

The following example defines \figref for figure references. The optional argument takes the subfigure number, the mandatory argument the label name without prefix fig:.

Full example:

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{todonotes}
\usepackage{hyperref}

\newcommand*{\figref}[2][]{%
  \hyperref[{fig:#2}]{%
    Figure~\ref*{fig:#2}%
    \ifx\\#1\\%
    \else
      \,#1%
    \fi
  }%
}

\begin{document}

\begin{figure}[t]
   \missingfigure[figwidth=6cm]{- << Ciel, mon mari >> dit-elle !}
\caption{a: Missing figure, and b: still missing figure.}
\label{fig:missing Figure}
\end{figure}

The figure \figref{missing Figure} contains two subfigures.
Something in \figref[a]{missing Figure} and some other thing in
Figure \figref[b]{missing Figure}.

\end{document}

Result

Version as generic command form via \myref (I do not recommend redefining \ref):

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{todonotes}
\usepackage{hyperref}
\usepackage{twoopt}

\newcommandtwoopt*{\myref}[3][][]{%
  \hyperref[{#3}]{%
    \ifx\\#1\\%
    \else
      #1~%
    \fi
    \ref*{#3}%
    \ifx\\#2\\%
    \else
      \,#2%
    \fi
  }%
}

\begin{document}

\begin{figure}[t]
   \missingfigure[figwidth=6cm]{- << Ciel, mon mari >> dit-elle !}
\caption{a: Missing figure, and b: still missing figure.}
\label{fig:missing Figure}
\end{figure}

The figure \myref[Figure]{fig:missing Figure} contains two subfigures.
Something in \myref[Figure][a]{fig:missing Figure} and some other thing in
Figure \myref[Figure][b]{fig:missing Figure}.

\end{document}

\eqref

A star form \eqref is not provided, but something like (\ref*{...}) can be used instead. Or more precise, \eqref is defined in package amsmath as:

\newcommand{\eqref}[1]{\textup{\tagform@{\ref{#1}}}}

Then \eqrefstar without link can be defined as:

\makeatletter
\newcommand*{\eqrefstar}[1]{\textup{\tagform@{\ref*{#1}}}}
\makeatother
Heiko Oberdiek
  • 271,626
  • Thanks a lot indeed, this is almost what I want. Although, I was more looking for something that could take the word "Figure" as an optionnal argument so I could use the same function for anything. By the way, is there an easy way to extend it to \eqref ? \eqref* does not seem to work. – HcN Sep 08 '15 at 11:05
  • That eqref comment will not give upright ()s. It is "fairly" simple to combine the hyperref command and eqref. – daleif Sep 08 '15 at 11:18
  • 1
    @daleif I have added a definition for \eqrefstar, a \eqref without link. – Heiko Oberdiek Sep 08 '15 at 11:21
0

Many thanks to Heiko and the others. That is perfect. This is just to add eqref to the answer:

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{hyperref}
\usepackage{twoopt}

\newcommandtwoopt*{\myeqref}[3][][]{%
  \hyperref[{#3}]{%
    \ifx\\#1\\%
      Equation~%
    \else
      #1~%
    \fi
    (\ref*{#3})%
    \ifx\\#2\\%
    \else
      \,#2%
    \fi
  }%
}
\begin{document}

See \myeqref{eq:plancksrelation} (\myeqref[Relation][a]{eq:plancksrelation}) for details.

\begin{equation}
E = \hbar \omega
\label{eq:plancksrelation}
\end{equation}

\end{document}
HcN
  • 1,274
  • 9
  • 25