[Tex/LaTex] Change format of reference to a subfigure

captionscross-referencingsubfloats

In my current document, I'm working with many subfigures. Normally, I like to refer to them using a style like Figure 1 b) where the space should be a halfspace, hence \, in the code. That's nice not only to reference subfigure but consistent to my reference to a part of an example. I would like to use something like \ref{subfig:p1} to get the just described output. But that way there is no space between the number and the alphanumberic letter.

Here's an MWE

\documentclass[4apaper]{scrartcl}
\usepackage{caption,subcaption,graphicx}
\captionsetup{format=hang,labelsep=space,indention=-2cm,labelfont=bf,width=.9\textwidth,skip=.5\baselineskip}
\captionsetup[sub]{labelfont=bf, labelsep=period,labelformat=simple, subrefformat=brace}

\begin{document}
\begin{figure}[t]
    \begin{subfigure}[b]{.49\textwidth}\centering
 %      \includegraphics[width=.9\textwidth]{myPicture}
        \rule{2cm}{2cm}.
        \caption{SubText 1}\label{subfig:p1}
    \end{subfigure}
    \begin{subfigure}[b]{.49\textwidth}\centering
        \rule{2cm}{2cm}.
        \caption{SubText 2}\label{subfig:p2}
    \end{subfigure}
    \caption{Description referencing to \subref{subfig:p1} and
  \subref{subfig:p2}}\label{fig:1}
  \end{figure}
  I usually refer to example 1\,c) somewhere else in a certain way leaving some space,
  but that's not possible just using \ref{subfig:p1}; I don't want to use
  \ref{fig:1}\,\subref{subfig:p1}, but that's quite longish.
\end{document}

enter image description here

How can I change the \ref in \ref{subfig:p1) to put a space between the figure and subfigure reference? My favourite would b something using \captionsetup if available.

Best Answer

Getting \, between the figure and the sub-figure reference part is quite easy, one can redefine \p@subfigure which usually only prepends the figure part, i.e. is usually defined as

\newcommand\p@subfigure{\thefigure}

and can be altered to

\renewcommand\p@subfigure{\thefigure\,}

(See also section "References" in the subcaption package documentation.)

But how to get the single closing parenthesis in references but not in the sub-caption label? One possibility would be adding a closing parens to the definition of \thesubfigure, like shown in section "References":

\renewcommand\thesubfigure{\alph{subfigure})}

But this will add the closing parens to the sub-figure label as well. So it's time for the clever part, dropping the parens in the sub-figure label. While the subcaption documentation only shows how to add stuff to the printed label, it does not actually show how to drop stuff. Well, one could try to build tricky code using macros like \@gobble but I think the most straight-ahead way is defining a new label format which does not use the given arguments (#1 and #2) at all but builds up the label right from the stretch:

\DeclareCaptionLabelFormat{mysublabelfmt}{\alph{sub\@captype}}

(Here we use the fact that the actual main floating environment name is stored in \@captype, e.g. figure inside a figure environment.)

In total we have:

\documentclass[4apaper]{scrartcl}
\usepackage{caption,subcaption,graphicx}
\captionsetup{format=hang,labelsep=space,indention=-2cm,labelfont=bf,width=.9\textwidth,skip=.5\baselineskip}
\captionsetup[sub]{labelfont=bf,labelsep=period,labelformat=mysublabelfmt}

\makeatletter
\renewcommand\p@subfigure{\thefigure\,}
\renewcommand\thesubfigure{\alph{subfigure})}
% If desired for table as well:
% \renewcommand\p@subtable{\thetable\,}
% \renewcommand\thesubtable{\alph{subtable})}
\DeclareCaptionLabelFormat{mysublabelfmt}{\alph{sub\@captype}}
\makeatother

\begin{document}
\begin{figure}[t]
    \begin{subfigure}[b]{.49\textwidth}\centering
 %      \includegraphics[width=.9\textwidth]{myPicture}
        \rule{2cm}{2cm}.
        \caption{SubText 1}\label{subfig:p1}
    \end{subfigure}
    \begin{subfigure}[b]{.49\textwidth}\centering
        \rule{2cm}{2cm}.
        \caption{SubText 2}\label{subfig:p2}
    \end{subfigure}
    \caption{Description referencing to \subref{subfig:p1} and
  \subref{subfig:p2}}\label{fig:1}
  \end{figure}
  I usually refer to example 1\,c) somewhere else in a certain way leaving some space,
  but that's not possible just using \ref{subfig:p1}; I don't want to use
  \ref{fig:1}\,\subref{subfig:p1}, but that's quite longish.
\end{document}

(Please note that I dropped the subrefformat setting additionally for not getting two closing parens when using \subref)

result

Related Question