4

I managed to make a table but the caption (or any text I add after it) is in the middle of the table, as if the table ended after the second row. I couldn't find a similar question so I decided to ask myself.

I added the caption and some text after the table. They both behave as if the table ended after the second row

Without the caption, or any text before/after it, the table looks almost how I would want it to look.

This is how the table looks without the caption

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

As a side note, the referencing also isn't working properly for some reason?. I wanted the sentence to say

[...] here is a reference to Table 1.

and instead it just says

[...] here is a reference to Table .

I've managed to use \ref with other tables but not this one.

  • Do you know how \multirow works? For example, what does the first argument in \multirow{12} mean. Once you figure that out, I'm sure you'll understand what's going on. – Werner Aug 21 at 19:44
  • From what I understand, it's the number of rows I want the multirow block to be. In this case, it's 3 rows. So the first multirow is rows 1-3, the second one is 3-6 and so on. – NATS Aug 21 at 20:00
  • Will there always bw three lines in colum B and C? – leandriis Aug 21 at 20:05
  • @NATS You have programmed last multirow to span 12 column, not 3. If you add all the spanning, you get 3+6+9+12=30 rows, and you have 12 rows in your table. I suggest you reread the multirow manual. – Sveinung Aug 21 at 20:12
3

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

enter image description here

\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 \reference. See Why does an environment's label have to appear after the caption?.

  • If you always have 3 rows/elements in Column B & Column C, you don't need makecell either. Just place the Column A entry on the second row of each "block." – Werner Aug 21 at 20:06
3

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

enter image description here

\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

enter image description here

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

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} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.