68

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}
3

5 Answers 5

87

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.

9
  • 2
    I think the `\\` can be omitted? Mar 9, 2013 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) Mar 9, 2013 at 12:15
  • 2
    @David: Wow, great explanation, thank you very much! Mar 9, 2013 at 20:07
  • 3
    In an Overleaf document I was unable to include \\, but the hspace worked nicely. It used IEEEtran.cls (packages [utf8]inputenc, framed, booktabs, hyperref, wrapfig, tikz, todonotes, regexpatch, xcolor, listings). The error was: Argument of \Hy@tempa has an extra }. <inserted text> \par l.631 ....
    – drevicko
    Apr 7, 2017 at 21:17
  • Nice answer. I defined a new command like so \newcommand\nextline{\\*\hspace{\textwidth}} with the star version of \\* to prevent a new page. Reasonable?
    – PatrickT
    Sep 18, 2018 at 13:39
15

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.

1
  • I think this is the prettiest option. Jul 22, 2022 at 22:19
10

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}
4

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}
4
  • This probably was accompanied by a bad box...
    – Werner
    Mar 9, 2013 at 6:27
  • @Werner, what do you mean by "bad box" is the "Overfull \hbox" warning? How dangerous is it really?
    – Fran
    Mar 9, 2013 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, 2013 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, 2013 at 18:52
2

Alternatively, you can use \protect\linebreak, also without caption package.

For a short caption text, it works the same as \\\hspace{\textwidth}. However, it can be a better solution, if your caption text is longer and without the line break would occupy more than one line and you want it to be stretched to fill the line.

Example:

    \documentclass{article}
    \begin{document}
    \begin{figure}
    \caption[caption]{This is the first line of a caption text that \\\hspace{\textwidth} without the
      linebreak would occupy more than one line and it goes and it goes}
    \end{figure}
    \begin{figure}
    \caption{This is the first line of a caption text that \protect\linebreak without the
      linebreak would occupy more than one line and it goes and it goes}
    \end{figure}
    \end{document}

figure1 figure2

3
  • Welcome to tex.sx. Jan 25, 2020 at 15:11
  • 1
    This doesn't work for me. Do you use the... caption package in your first example?
    – PatrickT
    Jun 20, 2020 at 23:41
  • Thanks, I corrected the code for first example (\\\hspace.. instead of "\\"), so that it works without caption package.
    – hanna
    Jun 27, 2020 at 13:31

You must log in to answer this question.

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