# Prevent wrapping of subfloat captions

Because the images in my \subfloats are smaller than their captions, captions are wrapped to the next line. How can I prevent it?

## Update

I was wrong assuming subfig is a popular package so, here is more info about my problem.

\usepackage{subfig}
% ...more latex here
\begin{figure}
\centering
\subfloat[A sufficiently long caption]{\includegraphics{bakan}‎} \qquad
\subfloat[Here goes another long caption]{\includegraphics{bakan}‎} \qquad
\subfloat[The captions are broken]{\includegraphics{bakan}‎}
\caption{Line breaks in subcaptions demo.}
\end{figure}


The code above produces:

When the images are small, subcaptions look ugly.

-
Complete, minimal working example, please? (I don't know anything about the subfloat package. Does it do anything other than change the counter used for captions?) – Harald Hanche-Olsen Dec 29 '10 at 15:48
@Harald: Confusingly enough, \subfloat comes from the subfig package. The subfloat package does not define anything called \subfloat and the subfig package does not define anything called \subfig. – TH. Dec 29 '10 at 16:17

## 3 Answers

You can put the image inside a \parbox with a width of your own demand. Since the subfig package will make the caption as wide as the content, this will help. An example (which uses \rule{1cm}{1cm} as placeholder for the small image):

\documentclass{article}
\usepackage{subfig}
\begin{document}
\begin{figure}
\subfloat[Blah blah blah blah blah blah\ldots]
{\rule{1cm}{1cm}}
\subfloat[Blah blah blah blah blah blah\ldots]
{\parbox{4cm}{\centering\rule{1cm}{1cm}}}
\end{figure}
\end{document}


As an alternative, one could use the optional width argument of \subcaptionbox offered by my subcaption package:

\documentclass{article}
\usepackage{caption,subcaption}
\begin{document}
\begin{figure}
\subcaptionbox{Blah blah blah blah blah blah\ldots}
{\rule{1cm}{1cm}}
\subcaptionbox{Blah blah blah blah blah blah\ldots}
[4cm]{\rule{1cm}{1cm}}
\end{figure}
\end{document}

-
I have used your subcaption package, it worked for me, I entered width argument. But before accepting your answer, I want to ask one more thing. Is there a way saying like 'whatever you do, do not add line breaks to sub captions'? – nimcap Dec 29 '10 at 21:01
@nimcap: Leo Liu gave an example of that using \mbox{}. – TH. Dec 29 '10 at 22:01
The problem with \mbox is that it will cause overfull hboxes since the width of the subfigure will not be altered. So an appropriate solution would be typesetting the subfigures with a width identical to the 'natural' width of the subcaption. But this would IMHO cause non-good-looking subfigures because the spacing between them would not be equal anymore. An ideal solutution would be having equal widths of the subfigures which corresponds to the natural width of the longest subcaption. But I doubt that programming (and using) such macros is worth the effort... – Axel Sommerfeldt Dec 30 '10 at 9:53

The subfig package has an option width that can be passed to \captionsetup to set the width of the caption, but it doesn't seem to actually set the entire \subfloat in a box of that width. I think the easiest thing to do is to use \makebox inside the \subfig to set the width to be what you want.

\documentclass{article}
\usepackage{subfig}
\usepackage{showframe} % This is just to show rules around the text area
\begin{document}
\begin{figure}
\noindent
\subfloat[Here is my caption that is longer than my
figure.]{\makebox[.45\textwidth]{\rule{1in}{1in}}}%
\hfill
\subfloat[Here is my caption that is longer than my
figure.]{\makebox[.45\textwidth]{\rule{1in}{1in}}}%
\caption{Here is my main caption.}
\end{figure}
\end{document}

-

A simple solution (but may not be the best one for all situations):

% in preamble
\usepackage{subfig}
\DeclareCaptionLabelSeparator{nobreakspace}{\nobreakspace}
\captionsetup[subfigure]{labelsep=nobreakspace}
% in text
\subfloat[\mbox{...}]{image}


And another one:

\DeclareCaptionFormat{oneline}{\mbox{#1#2#3}}
\captionsetup[subfigure]{format=oneline}
% in text
\subfloat[any thing]{image}

-
I think the former is preferable. Long captions should still be wrapped most of the time. – Leo Liu Dec 29 '10 at 16:30