# Redefine \figref so that it can accept optional arguments

I have some figures to include in my document that contains subfigures. I don't use the subfigure environment, but rather I produce only one EPS in which every part of it is labelled as A, B and so on. (editors want subfigures to be uploaded this way).

I want to use these figures also in my PhD thesis and refer to them using the \figref command. The problem is that if I write something like ( \figref{fig:label} A), then the "Figure 1" part is blue and contains the link to the figure but the "A" is black. I find this a bit ugly, so I'd like to redefine the \figref so that it can accept an argument like \figref{fig:label}{A} and the reference and the link (I use the package hyperref in my document) appear blue. Is there any way to do this?

-

My subcaption package offers a command called \phantomcaption (resp. \phantomsubcaption) for supporting labels and references to sub-figures with already build-in sub-captions. Using this approach there is no need to re-define \figref, it should work with the existing definition of yours.

An example:

\documentclass{article}
\usepackage{subcaption}
\usepackage[demo]{graphicx}

% Define \figref as abbreviation of "Figure~\ref{...}"
\newcommand\figref{Figure~\ref}

% Use uppercase letters for sub-figures
\renewcommand\thesubfigure{\Alph{subfigure}}

\begin{document}

\begin{figure}
\centering
\begin{subfigure}{0.4\textwidth}
\includegraphics{A}
\phantomcaption
\label{fig:test:A}
\end{subfigure}
\begin{subfigure}{0.4\textwidth}
\includegraphics{B}
\phantomcaption
\label{fig:test:B}
\end{subfigure}
\caption{A demo figure}
\end{figure}

\begin{figure}
\begin{subfigure}{\linewidth}%
\centering
\includegraphics{AandBandC}
\phantomcaption
\label{fig:test2:A}%
\phantomcaption
\label{fig:test2:B}%
\phantomcaption
\label{fig:test2:C}%
\end{subfigure}
\caption{A demo figure}
\label{fig:test2}
\end{figure}

See \figref{fig:test:A} and \figref{fig:test2:C}...

\end{document}


See subcaption package documentation for details.

(Please note that this needs version 1.1 (2011/08/17) of the subcaption package.)

-
Thanks @Axel . I'm using the version installed with TexLive on my Ubuntu 11.10 (I've the package in /usr/share/texmf-texlive/tex/latex/caption/subcaption.sty). Unfortunately I don't think it is the most recent one, since when I try to use the \phantomcaption command I get an "Undefined control sequence \phantomcaption" error. When will your package be in the official repos? Thanks a lot for your help in any case! –  lucacerone Jan 10 '12 at 9:54
My package is part of TeXlive. I do not have any influence when Ubuntu will package a more recent version of TeXlive, currently TeXlive 2009 is in the repros of Ubuntu while TeXlive 2011 is the current version, so Ubuntu is laying two years behind. So either update/install the recent caption package bundle manually, or install TeXlive 2011. See also: tex.stackexchange.com/questions/27721/… and tex.stackexchange.com/questions/1092/… –  Axel Sommerfeldt Jan 10 '12 at 11:24

You can define the command as:

\newcommand{\figref}[2][{}]{\hyperref[#2]{\figurename~\ref{#2}#1}}


Use it as \figref{label} for figures that contain only one plot, and use it as \figref[A]{label} for those figures for which you want the reference to be Figure 1A.

Hope this helps!

-
If you use the hyperref package you could replace \figurename~\ref by \autoref, so you don't need to have discrete macros for references to figures, tables, ... –  Axel Sommerfeldt Jan 10 '12 at 11:28