Wednesday, April 13, 2011

Centering subfloats inside a box.

One would think centering a subfloat inside a box would be easy. Well it does not appear to be. I found many ways of doing this. And most have there shortcomings. First of all, I use subfig instead of subfigure, because from what I understand, subfigure is deprecated and I've had some weird bugs using subfigure.

So let's take a look at a canonical subfloat code (http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions)

\begin{figure}

\centering

\subfloat[A gull]{\label{fig:gull}\includegraphics[width=0.3\textwidth]{gull.jpg}}

\subfloat[A tiger]{\label{fig:tiger}\includegraphics[width=0.3\textwidth]{tiger.jpg}}

\subfloat[A mouse]{\label{fig:mouse}\includegraphics[width=0.3\textwidth]{mouse.jpg}}

\caption{Pictures of animals}

\label{fig:animals}

\end{figure}


Now I need to get a box around this. I could use the boxed style for the float, but this will make a box that is page width. I want the box to be around my content. Typically, for normal float you would use an \fbox, and this will work.

\begin{figure}

\centering

\fbox{

\subfloat[A gull]{\label{fig:gull}\includegraphics[width=0.3\textwidth]{gull.jpg}}

\subfloat[A tiger]{\label{fig:tiger}\includegraphics[width=0.3\textwidth]{tiger.jpg}}

\subfloat[A mouse]{\label{fig:mouse}\includegraphics[width=0.3\textwidth]{mouse.jpg}}

}

\caption{Pictures of animals}

\label{fig:animals}

\end{figure}


So why is this post not done, well it's all about short comings, this will only if you put all your subfloats on the same line. If you want a line break. That's not going to work.

\subfloat[A gull]{\label{fig:gull}\includegraphics[width=0.3\textwidth]{gull.jpg}} \\ %

\subfloat[A tiger]{\label{fig:tiger}\includegraphics[width=0.3\textwidth]{tiger.jpg}}

\subfloat[A mouse]{\label{fig:mouse}\includegraphics[width=0.3\textwidth]{mouse.jpg}}

}}


So we need another box that will need to support newlines. Like minibox
(http://ctan.mirror.rafal.ca/macros/latex/contrib/minibox/minibox.pdf)

Now this is much more stable.

\begin{figure}

\centering

\fbox{

\minibox[c]{

\subfloat[A gull]{\label{fig:gull}\includegraphics[width=0.3\textwidth]{gull.jpg}} \\ %

\subfloat[A tiger]{\label{fig:tiger}\includegraphics[width=0.3\textwidth]{tiger.jpg}}

\subfloat[A mouse]{\label{fig:mouse}\includegraphics[width=0.3\textwidth]{mouse.jpg}}

}}

\caption{Pictures of animals}

\label{fig:animals}

\end{figure}