初心者向け表のキャプションで改行/複数行を機能させるにはどうすればよいですか?

初心者向け表のキャプションで改行/複数行を機能させるにはどうすればよいですか?

初心者向け表のキャプションで改行/複数行を機能させるにはどうすればよいですか?

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

編集

しようとする中心2行の見出し:

\usepackage{caption}
(...)
\begin{table}
  \captionsetup{singlelinecheck=false, justification=centering}
\begin{threeparttable}
\caption{this is my first\newline table's very captive caption}

... この試みの結果は、あまり正しくないようです... どうすれば正しく実行できるでしょうか?


編集2

  • 特定の2つの単語(または文字)の間で改行するにはどうすればよいでしょうか。ただし、いずれにしてもキャプションは改行されるのでしょうか?

つまり、キャプションが1行に収まるほど短い間は改行は「休止状態」になりますが、しなければならない改行の場合、事前に定義された場所で改行が行われます。

答え1

LaTeX はキャプションに 2 つのパスを使用します。最初のパスでは、\hbox結果が 1 行に収まる場合はキャプションが 1 行に設定され、そうでない場合は複数行に設定されます。この場合、キャプションが短すぎて 1 行に収まりません。

トリック: 改行が設定され、キャプションに多くの水平スペースが追加されます。最初のパスでは改行は無視されますが、水平スペースによりキャプションが 1 行に収まりません。キャプションは複数行モードに設定され、改行が実行されますが、次の行の先頭の水平スペースは破棄されます (\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}

(オプションはプリアンブルでグローバルに設定することもできます。)

センタリング

以下では、 inner tabular(およびデフォルトのsinglelinecheck=true) を使用します。テーブル ラベルの with がTable 1:計算され ( \settowidth)、考慮されます。

オプションの引数 が\caption使用されない場合は、次の例でも がローカルに再定義され、図のリストの と改行が\centeredmultilineincaption削除されます。tabular

\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クラスに対して再定義します。articlecaption

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

ここに画像の説明を入力してください

付録「条件付き改行」

1 つを使用すると\DeclareCaptionStyle、「1 行」のキャプションと「長い」キャプションの異なるオプション セットを定義できます。\DeclareCaptionOption1 つを使用すると、パッケージで使用するための独自のオプションを定義できますcaption

そこで例として、 と呼ばれる新しい条件文と、で切り替えられるパッケージ\ifcaptionlinebreakで使用するための適切なオプションを定義します。captionlinebreak=falselinebreak=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/

関連情報