# Centering subfloat caption with subfig package

A footnote on page 12 of the subfig documentation says in part:

Note that the ‘width’ may be larger than the width of the sub-float body, or equivalently, the ‘width’ may be negative to provide more room for a caption if the sub-float body is small. The box holding the caption is centered relative to the sub-float body.

I must not be understanding something.

First, by looking at Figure 91 on page 35, don't they mean to say that you can set the margin to a negative value in order to get a caption that is wider than (and centered relative to) the sub-float body?

Even if this is right, I can't get a simple example to work. See my MWE below.

What am I doing wrong?

MWE:

\documentclass{article}
\usepackage{caption}
\usepackage{subfig}
\begin{document}
\begin{figure}[ht]
\centering
\subfloat[very long subfloat caption]{
\framebox[3cm][c]{
subfloat body
}
}
\end{figure}
\begin{figure}[ht]
\centering
\captionsetup[subfloat]{width=-2cm}
\subfloat[very very long subfloat caption]{
\framebox[3cm][c]{
subfloat body
}
}
\end{figure}
\begin{figure}[ht]
\centering
\captionsetup[subfloat]{margin=-2cm}
\subfloat[very very long subfloat caption]{
\framebox[3cm][c]{
subfloat body
}
}
\end{figure}
\end{document}


• Better using subcaption, anyway. – egreg Apr 17 '15 at 23:22

You can fool subfig letting it think that the subfloat is, e.g., \textwidth wide, by inserting the subfloat body in a box of that width.

Example:

\documentclass{article}
\usepackage{subfig}
\begin{document}
\begin{figure}[ht]
\centering
\subfloat[very very very very very long subfloat caption]{%
\makebox[\textwidth]{\framebox[3cm][c]{subfloat body}}%
}
\end{figure}
\end{document}


Output:

• Yes, thanks. That has been my workaround for a few years now. I am specifically wondering about the question I asked. – Tyson Williams Apr 18 '15 at 11:53
• @TysonWilliams I don't think it can be done any better... – karlkoeller Apr 18 '15 at 11:58
• Yes, I agree that the output of your workaround looks perfect. That is why I also have been using that workaround for years now. I really am just trying to understand this footnote in the subfig documentation. – Tyson Williams Apr 18 '15 at 12:01