Table caption in the middle of the table
\multirow{<nrows>}{<width>}{<text>}
spreads text (vertically) across <nrows>
rows of width <width>
. If <width>
is given as *
, it uses the natural width of <text>
. So, for example, using \multirow{12}{*}{<...>}
means you want to spread that specific cell over 12 rows. It's obviously incorrect since each TestX
you want to spread over only 3 rows.
Here's a slightly different implementation (since you don't need multirow
here) using 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}
Note how the \label
comes after \caption
in order to achieve the correct \ref
erence. See Why does an environment's label have to appear after the caption?.
As I write in the comment to the question, you have misunderstood how multirow calculates spanning of rows, but it is easily fixed. Just add 3 all places (i.e. instead of 6, 9 and 12).
To fix the misplaced caption, if you want to use your original code, you have to add extra space after the multirow
-row to create enough space for the spanned rows. Since you span three rows, you may either add two rows (&&\\
) or the similar amount extra height at the end-of-row
command (\\[<height>]
. I added two \normalbaselineskip
s so you have space equal three rows.
Actually, you may typeset this table without using any additional package at all. Define a \parbox
and place the three lines within that. You have to decide a height and width for the \parbox
to have correct spacing. You may change the height and width parameters to have a spacing that suites your document. If you want to avoid that, use makecell
. I have defined a shortcut \pb
to reduce typing.
The safest place for the label is inside the caption command. Then it will not fail.
Table with \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}
Table with \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}
A variant code, also based on makecell
, combined with multirow
– and booktabs
, to add some padding around rules. I added some vertical spacing to separate the groups of three lines. Also, a reference to requires a \label
command (after the caption) to work.
\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}