16

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.

1 Answer 1

17

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

5
  • While that works fine for my MWE, if you try to load hyperref with that (adding it in line 2 to the packages) would give you an error. But loading hyperref after your redefinition works.
    – Ronny
    Dec 19, 2012 at 10:52
  • I cannot confirm this, at least at my computer it works with hyperref added to line 2, too. (TeXlive 2012, up-to-date) Did you remember deleting the .aux file after adding hyperref?
    – user2574
    Dec 19, 2012 at 10:57
  • Oh, sorry, i forgot to delete those. Then it works for me, too (same TeXLive 2012)
    – Ronny
    Dec 19, 2012 at 11:04
  • +1 thanks for all of your wonderful packages :) could you take a look at Are there any subfloat package that works with tufte-latex? I'll delete this comment later, wasn't sure how else to contact you :)
    – cmhughes
    Dec 19, 2012 at 17:04
  • Until now no-one has questioned me for tufte-latex support, so I did not considered adding support for it to the caption package bundle yet. I just have added this to my TODO list, but first of all the next release v3.3 has to be finished... (BTW: Contacting me is quite easy since my e-mail address can be found in the caption package documentation.)
    – user2574
    Dec 21, 2012 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .