[Tex/LaTex] Line break in table caption using beamer

beamercaptionsline-breakingtables

I would like to add a line break in a centered table caption with beamer. I can't seem to figure it out. Here is sample code:

\documentclass[xcolor=pdftex,svgnames,table]{beamer}
\setbeamertemplate{caption}[numbered]
\setbeamerfont{caption}{size=\scriptsize}
\usepackage{amsmath,amsthm, amssymb, latexsym}
\usetheme{Boadilla}

\begin{document}

\begin{frame}
\begin{table}
      \begin{tabular}{|c c|}\hline
      a & b  \\
      \hline
      1 & 2 \\
      \hline
      \end{tabular}
      \vspace{3mm}
      \caption{I would like to put a break \newline here in the caption.}
\end{table}
\end{frame}

\end{document}

This does not work (unless the caption stretches the whole page, in which case it becomes left aligned, which is not what I want). \\ produces an error.

If I tried to load the caption package I receive this error

! LaTeX Error: \@makecaption undefined.

This occurs even if I remove the beamer caption templates.

Best Answer

I would use a [t]op-aligned \parbox of fixed width. For example, using

\caption{\parbox[t]{4cm}{I would like to put a break here in the caption.}}

in your MWE produces

enter image description here

The same holds if you use a [t]op-aligned tabular:

\caption{%
  \begin{tabular}[t]{@{}l}
  I would like to put a break here \\ in the caption.
  \end{tabular}}

In the \parbox you specify the width, while in the tabular you specify the line break.

Finally, there is the varwidth package that will shrink to the natural width of a box if its contents is narrower than the width specified:

\usepackage{varwidth}% http://ctan.org/pkg/varwidth
%...
\caption{%
  \begin{varwidth}[t]{10cm}
  I would like to put a break here \\ in the caption.
  \end{varwidth}}

Although I've specified 10cm, the natural width (which now allows for line breaking using \\ since the contents is boxed) is narrower.

Related Question