11

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?

0

2 Answers 2

9

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.

2

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}
1
  • This works, but I want to avoid manually specifying a width.
    – Adam Crume
    Commented Oct 13, 2011 at 2:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .