2

The figures I intend to include in my latex files are sometimes splitted into subfigures and labeled accordingly (e.g. have an (a), (b), etc marker), but still a single figure. This makes it easier for me to align them properly (labels (a) and (b) on the same height etc.). When I reference them in the text, I use something like \ref{fig:my_face}(a). Using the hyperref package, the text linked includes only the figure number of course, not the (a) as well. Is there a way to include a post- or prenote like the cite command does (e.g. \cite[p. 99]{myRef} links the p.99 as well)?

The following code

\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[bookmarks=true,colorlinks=true,linkcolor=red]{hyperref}

\begin{document}

\begin{figure}
\includegraphics{fig.pdf}
\caption{
Some caption.
}
\label{fig:test}
\end{figure}

\ref{fig:test}(a)

\end{document}

produces the output

enter image description here

I like to have the (a) also link to the image, so it should in appear in red too.

3
  • Doesn't subfig or subcaption package provide this feature already?
    – user31729
    Jan 19 '16 at 10:31
  • Yes, but I would have to provide multiple figures for that. I have my subfigures within a single figure.
    – DaPhil
    Jan 19 '16 at 10:40
  • It would be extremely helpful if you could provide a compilable example!
    – user31729
    Jan 19 '16 at 10:58
2

You can use a combination of a manual \hyperref and \ref*, wrapped inside a macro:

enter image description here

\documentclass{scrreprt}
\usepackage{graphicx}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}

\newcommand{\myref}[2][]{\hyperref[#2]{\ref*{#2}#1}}

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=.3\linewidth]{example-image-a}\quad
  \includegraphics[width=.3\linewidth]{example-image-b}
  \caption{Some caption.}
  \label{fig:test}
\end{figure}

See Figure~\myref[(a)]{fig:test}.

\end{document}

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.