如何在初學者表格的標題中使用斷行/多行?
\caption{this is my first table's captive caption}
我希望標題看起來像這樣:
這是我的第一次
表格的強制標題
如何?
\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}
編輯
嘗試去中心兩行標題:
\usepackage{caption}
(...)
\begin{table}
\captionsetup{singlelinecheck=false, justification=centering}
\begin{threeparttable}
\caption{this is my first\newline table's very captive caption}
……這次嘗試的結果看起來不太正確...如何正確執行?
編輯2
- 我怎麼能讓換行發生在兩個特定的單字(甚至字元)之間,但僅當且僅當無論如何,標題都會換行嗎?
這意味著,只要標題夠短以完全適合一行,換行符號就會「休眠」;盡快必須linebreak,斷行發生在預先定義的位置。
答案1
LaTeX 對標題使用兩次傳遞。在第一遍中,\hbox
如果結果適合一行,則將標題設為一行中,否則將其設定在多行中。在您的情況下,標題太短並且適合一行。
技巧:設定換行符並在標題中添加大量水平空間。在第一遍中,換行符被忽略,但水平空間確保標題不適合一行。標題設定為多行模式並執行換行,但下一行開頭的水平空格被丟棄(\hspace
沒有星星):
\documentclass{article}
\begin{document}
\begin{table}
\caption{this is my first\newline
\hspace{\linewidth}table's captive caption}
\end{table}
\end{document}
另一種選擇是包caption
,它提供了singlelinecheck
允許禁用檢查標題長度的第一遍的選項:
\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}
(此選項也可以在序言中全域設定。)
定心
以下使用內部tabular
(和預設singlelinecheck=true
)。Table 1:
計算表標籤的 with ( \settowidth
) 並予以考慮。
\caption
如果未使用可選參數 of ,則下列範例也會在本機重新定義\centeredmultilineincaption
以刪除tabular
圖表清單的 the 和換行符。
\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}
更新:僅當第一行較長時此方法才有效。
「有條件」換行
\\
和都\linebreak
可以使用。在單行模式下它們消失,因此應該在之前設定一個空格:first line \linebreak second line
,而不是first line\linebreak second line
。後者將變為first linesecond line
單行模式。這兩個宏都很脆弱。如果相同的換行符號也應該出現在表格清單中,則
\protect
需要:\caption{First line \protect\\second line}
可選參數
\caption
可以用於表列表:\caption[Short version]{First line \\second line}
或表格列表中的不同換行符:
\caption[Short\protect\\version]{First line \\second line}
以下範例定義\captionlinebreak
了
- 照顧好之前的空間,
- 是穩健的並且
- 可以為表格列表重新定義。
如果多行標題也應該居中,它會變得更難看,因為\@makecaption
需要重新定義,而且它的定義取決於類別和套件。該範例為article
沒有 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}
答案2
\centering
重新定義\\
可以正確工作,但不幸的是沒有重新定義\newline
。因此結合\centering
併\newline
給出奇怪的結果:
\documentclass{article}
\begin{document}
\parbox{\linewidth}{\centering
This is some text\newline And this is some text, too.}
\par\bigskip
\parbox{\linewidth}{\centering
This is some text\\And this is some text, too.}
\end{document}
(為什麼?LaTeX 中的錯誤?也許 David Carlisle 可以打開這裡的燈?)
\\
因此,最好在標題或其他應排版居中的內容中使用。順便說一句:使用該caption
套件時,排版的換行符\\
將自動替換為清單條目中的空格字符,因此通常不需要使用替代清單條目文字:
\documentclass{article}
\usepackage[justification=centering]{caption}
\begin{document}
\listoftables
\begin{table}
\caption{This is my first\\table's captive caption}
\end{table}
\end{document}
附錄“條件換行符”
使用\DeclareCaptionStyle
一個可以為“單行”標題和“長”標題定義不同的選項集。人們\DeclareCaptionOption
可以定義自己的選項來與套件一起使用caption
。
因此,作為範例,我定義了一個名為的新條件\ifcaptionlinebreak
和一個與套件一起使用的適當選項caption
,可以使用 和 進行linebreak=false
切換linebreak=true
:
\documentclass{article}
\usepackage{caption}
% New conditional \ifcaptionlinebreak
\newif\ifcaptionlinebreak
% New caption package option "linebreak" for toggeling \ifcaptionlinebreak
\DeclareCaptionOption{linebreak}{\csname captionlinebreak#1\endcsname}
% New command \captionlinebreak which either typesets a line break or a space,
% depending on \ifcaptionlinebreak
\newcommand\captionlinebreak{\ifcaptionlinebreak\\\else\space\fi}
% Own caption style which turns the linebreak into a space for short captions
% and into a line break for long captions
\DeclareCaptionStyle{mystyle}[linebreak=false]{linebreak=true}
\captionsetup{style=mystyle,justification=centering}
\begin{document}
\listoftables
\begin{table}[!hb]
\caption{This is my first\\table's captive caption}
\end{table}
\begin{table}[!hb]
\caption{This is my first\captionlinebreak table's captive caption}
\end{table}
\begin{table}[!hb]
\caption{This is my first\captionlinebreak table's captive caption.
This is my first table's captive caption}
\end{table}
\end{document}
附錄「條件換行」的附錄
我剛剛介紹了\ifsinglelinecaption
該caption
軟體包,因此從 CTAN 上發布的下一個版本開始,上面的範例文件可以簡化為:
\documentclass{article}
\usepackage[justification=centering]{caption}
\newcommand\captionlinebreak{\ifsinglelinecaption\space\else\\\fi}
\begin{document}
\listoftables
\begin{table}[!hb]
\caption{This is my first\\table's captive caption}
\end{table}
\begin{table}[!hb]
\caption{This is my first\captionlinebreak table's captive caption}
\end{table}
\begin{table}[!hb]
\caption{This is my first\captionlinebreak table's captive caption.
This is my first table's captive caption}
\end{table}
\end{document}
(住院患者可在以下網址取得最新版本:http://sourceforge.net/p/latex-caption/code/HEAD/tree/trunk/tex/)