Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Oct 13 '11 at 1:19

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

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer

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}
share|improve this answer
    
This works, but I want to avoid manually specifying a width. –  Adam Crume Oct 13 '11 at 2:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.