1

I have a figure with 6 subfloat figures.

\begin{figure}
    \centering
    \subfloat[]{\includegraphics{fig1}\label{fig1}} \hfill
    \subfloat[]{\includegraphics{fig2}\label{fig2}} \hfill
    \subfloat[]{\includegraphics{fig3}\label{fig3}} \hfill
    \subfloat[]{\includegraphics{fig4}\label{fig4}} \hfill
    \subfloat[]{\includegraphics{fig5}\label{fig5}} \hfill
    \subfloat[]{\includegraphics{fig6}\label{fig6}} \hfill
    \caption{Analysis of color variation}
    \label{fig_main}
\end{figure}

I am trying to reference subfigures in figure caption and in text. Currently, i am using \ref to reference figures. I need two styles of references, one separated by hyphen and one separated by comma.

I need the caption of the figure as: Analysis of color variation. (a-c) represent color. (d-f) represent contrast, and (a-c) represent gamma.

In text, I need to reference ...as shown in Fig. 12(a, d), (b, e) and (c, f) respectively... Fig. 13(a) shows...

Any idea how to achieve this? Any help will be highly appreciated.

1
  • 2
    Please do not post such fragments only. You may have a look into cleveref which will provide customized cross-referencing formats
    – user31729
    Commented Dec 27, 2017 at 12:26

1 Answer 1

2

This can be accomplished with cleveref.

\documentclass{article}
\usepackage{xparse}
\usepackage{subfig}
\usepackage{cleveref}

\crefformat{subfigure}{#2\onlyletter{#1}#3}
\crefrangeformat{subfigure}{(#3\onlyletter{#1}#4--#5\onlyletter{#2}#6)}
\crefmultiformat{subfigure}
  {(#2\onlyletter{#1}#3}
  {,~#2\onlyletter{#1}#3)}
  {,~#2\onlyletter{#1}#3)}
  {,~#2\onlyletter{#1}#3)}


\ExplSyntaxOn
\NewDocumentCommand{\onlyletter}{m}
 {
  \tl_set:Nx \l_tmpa_tl { #1 }
  \tl_item:Nn \l_tmpa_tl { -1 }
 }
\ExplSyntaxOff

\newcommand{\includegraphics}[1]{{\Huge #1}} % just for the example

\begin{document}

As shown in \cref{fig_main} \cref{fig1,fig4}, \cref{fig2,fig5}, and
\cref{fig3,fig6} respectively.

\begin{figure}
\centering

\subfloat[]{\includegraphics{fig1}\label{fig1}}\hfill
\subfloat[]{\includegraphics{fig2}\label{fig2}}\hfill
\subfloat[]{\includegraphics{fig3}\label{fig3}}\hfill
\subfloat[]{\includegraphics{fig4}\label{fig4}}\hfill
\subfloat[]{\includegraphics{fig5}\label{fig5}}\hfill
\subfloat[]{\includegraphics{fig6}\label{fig6}}
\caption[Analysis of color variation]{%
  Analysis of color variation;
  \cref{fig1,fig2,fig3} represent color,
  \cref{fig4,fig5,fig6} represent contrast,
  and \cref{fig1,fig2,fig3} represent gamma.%
}
\label{fig_main}

\end{figure}

\end{document}

The command \includegraphics has been given a suitable definition to emulate printing figures I don't have; you'll remove this definition and use graphicx, of course.

The main problem is to get subfigures referenced only with the letter, which requires the \onlyletter trick. The rest is standard cleveref.

enter image description here

4
  • Thank you very much @egreg, you saved the day. It works like a charm... Thanks dearie :)
    – Raja Ayaz
    Commented Dec 27, 2017 at 13:42
  • +1. The \onlyletter trick is really neat. The same result could have been achieved, arguably somewhat more simply, by running \makeatletter \renewcommand\p@subfigure{} \makeatother, i.e., by un-setting the definition of \p@subfigure. Of course, the cleveref-related setup would still be needed.
    – Mico
    Commented Dec 27, 2017 at 17:30
  • @Mico You're right, but perhaps the whole reference is needed in other situations
    – egreg
    Commented Dec 27, 2017 at 17:53
  • @egreg exactly, that was the case. I had too many whole references besides this in the text.
    – Raja Ayaz
    Commented Dec 27, 2017 at 18:16

You must log in to answer this question.

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