How to get line-breaking / multirow to work in the caption of my beginner's table?
\caption{this is my first table's captive caption}
I'd like the caption to look sth like this:
this is my first
table's captive caption
How?
\documentclass{article}
\usepackage{tabulary}
\usepackage{threeparttable}
\usepackage{array}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pbox}
\usepackage{lipsum}
\usepackage[utf8]{inputenc}\usepackage{textcomp}
\renewcommand{\arraystretch}{1.2}
\sisetup{round-mode=places,round-precision=1, add-decimal-zero=true, add-integer-zero=true, round-integer-to-decimal}
\begin{document}
\lipsum[1]
\begin{table}
\begin{threeparttable}
\caption{this is my first table's captive caption}
\begin{tabulary}{\textwidth}{@{}*{2}{L}*{6}{S[table-format=3.2]}@{}} \toprule
× & TOTALLY bla percentage of bla & {1000} & {2000} & {3000} &
\multicolumn{1}{c}{\begin{tabular}{@{}c@{}}Bonjour\tabularnewline monde!\end{tabular}} &
{5000} & {6000\tnote{1}}\\ \midrule
% × & TOTALLY bla percentage of bla & {1000} & {2000} & {3000} & \multirow{2}*{4000 apples and pears \\ and whatnot} & {5000} & {6000\tnote{1}}\\ \midrule
DDD 1 & 47.6 & 29.1 & 1.0 & 0.2 & 1.9 & 15.2 & 0.0\\
UUU & 24.8 & 10.8 & 6.4 & 0.0 & 3.2 & 4.5 & 0.0\\
× & × & × & × & × & × & × & ×\\
Unweighted average: & × & × & × & × & × & × & ×\\
BBB & 33.8 & 11.3 & 9.1 & 0.4 & 1.8 & 11.0 & 0.2\\
GGG & 32.9904 & 8.60325 & 9.3845 & 0.0495 & 1.43225 & 10.79525 & 0.119\\
DDD & 39.4545 & 9.8695 & 15.3365 & 0.6915 & 2.246 & 10.6705 & 0.5105\\ \bottomrule
\end{tabulary}
\begin{tablenotes}
\item [1] the first note ...
\end{tablenotes}
\end{threeparttable}
\end{table}
\lipsum[2]
\end{document}
Edit
trying to center the two-line headings:
\usepackage{caption}
(...)
\begin{table}
\captionsetup{singlelinecheck=false, justification=centering}
\begin{threeparttable}
\caption{this is my first\newline table's very captive caption}
… the result of this attempt doesn't look quite right … how to do it right?
Edit 2
- How could I make the linebreak happen between two particular words (or even characters), but only iff the caption would linebreak in any case?
Meaning, the linebreak would be "dormant" so long as the caption is short enough to fit entirely on one line; as soon as it would have to linebreak, the break occurs at the predefined place.
Best Answer
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):An alternative is package
caption
it provides optionsinglelinecheck
that allows to disable the first pass that checks the caption length:(The option can also be globally set in the preamble.)
Centering
The following uses an inner
tabular
(and the defaultsinglelinecheck=true
). The with of the table labelTable 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 thetabular
and the line break for the list of figures.Update: This method only works, if the first line is longer.
"Conditional" linebreak
\\
and\linebreak
can be used. In single line mode they vanish, thus there should be set a space before:first line \linebreak second line
, notfirst line\linebreak second line
. The latter would becomefirst 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:The optional argument of
\caption
can be used for the list of tables:or a different line break in the list of tables:
The following example defines
\captionlinebreak
thatIf 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 classarticle
without packagecaption
: