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 wish to add a line break to a caption, but I would wish to avoid adding of caption package. Is it possible?

\documentclass{article}
\usepackage[format=plain, 
justification=raggedright,singlelinecheck=false]{caption}
\begin{document}
\begin{figure}
\caption{This is the caption\\ This is the second line}
\end{figure}
\end{document}
share|improve this question
1  
Why don't you want to use caption package? –  hpesoj626 Mar 9 '13 at 3:31
1  
@hpesoj626 it's related to this question:tex.stackexchange.com/questions/101424/… –  william007 Mar 9 '13 at 3:37

4 Answers 4

up vote 12 down vote accepted

enter image description here

\documentclass{article}
\begin{document}
\begin{figure}
\caption[caption]{This is the caption\\\hspace{\textwidth}This is the second line}
\end{figure}

\end{document}

The reason why \\ does not work originally is that the caption is small enough to fit on one line. The default caption setting first sets the caption in an mbox and if that is less than the line width that box is centred. Otherwise that box is discarded and the caption is re-set in a parbox. On the first setting \\ does nothing as it is always a no-op in such a box, on the second setting (if it is used) then \\ starts a new line. So all you have to do is make sure that the first setting is not used, as the space at the start of a line is discarded, it doesn't really matter how big the space is, so long as the total natural width of the whole caption is bigger than \textwidth.

share|improve this answer
    
I think the `\\` can be omitted? –  Yiannis Lazarides Mar 9 '13 at 12:10
    
@YiannisLazarides no see comment to Hendrick (it sort of works in this case but you get an overfull box) but in general you need \\ as the setting is not raggedright by default) –  David Carlisle Mar 9 '13 at 12:15
1  
@David: Wow, great explanation, thank you very much! –  Hendrik Vogt Mar 9 '13 at 20:07

The problem with using \\ in \caption{..} (a single mandatory argument) is that this defaults to \caption[..]{..} (optional + mandatory), and \\ is problematic in the optional argument. If you specify an alternative non-\\ optional argument, then it's okay, but \\ is made "useless" or has no effect. You could use a tabular to set the argument:

enter image description here

\documentclass{article}
\begin{document}
\begin{figure}
  \caption[This is the caption; This is the second line]
    {\tabular[t]{@{}l@{}}This is the caption \\ This is the second line\endtabular}
\end{figure}
\end{document}

Of course, if you don't need the optional argument (since you might not be using a \listoftables), just use an empty optional argument. Without more context of the requirement, I'm not sure how useful it might be.

share|improve this answer

One alternative is a minipage with \\ in the long caption. Note that in this way, the sort caption (between []) is not longer optional but as mandatory argument. If you want also a line break in the list of figures, simply use a \newline in the sort caption:

Edit: As Werner noted, the width of minipage must be less than \linewidth to avoid problems (now corrected in the MWE) or use varwidth package)

MWE

\documentclass{article}
\begin{document}
\listoffigures
\begin{figure}
  \centering
  \caption
  [This is the caption \newline This is the second line]
  {\begin{minipage}[t]{.8\linewidth}This is the caption \\This is the second line \end{minipage}}
\end{figure}
\end{document}
share|improve this answer
    
This probably was accompanied by a bad box... –  Werner Mar 9 '13 at 6:27
    
@Werner, what do you mean by "bad box" is the "Overfull \hbox" warning? How dangerous is it really? –  Fran Mar 9 '13 at 7:13
    
Since you're setting the contents in a box, if the figure name + number + first line of caption > \textwidth, it will spill over the margin visually. While that may never happen, the box will necessarily push the caption to the left (always) because of its size. In this instance it would be better to use varwidth. –  Werner Mar 9 '13 at 15:34
    
As usual you're right. Funnily, I really did a first draft with varwidth, but then I thought that if he wanted a solution without caption package, he would not want a solution using another package and tested that original minipage worked also in the MWE. –  Fran Mar 9 '13 at 18:52

Not sure why, but a \protect\linebreak works in the boxhandler package, but not in the \caption command.

\documentclass{article}
\usepackage{boxhandler}
\begin{document}

\bxfigure{Caption goes \protect\linebreak here}
{\fbox{Goodbye, very, very, very cruel world}}

\begin{figure}
\begin{center}
\fbox{Goodbye, very, very, very cruel world}
\caption{Caption goes \protect\linebreak here}
\end{center}
\end{figure}

\end{document}
share|improve this answer

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.