3

I would like to style my subfigure references as Fig. 1(a) in running text and (Fig. 1a) when parenthetical. The subfigure labels are created using this code. (I would prefer to use subcaption like in this answer, but it causes a warning with the severely outdated journal class I'm stuck with.)

It is easy enough to get the first format with \renewcommand{\thesubfigure}{\thefigure(\alph{subfigure})}, and I tried to follow this answer for the second step, but no matter what I try, it does not seem to work.

\documentclass{article}

\usepackage{hyperref}
\usepackage{cleveref}

% Sub-figure references b/c class is broken. See link above.
\makeatletter
\newcounter{subfigure}[figure]
\renewcommand{\thesubfigure}{\thefigure(\alph{subfigure})}
\newcommand{\subfigure}{%
    \H@refstepcounter{subfigure}%
    \ifHy@hypertexnames%
            \hyper@makecurrent\@captype%
    \else%
            \global\let\@currentHref\hc@currentHref%
    \fi%
}
\makeatother

% Special format when within enclosing parentheses
\newcommand{\crefp}[1]{%
  \begingroup%
    \renewcommand{\thesubfigure}{\thefigure\alph{subfigure}}%
    (\cref{#1})%
  \endgroup%
}

\begin{document}

\begin{figure}
  \framebox[\textwidth]{Something}
  \caption{Caption}
  \label{fig:example}
  \subfigure\label{fig:example-1}
\end{figure}

Some text referencing the figure: \cref{fig:example},
and in parentheses: \crefp{fig:example}.
Some text referencing the subfigure: \cref{fig:example-1},
and in parentheses: \crefp{fig:example-1}.
\end{document}

The result is:

Some text referencing the figure: fig. 1, and in parentheses: (fig. 1). Some
text referencing the subfigure: fig. 1(a), and in parentheses: (fig. 1(a)).

I also tried messing with \p@subfigure instead or adding an extra macro around everything, but have so far not been successful.

(Note: I also could have left hyperref or cleveref out of the MWE, but since they do magical things with references and I'm go to use them anyway, I left them in.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.