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?

 & \textbf{Column 1} & \textbf{Column 2}\\
 \textbf{Row 1}  & \multirow{2}{3cm}{ \cellcolor{yellow}A longer text for line break 1} & Text row 1\\
 \textbf{Row 2}  & & \cellcolor{yellow} A longer text for line break 2\\
 \textbf{Row 3}  & Text row 3 & A longer text for line break 3\\

enter image description here


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\vc#1{\vbox{\hsize=3cm \leftskip=3pt plus1fill \rightskip=\leftskip
   \noindent\vbox to10pt{}#1\unskip\strut}}

\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

   \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


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.

| improve this answer | |

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.