
Consegui fazer uma tabela mas a legenda (ou qualquer texto que eu adicionar depois dela) fica no meio da tabela, como se a tabela terminasse depois da segunda linha. Não consegui encontrar uma pergunta semelhante, então decidi me perguntar.
Sem a legenda, ou qualquer texto antes/depois dela, a tabela fica quase como eu gostaria que ficasse.
\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}
Como observação lateral, a referência também não está funcionando corretamente por algum motivo? Eu queria que a frase dissesse
[...] aqui está uma referência à Tabela 1.
e em vez disso apenas diz
[...] aqui está uma referência à Tabela .
Consegui usar \ref
com outras tabelas, mas não com esta.
Responder1
\multirow{<nrows>}{<width>}{<text>}
espalha o texto (verticalmente) pelas <nrows>
linhas de largura <width>
. Se <width>
for dado como *
, ele usa a largura natural de <text>
. Então, por exemplo, using \multirow{12}{*}{<...>}
significa que você deseja espalhar aquela célula específica em 12 linhas. É obviamente incorreto, pois cada um TestX
você deseja distribuir em apenas 3 linhas.
Aqui está uma implementação um pouco diferente (já que você não precisamultirow
aqui) usandomakecell
:
\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}
Observe como \label
vemdepois \caption
para alcançar a \ref
erência correta. VerPor que o rótulo de um ambiente precisa aparecer após a legenda?.
Responder2
Enquanto escrevo o comentário à pergunta, você não entendeu comomultilinhacalcula a abrangência das linhas, mas é facilmente corrigido. Basta adicionar 3 todos os lugares (ou sejaem vez de 6, 9 e 12).
Para corrigir a legenda mal colocada, se quiser usar seu código original, você deve adicionar espaço extra após -row multirow
para criar espaço suficiente para as linhas estendidas. Como você abrange três linhas, você pode adicionar duas linhas ( &&\\
) ou uma quantidade semelhante de altura extra no end-of-row
comando ( \\[<height>]
. Adicionei dois \normalbaselineskip
s para que você tenha espaço igual a três linhas.
Na verdade, você pode compor esta tabela sem usar nenhum pacote adicional. Defina a \parbox
e coloque as três linhas dentro dele. Você tem que decidir a altura e a largura para \parbox
ter o espaçamento correto. Você pode alterar os parâmetros de altura e largura para obter um espaçamento adequado ao seu documento. Se você quiser evitar isso, use makecell
. Eu defini um atalho \pb
para reduzir a digitação.
O local mais seguro para a etiqueta édentroo comando de legenda. Então não falhará.
Mesa com\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}
Mesa com\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}
Responder3
Um código variante, também baseado em makecell
, combinado com multirow
– e booktabs
, para adicionar algum preenchimento às regras. Adicionei algum espaçamento vertical para separar os grupos de três linhas. Além disso, uma referência requer um \label
comando (após a legenda) para funcionar.
\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}