[Tex/LaTex] custom sub-caption and refs format for subfigures

captionscross-referencingfloatssubcaptionsubfloats

I've just read questions:

But i still haven't found answer.

Minimal example:

\documentclass{article}
\usepackage{caption}
\usepackage{subfig}
\usepackage{hyperref}

\begin{document}
\begin{figure}
 \centering
  \subfloat[subfigure A]{\rule{100pt}{50pt}
   \label{fig:subfig-a}
  }
  \subfloat[subfigure B]{\rule{100pt}{50pt}
   \label{fig:subfig-b}
  }
  \subfloat[subfigure C]{\rule{100pt}{50pt}
   \label{fig:subfig-c}
  }
 \caption{Subfigures:}
 \label{fig:figures}
\end{figure}

See Figures~\ref{fig:subfig-a}, \ref{fig:subfig-b} and~\ref{fig:subfig-c}.
\end{document}

Output looks like:
default captions

What i need:

  1. Subcaptions should be a), b), c) without caption text instead of (a), (b) and (c) with caption text.
  2. Refs to subfigures should be 1,\,a instead 1a.
  3. Figure caption should consist of prefix (Fig.~1.), separation space, general caption (Subfigures:) and sub-captions of subfigures (for example, (a) --- subfigure A; (b) --- subfigure B; (c) --- subfigure C).

What i tried:

Set caption

\captionsetup[figure]{labelsep=period,justification=centering,singlelinecheck=off}

Set ref format

\makeatletter
\def\thesubfigure{\textit{\alph{subfigure}}}
\providecommand\thefigsubsep{,\,}
\def\p@subfigure{\@nameuse{thefigure}\thefigsubsep}
\makeatother

So i've got:

\documentclass{article}
\usepackage{caption}
\usepackage{subfig}
\usepackage{hyperref}

\captionsetup[figure]{labelsep=period,justification=centering,singlelinecheck=off}

\makeatletter
\def\thesubfigure{\textit{\alph{subfigure}}}
\providecommand\thefigsubsep{,\,}
\def\p@subfigure{\@nameuse{thefigure}\thefigsubsep}
\makeatother

\begin{document}
\begin{figure}
 \centering
  \subfloat[subfigure A]{\rule{100pt}{50pt}
   \label{fig:subfig-a}
  }
  \subfloat[subfigure B]{\rule{100pt}{50pt}
   \label{fig:subfig-b}
  }
  \subfloat[subfigure C]{\rule{100pt}{50pt}
   \label{fig:subfig-c}
  }
 \caption{Subfigures:}
 \label{fig:figures}
\end{figure}

See Figures~\ref{fig:subfig-a}, \ref{fig:subfig-b} and~\ref{fig:subfig-c}.
\end{document}

It looks like that:
custom captions

It is still needed to correct format of sub-captions and main caption.

Also, why there are different spaces between subfigures?

Best Answer

I don't think using three different formats for the numbering of subfigures is good. Anyway, here it is:

\documentclass{article}
\usepackage{caption}
\captionsetup[figure]{labelsep=period,justification=centering,singlelinecheck=off}
\DeclareCaptionLabelFormat{rightparen}{#2)}
\usepackage{subfig}
\captionsetup[subfloat]{labelformat=rightparen}
\showcaptionsetup[uniq]{subfloat}
\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \g_akj_subfloat_seq
\NewDocumentCommand{\xsubfloat}{ m m }
 {
  \akj_subfloat:nn { #1 } { #2 }
 }
\NewDocumentCommand{\makexcaption}{o}
 {
  \caption{\IfNoValueF{#1}{#1~}Subfigures:~%
  \seq_use:Nnnn \g_akj_subfloat_seq {;~} {;~} {;~}}
 }
\cs_new_protected:Npn \akj_subfloat:nn #1 #2
 {
  % initialize
  \int_compare:nT { \value{subfigure} = 0 }
   { \seq_gclear:N \g_akj_subfloat_seq }
  % do the subfloat
  \subfloat[]{#2}
  % store the subcaption
  \seq_gput_right:Nx \g_akj_subfloat_seq { (\exp_not:N\textit{\alph{subfigure}}) ~ \exp_not:n { #1 } }
 }
\ExplSyntaxOff
\renewcommand{\thesubfigure}{\textit{\alph{subfigure}}}
\makeatletter
\def\p@subfigure{\thefigure\,}
\makeatother

\begin{document}
\begin{figure}
\centering
\xsubfloat{subfigure A}{%
  \rule{100pt}{50pt}%
  \label{fig:subfig-a}%
}
\xsubfloat{subfigure B}{%
  \rule{100pt}{50pt}%
  \label{fig:subfig-b}%
}
\xsubfloat{subfigure C}{%
  \rule{100pt}{50pt}%
  \label{fig:subfig-c}%
}
\makexcaption\label{fig:figures}
\end{figure}

See Figures~\ref{fig:subfig-a}, \ref{fig:subfig-b} and~\ref{fig:subfig-c}.
\end{document}

The \makexcaption command has also an optional argument for text going before the list of subcaptions, so

\makexcaption[Some text.]

would produce

Figure 1. Some text. Subfigures: (a) subfigure A; (b) subfigure B; (c) subfigure C

Beware of unprotected end-of-lines in the arguments of \subfloat, that are the cause of your spurious spaces.

enter image description here

Related Question