How to add line break to caption without using caption package
\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
.
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:
\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.
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}