表格標題位於表格中間

表格標題位於表格中間

我設法製作了一個表格,但標題(或我在其後面添加的任何文字)位於表格的中間,就好像表格在第二行之後結束一樣。我找不到類似的問題,所以我決定問自己。

我在表格後面添加了標題和一些文字。它們的行為就像表格在第二行之後結束一樣

如果沒有標題或之前/之後的任何文本,表格看起來幾乎就是我想要的樣子。

這是沒有標題的表格的樣子

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

相反,它只是說

[...] 這裡是 Table 的引用。

我已經成功地與其他表一起使用\ref,但不是這個。

答案1

\multirow{<nrows>}{<width>}{<text>}將文字(垂直)分佈在<nrows>width 的行上<width>。如果<width>給定為*,則它使用 的自然寬度<text>。例如,使用\multirow{12}{*}{<...>}表示您希望將該特定儲存格分佈到 12 行。這顯然是不正確的,因為每個TestX你只想分佈在 3 行上。

這是一個稍微不同的實作(因為你不需要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 個所有位置 (IE而不是 6、9 和 12)。

要修復錯位的標題,如果您想使用原始程式碼,則必須在 -row 後面添加額外的空格,multirow以便為跨行創建足夠的空間。由於您跨越三行,您可以添加兩行 ( ) 或在命令 ( )中添加&&\\類似數量的額外高度。end-of-row\\[<height>]\normalbaselineskip

實際上,您可以根本不使用任何附加套件來排版此表。定義 a\parbox並將三行放在其中。您必須確定高度和寬度才能\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,結合multirow– 和booktabs,在規則周圍添加一些填充。我添加了一些垂直間距來分隔三行組。此外,對 的引用需要\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} 

在此輸入影像描述

相關內容