表の中央にある表のキャプション

表の中央にある表のキャプション

なんとか表を作成しましたが、キャプション (またはその後に追加したテキスト) が表の中央にあり、表が 2 行目で終了したように見えます。同様の質問が見つからなかったので、自分で質問することにしました。

表の後にキャプションとテキストを追加しました。どちらも、表が2行目で終了したかのように動作します。

キャプションやその前後のテキストがなければ、表はほぼ私が望んでいた通りの見た目になります。

キャプションなしの表は次のようになります

\documentclass{report}
\usepackage{multirow}


\begin{document}

This is some text and here is a reference to Table \ref{table:mytable}. 


\begin{table} [h]  
       \centering
       \begin{tabular} {c c c} \\
        \textbf{Column A} & \textbf{Column B} & \textbf{Column C} \\ 

    \hline

    \multirow{3}{*}{\textbf{Test1}} 
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}} \\ 

    \multirow{6}{*}{\textbf{Test2}} 
    & \multicolumn{2}{c}{ \multirow{6}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}} \\

    \multirow{9}{*}{\textbf{Test3}}
    & \multirow{9}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}
    & \multirow{9}{*}{\shortstack[l]{line1 \\  line2 \\  line3}} \\ 

    \multirow{12}{*}{\textbf{Test4}} 
    & \multirow{12}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}
    & \multirow{12}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}    \\ 

       \end{tabular}
       \label{table:mytable}
        \caption{This is the caption.} 

 \end{table}

\end{document}

ちなみに、参照も何らかの理由で正常に機能していません。この文は次のように言いたかったのです。

[...] ここでは表 1 を参照します。

その代わりに

[...] ここでは表を参照します。

他のテーブルでは使用できました\refが、このテーブルでは使用できませんでした。

答え1

\multirow{<nrows>}{<width>}{<text>}は、テキストを (垂直に)<nrows>幅 の行に広げます<width><width>が として指定されている場合は*、 の自然な幅が使用されます<text>。したがって、たとえば を使用すると、特定のセルを 12 行に広げることになります。はそれぞれ 3 行にのみ広げたいので、\multirow{12}{*}{<...>}これは明らかに誤りです。TestX

ここでは少し異なる実装を紹介します(multirowここで)を使用してmakecell:

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

\documentclass{report}

\usepackage{makecell}

\begin{document}

This is some text and here is a reference to Table~\ref{table:mytable}. 

\begin{table} [h]  
  \centering
  \begin{tabular} {c c c}
    \textbf{Column A} & \textbf{Column B} & \textbf{Column C} \\ 
    \hline
    \textbf{Test1} 
      & \makecell{line1 \\ line2 \\ line3}
      & \makecell{line1 \\ line2 \\ line3} \\ 
    \textbf{Test2} 
      & \multicolumn{2}{c}{\makecell{line1 \\ line2 \\ line3}} \\
    \textbf{Test3}
      & \makecell{line1 \\ line2 \\ line3}
      & \makecell{line1 \\ line2 \\ line3} \\ 
    \textbf{Test4} 
      & \makecell{line1 \\ line2 \\ line3}
      & \makecell{line1 \\ line2 \\ line3}
  \end{tabular}
  \caption{This is the caption.} 
  \label{table:mytable}
\end{table}

\end{document}

どのように\label来るかに注目してください \caption正しい\refエラーを達成するために。環境のラベルはなぜキャプションの後に表示される必要があるのですか?

答え2

