LaTeX uses two passes for the caption. In the first pass the caption is set in an \hbox
if the result fits in one line, the caption is set in one line, otherwise it is set in several lines. In your case the caption is too short and fit in one line.
A trick: A line break is set and much horizontal space is added to the caption. In the first pass the line break is ignored, but the horizontal space ensures the caption does not fit in one line. The the caption is set in multi-line mode and the line break is executed, but the horizontal space at the begin of the next line is discarded (\hspace
without star):
\documentclass{article}
\begin{document}
\begin{table}
\caption{this is my first\newline
\hspace{\linewidth}table's captive caption}
\end{table}
\end{document}
An alternative is package caption
it provides option singlelinecheck
that allows to disable the first pass that checks the caption length:
\documentclass{article}
\usepackage{caption}
\begin{document}
\begin{table}
\captionsetup{singlelinecheck=false}
\caption{this is my first\newline
table's captive caption}
\end{table}
\end{document}
(The option can also be globally set in the preamble.)
Centering
The following uses an inner tabular
(and the default singlelinecheck=true
).
The with of the table label Table 1:
is calculated (\settowidth
) and taken into account.
If the optional argument of \caption
is not used, the following example also locally redefines \centeredmultilineincaption
to get rid of the tabular
and the line break for the list of figures.
\documentclass{article}
\makeatletter
\newlength{\@captionlabelwidth}
\DeclareRobustCommand*{\centeredmultilineincaption}[1]{%
\settowidth{\@captionlabelwidth}{%
\@nameuse{fnum@\@captype}: %
}%
\begin{tabular}[t]{@{\hspace{-\@captionlabelwidth}}c@{}}%
\hspace{\@captionlabelwidth}\ignorespaces
#1%
\end{tabular}%
}
\begin{document}
\begingroup
\renewcommand*{\centeredmultilineincaption}[1]{%
\begingroup
\let\tabularnewline\space
#1%
\endgroup
}%
\listoftables
\endgroup
\begin{table}
\caption{%
\centeredmultilineincaption{%
this is my first\tabularnewline
table's captive caption
}%
}
\end{table}
\end{document}
Update: This method only works, if the first line is longer.
"Conditional" linebreak
- Both
\\
and \linebreak
can be used. In single line mode they vanish,
thus there should be set a space before: first line \linebreak second line
, not first line\linebreak second line
. The latter would become first linesecond line
in single-line mode.
Both macros are fragile. If the same line break also should occur in the list of tables, then \protect
is needed:
\caption{First line \protect\\second line}
The optional argument of \caption
can be used for the list of tables:
\caption[Short version]{First line \\second line}
or a different line break in the list of tables:
\caption[Short\protect\\version]{First line \\second line}
The following example defines \captionlinebreak
that
- takes care for the space before,
- is robust and
- can be redefined for the list of tables.
If the multi-line caption should also be centered, it becomes more ugly, because \@makecaption
needs to be redefined and its definition depends on the class and packages. The example redefines it for the class article
without package caption
:
\documentclass[a5paper]{article}
\usepackage{varwidth}
\DeclareRobustCommand*{\captionlinebreak}{%
\leavevmode\unskip\space % one space before
\\%
}
\makeatletter
\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
\centerline{%
\begin{varwidth}{\hsize}%
#1: #2%
\end{varwidth}%
}%
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip
}
\makeatother
\begin{document}
\begingroup
\let\captionlinebreak\relax
\listoftables
\endgroup
\begin{table}
\centering
\caption{Lorem ipsum\protect\captionlinebreak
dolor sit amet, consetetur sadipscing elitr, \dots}
\end{table}
\begin{table}
\centering
\caption{Lorem ipsum\captionlinebreak
dolor sit amet, \dots}
\end{table}
\end{document}
You have defined your subfigure
environments to have a width of .3\textwidth
, but your images are wider than that, so they will stick out on the right side. This is also the reason the captions are narrow, as they have the same width as the subfigure
s. So to fix this, just increase the width. You can use \columnwidth
to make them as wide as the columns.
See the below code for an example, where I also added an \fbox
around the each subfigure
, so their boundaries are obvious.
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
% this is wide enough
\fbox{\begin{subfigure}[b]{\textwidth}
\centering
\includegraphics[width=7cm,clip=false]{example-image-a}
\caption{Some long caption some long caption some long caption}
\end{subfigure}}
% this has a too narrow subfigure
\fbox{\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[width=7cm,clip=false]{example-image-b}
\caption{Some long caption some long caption some long caption}
\end{subfigure}}
\caption{Label predictions using cross-correlation-, GTW-, and LSTM-based classifiers}
\end{figure}
\end{document}
Best Answer
Remove
\mbox
the only thing it is doing is preventing the linebreak.