[Tex/LaTex] Line breaks in subfig

line-breakingsubfloats

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?

Best Answer

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.

Related Question