1

I have a coloured table with some multirows. I have observed that when I use multirow, sometimes, especially when the text has to make a linebreak, the table losses its vertical alignment. How can I align the text of the 1st column and 1-2 row to the middle?

If is allowed another question, how can I colour correctly this specific cell, still using booktabs?

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{rotating}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{array}
\usepackage{colortbl}
\begin{document}
\begin{table}
\centering
\begin{tabular}{c*2{>{\centering\arraybackslash}m{3cm}}}
\cmidrule{2-3}
 & \textbf{Column 1} & \textbf{Column 2}\\
\midrule
 \textbf{Row 1}  & \multirow{2}{3cm}{ \cellcolor{yellow}A longer text for line break 1} & Text row 1\\
\cmidrule{1-1}\cmidrule{3-3}
 \textbf{Row 2}  & & \cellcolor{yellow} A longer text for line break 2\\
\midrule
 \textbf{Row 3}  & Text row 3 & A longer text for line break 3\\
\midrule
\end{tabular}
\end{table}
\end{document}

enter image description here

2

You need to decide what can be calculated automatically and what must be set as constant dimensions. It seems that your table has constant cell widths (3 cm in your case, the longer text must be broken to more lines). So, using \halign for such table is somewhat impractical. On the other hand, you need to calculate cell heights automatically and you need to centre texts in such cells (vertically). This task can be done by \valign. There is only one little problem: you need to set the data as in transpose table in \valign, i.e. column by column. It can be done by following code:

\def\Yellow{\pdfliteral{0 0 1 0 k}}
\def\Green{\pdfliteral{.7 0 .7 0 k}}
\def\Black{\pdfliteral{0 g}}

\def\vcc#1{\medskip\vfil\vc{#1}\vfil\medskip\hrule}
\def\vc#1{\vbox{\hsize=3cm \leftskip=3pt plus1fill \rightskip=\leftskip
   \noindent\vbox to10pt{}#1\unskip\strut}}
\def\scc#1{\medskip\vfil\hbox{\kern5pt\bf#1\kern5pt}\vfil\medskip\hrule}

\def\cvcc#1#2{\medskip\setbox0=\vc{#2}\dimen0=\ht0 \advance\dimen0 by\dp0
   #1\leaders\hrule width3cm\vskip\dimen0 plus1fil
   \Black \kern-\dimen0 \box0
   #1\leaders\hrule width3cm\vfil
   \Black \medskip \hrule
}

\valign{\hrule\vcc{#}&\vcc{#}&\vcc{#}&\vcc{#}\cr
   \omit\scc{} & \omit\scc{Row 1} & \omit\scc{Row 2} & \omit\scc{Row 3} \cr
   \bf Column 1 & 
   \multispan2 \cvcc\Yellow{A longer text for line break} & 
   \omit\cvcc\Green{Text row 3} \cr
   \bf Column 2 & 
   Text row 1 & 
   \omit\cvcc\Green{A longer text for line break 2} & 
   A longer text for line break 3 \cr
}

\bye

valign table

This task cannot be solved using standard LaTeX tables because they are using \halign TeX primitive inside, no \valign. So, I used \valign natively and I tested this in plain TeX (of course, because I don't prefer LaTeX). But I hope that this code (based on TeX primitives and standard plain TeX macros) will work in LaTeX too (I didn't try it).

You cannot to do your task using \halign (alias using standard LaTeX tables) without guessing many special constants for each \multirow occurrences.

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.