# Line breaks in subfig

I'm trying to put some text in a subfloat using the subfig package. (It's supposed to be part of the figure, not the caption.) I can't figure out how to add a line break. When I try this code:

\documentclass{article}
\usepackage{subfig}

\begin{document}

\begin{figure}[h]
\centering
\subfloat[a]{A\\\texttt{This is a test}}
\subfloat[b]{B}
\end{figure}

\end{document}


I get the error: Something's wrong--perhaps a missing \item.

I tried changing the \\ to \linebreak, \newline, and \par. They all either do nothing or give an error. I also tried wrapping the text in an mbox, and I tried putting the line break inside the texttt. Nothing works. How do I do this?

-

## migrated from stackoverflow.comOct 13 '11 at 1:19

This question came from our site for professional and enthusiast programmers.

If you enclose your text in \shortstack{...}, you'll get the line breaks:

\documentclass{article}
\usepackage{subfig}

\begin{document}
\begin{figure}[h]
\centering
\subfloat[a]{\shortstack{A\\\texttt{This is a test}}}
\subfloat[b]{B}
\end{figure}

\end{document}


The alignment can be adjusted using the optional argument. \shortstack[l]{...} will align your text on the left, \shortstack[r]{...} on the right.

-

I don't think \mbox allows line breaks. You could put the text in a \parbox which does allow like breaks as in:

\documentclass{article}
\usepackage{subfig}

\begin{document}
\begin{figure}[h]
\centering
\subfloat[a]{\parbox{2.0in}{A\\\texttt{This is a test}}}
\subfloat[b]{B}
\end{figure}
\end{document}

-
This works, but I want to avoid manually specifying a width. –  Adam Crume Oct 13 '11 at 2:39