質問のコメントに書いたように、あなたは複数行行のスパニングを計算しますが、これは簡単に修正できます。すべての場所に3を追加するだけです(つまり(6、9、12 の代わりに)

間違った位置に置かれたキャプションを修正するには、元のコードを使用する場合は、multirow-row の後にスペースを追加して、またがる行に十分なスペースを作成する必要があります。3 行にまたがるので、2 行 ( ) を追加するか、コマンド (&&\\で同様の量の高さを追加します。スペースが 3 行になるように s を2 つ追加しました。end-of-row\\[<height>]\normalbaselineskip

実際には、追加のパッケージをまったく使用せずにこの表をタイプセットできます。 を定義し\parbox、その中に 3 行を配置します。 の\parbox間隔が適切になるように、高さと幅を決定する必要があります。 高さと幅のパラメータを変更して、ドキュメントに適した間隔にすることができます。 これを避けたい場合は、 を使用しますmakecell。入力を減らすためのショートカットを定義しました\pb

ラベルにとって最も安全な場所は内部キャプションコマンド。そうすれば失敗しなくなります。

テーブル付き\parbox

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

\documentclass{report}
%\usepackage{multirow}
\newcommand{\pb}[1]{\parbox[c][40pt][c]{0.2\linewidth}{\centering #1}}

\begin{document}

\begin{table}
\caption{This is the caption.\label{table:mytable}} 
       \centering
       \begin{tabular} {@{}c c c@{}} \\
\bfseries Column A & \bfseries Column B                  & \bfseries Column C \\\hline
\bfseries Test1         & \pb{line1 \\  line2 \\  line3}    & \pb{line1 \\  line2 \\  line3} \\
\bfseries Test2         & \multicolumn{2}{c}{\pb{line1 \\  line2 \\  line3}} \\
\bfseries Test3         & \pb{line1 \\  line2 \\  line3}    & \pb{line1 \\  line2 \\  line3}\\
\bfseries Test4         & \pb{line1 \\  line2 \\  line3}    & \pb{line1 \\  line2 \\  line3}\\\hline
\end{tabular}
\end{table}
\end{document}

テーブル付き\multirow

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

\documentclass{report}
\usepackage{multirow}


\begin{document}

This is some text and here is a reference to Table \ref{table:mytable}. As you see, the table is a float, and since you have available space, it is place at top of the page. Do not mess around with placing floats before you have finished all creative writing. Everting you do, will be invalid when you add more text. 


\begin{table}
       \centering
       \begin{tabular} {c c c} \\
        \textbf{Column A} & \textbf{Column B} & \textbf{Column C} \\ 
    \hline
\multirow{3}{*}{\textbf{Test1}} 
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}} \\[2\normalbaselineskip]  % Add more space
\multirow{3}{*}{\textbf{Test2}} 
    & \multicolumn{2}{c}{ \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}} \\[2\normalbaselineskip] \multirow{3}{*}{\textbf{Test3}}
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}} \\[2\normalbaselineskip]
\multirow{3}{*}{\textbf{Test4}} 
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}
    & \multirow{3}{*}{\shortstack[l]{line1 \\  line2 \\  line3}}    \\[2\normalbaselineskip]
\end{tabular}
\caption{This is the caption.\label{table:mytable}} 
 \end{table}

\end{document}

答え3

同じく に基づくバリアント コードで、-makecellおよび を組み合わせて、ルールの周囲にパディングを追加します。3 行のグループを区切るために、垂直方向のスペースを追加しました。また、 への参照が機能するには、コマンド (キャプションの後)が必要です。multirowbooktabs\label

\documentclass{report}
\usepackage{multirow}
\usepackage{makecell, booktabs}
\renewcommand{\theadfont}{\normalsize\bfseries}


\begin{document}

This is some text and here is a reference to Table \ref{table:mytable}.

\begin{table} [h]
       \centering
       \begin{tabular} {c c c} \\
        \textbf{Column A} & \textbf{Column B} & \textbf{Column C} \\
    \midrule
    \multirowthead{3}{Test1} & line1 & line1 \\
        & line2 & line2 \\
        & line3 & line 3\\
\addlinespace
    \textbf{Test2} & \multicolumn{2}{c}{\makecell{line1 \\ line2 \\ line3}} \\
\addlinespace
    \multirowthead{3}{Test 3} & line1 & line1 \\
        & line2 & line2 \\
        & line3 & line 3\\
\addlinespace
    \multirowthead{3}{Test 4} & line1 & line1 \\
        & line2 & line2 \\
        & line3 & line 3\\
\bottomrule
       \end{tabular}
       \label{table:mytable}
        \caption{This is the caption.}\label{table:mytable}

 \end{table}

\end{document} 

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

関連情報