# Extra command in subfig package

I would like an extra command to be run in the captions using the \subfloat command from the subfig package. With normal captions, I just redefine \captionfont, but this doesn't work for captions of subfloats.

I have tried (the command I want to run is \sansmath:

\documentclass{article}
\usepackage{caption}
\renewcommand{\captionfont}{\footnotesize\sffamily}
\usepackage{sansmath}
\usepackage[textfont=sf]{subfig}
\let\bensubfloat\subfloat
\renewcommand{\subfloat}[2][]{\bensubfloat[\sansmath #1]{#2}}

\begin{document}
\begin{figure}
\subfloat[Subcaption $1+1$]{\framebox[3cm]{First}} \hfill
\subfloat[Subcaption $1+a$]{\framebox[3cm]{Second}}
\caption{Main caption}
\end{figure}
\end{document}


This gives the desired output, but also many errors.

-
Please do not re-define \captionfont. It's still possible (and working fine most times) for compatibility reasons to version 1.4 of the caption package, but it's obsolete. Please use \captionsetup{font=...} instead. – Axel Sommerfeldt Dec 22 '10 at 9:34

It seems to me like you want to have this:


\documentclass{article}
\usepackage{sansmath,subfig}
\DeclareCaptionFont{sansmath}{\sansmath}
\captionsetup{font={footnotesize,sf,sansmath}}
\captionsetup[subfloat]{font={footnotesize,sf,sansmath}}

\begin{document}

\begin{figure}
\subfloat[Subcaption $1+a$]{\framebox[3cm]{First}} \hfill
\subfloat[Subcaption $1+b$]{\framebox[3cm]{Second}}
\caption{Main caption $1+x$}
\end{figure}

\end{document}


This code uses \DeclareCaptionFont which is offered by the caption package for exactly this purpose - using own commands (like \sansmath) for caption or sub-caption font settings.

-
Thank you, this is a better solution. – Ben Dec 28 '10 at 13:20
\documentclass{article}
\usepackage[textfont=sf]{subfig}
\usepackage{caption}
\renewcommand{\captionfont}{\footnotesize\sffamily}
\usepackage{sansmath}
\makeatletter
\long\def\sf@@subfloat[#1]{%
\@ifnextchar [%  %] match left bracket
{\sf@@@subfloat{sub\@captype}[{\sansmath #1}]}%
{\sf@@@subfloat{sub\@captype}[\@empty{#1}][{\sansmath #1}]}}
\makeatother

\begin{document}

\begin{figure}
\subfloat[Subcaption $1+1$]{\framebox[3cm]{First}} \hfill
\subfloat[Subcaption $1+a$]{\framebox[3cm]{Second}}
\caption{Main caption}
\end{figure}

\end{document}

-
Thanks! This worked. What a wonderful site this is. – Ben Dec 21 '10 at 23:00
Maybe I'm too halfhearted but personally I don't like redefining internal macros if this isn't necessary at all. (And if the redefinition makes trouble I got a support e-mail where I have to guess what the heck the original purpose of the redefinition could be ;-)) The caption package offers the macro \DeclareCaptionFont for this purpose. (see my answer below) – Axel Sommerfeldt Dec 22 '10 at 9:38

Protect fragile commands within moving arguments:

\renewcommand{\subfloat}[2][]{\bensubfloat[\protect\sansmath #1]{#2}}

-
Unfortunately that didn't work. I have edited the question and added a minimal example. The \protect command messed things up (the subcaptions appear twice and the positioning is wrong). – Ben Dec 21 '10 at 18:29
It is necessary to fix the errors. However, \sansmath doesn't seem to work well here. I would try the subcaption package instead of subfig. – Stefan Kottwitz Dec 21 '10 at 18:53