thomas
thomas

Reputation: 43

latex combining multirow and multicolumn

I noticed some strange behaviour when combining \multirow with \multicolumn:

pdf output of code below

Is there an alternative solution to \multirow and \multicolumn for creating more complex headers for LaTeX tables or is there a fix for my problem?

\documentclass{article}
\usepackage{multirow}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}p{#1}}

\begin{document}

\begin{table}[ht]
\centering
\begin{tabular}{|r|r|r|r|}
\multicolumn{1}{|C{2cm}}{\multirow{3}{*}{head 1.1}} &
\multicolumn{2}{|C{2cm}}{\multirow{2}{*}{head 1.2}} &
\multicolumn{1}{|C{2cm}}{head 1.3 which is longer than expected} \\ \hline
& & & \multicolumn{1}{|C{2cm}}{head 2.3} \\
& \multicolumn{1}{|C{2cm}}{head 2.2.1} & 
\multicolumn{1}{|C{2cm}}{head 2.2.2} &
\multicolumn{1}{|C{2cm}}{head 3.3}
\end{tabular}
\end{table}

\end{document}

Upvotes: 0

Views: 4787

Answers (1)

Werner
Werner

Reputation: 15065

I'd suggest stacking your multi-level headings/cells using a tabular, which will naturally centre it vertically with respect to the other cells. Such tabular stacking is made easy using makecell:

enter image description here

\documentclass{article}

\usepackage{makecell}

\begin{document}

\begin{tabular}{|r|r|r|r|}
  head 1.1 & 
    \multicolumn{2}{c|}{head 1.2} & 
    \makecell{head 1.3 \\ which is \\ longer than \\ expected} \\
  \hline
    & head 2.2.1 & head 2.2.2 & \makecell{head 2.3 \\ head 3.3}
\end{tabular}

\end{document}

Other options also exist in aligning cells to the [t]op or [b]ottom.

Upvotes: 2

Related Questions