[Tex/LaTex] Changing subfig \subfref format

cross-referencingformattingsubfloats

I'd like to know how to hack subfig in order to change the format of \subref. The current format, when subfig is initialized with listofformat=parens yields output like 4.5(b) . I'd like to add a space between the 4.5 and (b).

I know the documentation of subfig says that new formats can be defined with \DeclareSubrefFormat{hkeyword valuei}{hcodei} but I haven't managed to actually get hat to work.

Does anyone have an example of how the format can be changed?

Best Answer

The option listofformat controls the way subfloat numbers are typeset in the List of Figures/Tables and the actual format of the string produced by the \subref command. To change this format, you have to declare a new format using \DeclareCaptionListOfFormat and then use this new format in \captionsetup. In the second argument of the \DeclareCaptionListOfFormat command, #1 represents the float number and #2 represents the subfloat number. So to obtain the space between the float number and the subfloat number in the the LoF/LoT and when using \subref, you'll have to do something like the following:

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage[lofdepth]{subfig}

\DeclareCaptionListOfFormat{myparens}{#1~(#2)}
\captionsetup[subfloat]{listofformat=myparens,listofnumwidth=4em}

\begin{document}
\listoffigures

\chapter{Test Chapter}
\section{Test Section}

\subref{fig:sub1}
\begin{figure}
  \centering
  \subfloat[Text 1 In Toc][Text 1 in document\label{fig:sub1}]{\includegraphics[width=3cm]{name1}}\qquad
  \subfloat[Text 2 In Toc][Text 2 in document\label{fig:sub2}]{\includegraphics[width=3cm]{name2}}
  \caption{test}
\end{figure}


\end{document}

Here's the List of Figures obtained:

enter image description here

and a fragment of the document, showing the output produce by \subref:

enter image description here

If you only want to change the way \subref* (note the star) typesets the string associated to a subfloat, without affecting the entries in the Lof/Lot, you have to declare a new format using \DeclareSubrefFormat and then use this format in \captionsetup. In the second argument of \DeclareSubrefFormat you can use #1 (the string associated to the float), #2 (the string associated to the subfloat), #3 (the value of the float counter) y #4 (the value of the counter associated to the subfloat). A little example producing a space between the float and subfloat numbers:

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage[lofdepth]{subfig}

\DeclareSubrefFormat{myparens}{#1~(#2)}
\captionsetup[subfloat]{subrefformat=myparens}

\begin{document}
\listoffigures

\chapter{Test Chapter}
\section{Test Section}

\subref*{fig:sub1}
\begin{figure}
  \centering
  \subfloat[Text 1 In Toc][Text 1 in document\label{fig:sub1}]{\includegraphics[width=3cm]{name1}}\qquad
  \subfloat[Text 2 In Toc][Text 2 in document\label{fig:sub2}]{\includegraphics[width=3cm]{name2}}
  \caption{test}
\end{figure}

\end{document}

Now the LoF looks like this:

enter image description here

and the fragment showing the output of \subref*:

enter image description here

Related Question