2

When I use \subcaption{... \label{myfig}} in memoir, and then reference it, the result is something like "Figure 1(a)". But I would like to customize the reference by removing the parentheses, thus making it appear as "Figure 1a".

How do I do that in memoir? I can't find anything in the manual.

gablin
  • 17,006

2 Answers2

6

It is possible to do this using some string manipulation from the xstring package. Using a combination of \StrBefore and \StrBetween, the "parent" and subfigure numbers can be extracted and printed as in your requirement. However, this would require some more work if needed with hyperref.

Below is a new macro \subfigref{<label>} that should be used to obtain this output, where <label> is the reference label used in the subfigure caption. It should also work for other subfloats.

enter image description here

\documentclass{memoir}% http://ctan.org/pkg/memoir
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{xstring}% http://ctan.org/pkg/xstring
\usepackage{xspace}% http://ctan.org/pkg/xspace
\newsubfloat{figure}% Allow subfigures using \subbottom/\subtop
\newcommand*{\subfigref}[1]{\StrBefore{\ref{#1}}{(}\StrBetween{\ref{#1}}{(}{)}\xspace}%
\begin{document}
\begin{figure}[ht]
  \centering
  \subbottom[This is a subfigure\label{fig:label:a}]{\includegraphics{figure1}} \qquad
  \subbottom[This is a subfigure\label{fig:label:b}]{\includegraphics{figure2}}
  \caption{This is a caption} \label{fig:label}
\end{figure}
See Figure~\ref{fig:label}. It has subfigures~\subfigref{fig:label:a} and~\subfigref{fig:label:b}.
\end{document}
Werner
  • 603,163
4

Inspired by Werner's, here's a definition that works with hyperref

\documentclass{memoir}
\usepackage[demo]{graphicx}
\newsubfloat{figure}% Allow subfigures using \subbottom/\subtop

\usepackage{refcount}
\newcommand\sref[1]{\edef\next{\getrefnumber{#1}}%
  \begingroup\edef\x{\endgroup
    \noexpand\hyperref[#1]{\expandafter\stripparens\next()\nnil}}\x}

\def\stripparens#1(#2)#3\nnil{\if\relax\detokenize{#2}\relax??\else#1#2\fi}

\usepackage{hyperref}

\begin{document}
\begin{figure}[ht]
  \centering
  \subbottom[This is a subfigure\label{fig:label:a}]{\includegraphics{figure1}} \qquad
  \subbottom[This is a subfigure\label{fig:label:b}]{\includegraphics{figure2}}
  \caption{This is a caption} \label{fig:label}
\end{figure}
See Figure~\ref{fig:label}. It has subfigures~\sref{fig:label:a} and~\sref{fig:label:b}.

\end{document}

If hyperref is not loaded, then change the definition of \sref into

\newcommand\sref[1]{\edef\next{\getrefnumber{#1}}%
  \expandafter\stripparens\next()\nnil}

It's a kludge and requires \sref instead of \ref, but it seems to work. When memoir will be adapted so that the aspect of references to subfloats is customizable, it will be necessary only to say

\let\sref\ref
egreg
  • 1,121,712