# How to color tabu row background in beamer LaTeX

I am trying to color the background of the third row of this tabu environment red:

\documentclass{beamer}

\usepackage{tabu}
\usepackage{colortbl}

\begin{document}

\begin{frame}
\begin{tabu}{XX}
\everyrow{\hline} \hline
Row 1 & Row 1 \\
Row 2 & Row 2 \\
\rowcolor{red}
Row 3 & Row 3 \\
Row 4 & Row 4 \\
\end{tabu}
\end{frame}

\end{document}


The rowcolor command has no effect, none of the rows has a colored background in the output. I am quite certain this used to work at some point (possibly in the distant past), but I am aware that colortbl may (now) be incompatible with tabu. I have also tried to use the \taburowcolors command instead with no success.

What is the recommended way to color a single row in a tabu environment inside a beamer document?

Note: I am compiling with XeLaTeX in case this makes a difference.

• \rowcolor is currently broken. Read the readme here and check the issues. github.com/tabu-fixed/tabu – Ulrike Fischer Mar 21 at 13:22
• Thanks a lot @UlrikeFischer. Since this isn't urgent, I might downgrade and watch the issue tracker closely. – Jesko Hüttenhain Mar 21 at 13:32
• I would simply not use tabu. No tabular in a presentation can be so complicated that you need it. – Ulrike Fischer Mar 21 at 13:38
• If you're using tabu just for the \everyrow things, I can post some code which should enable you to use something similar in normal tabular and tabularx tables, too. – Skillmon Mar 21 at 13:42
• @JeskoHüttenhain the problem would be that the code would either require a dedicated environment (and would then be hard to do for tabularx), or would affect every tabular and array. Is that ok? Which seems favourable? – Skillmon Mar 21 at 15:34

In the comments you indicate that you are using tabu to inject a \hline after every row. This could be done also like this (if the optional argument of \ is needed some adjustments must be added):

\documentclass{beamer}

\usepackage{colortbl}
\newcommand\tabnewlinewithhline{\tabularnewline\hline}
\begin{document}

\begin{frame}

\begin{tabular}{\ccwithhline}
\hline
Row 1 & Row 1 \\
Row 2 & Row 2 \\
\rowcolor{red}
Row 3 & Row 3 \\
Row 4 & Row 4 \\
\end{tabular}
\end{frame}

\end{document}


The following defines two environments, a mytabular (which is like tabular) and a mytabularx (which is like tabularx) environment, that do put a \hline in between each line and two \hlines at the top and the bottom of the environments. It does so by locally patching some internals of the array package. Any nested tabular or array environment should not contain those \hlines, which is desirable to create some special alignments (e.g. using \makecell needs this). The last row doesn't need to be terminated by \\ (which is responsible for much of the needed code) in both environments.

The mechanism should work, but if anybody finds bugs, please report them. Also if anybody thinks the mechanism is too clumsy, please feel free to leave comments and suggest something better (I know of \everycr but for unknown to me reasons I refrain from using it, so that could be a viable alternative to patching array's \\).

\documentclass[]{article}

\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{tabularx}
\usepackage{etoolbox}
\usepackage{environ}

\makeatletter
\newcommand\mypatchcmd[3]%>>>
{%
\patchcmd#1{#2}{#3}{}{\GenericError{}{Patching \string#1\space failed}{}{}}%
}%<<<
\newcommand\mypretocmd[2]%>>>
{%
\pretocmd#1{#2}{}{\GenericError{}{Patching \string#1\space failed}{}{}}%
}%<<<
\newcommand\myapptocmd[2]%>>>
{%
\apptocmd#1{#2}{}{\GenericError{}{Patching \string#1\space failed}{}{}}%
}%<<<
\newcommand*\unpatch@array%>>>
{%
\let \@@array      \@@array@original
\let \@arraycr     \@arraycr@original
\let \@xarraycr    \@xarraycr@original
\let \@xargarraycr \@xargarraycr@original
\let \@yargarraycr \@yargarraycr@original
\let \tabular      \tabular@original
\let \endtabular   \endtabular@original
\let \array        \array@original
\let \endarray     \endarray@original
}%<<<
\newcommand*\patch@array%>>>
{%
\mypretocmd\tabular{\unpatch@array}%
\mypretocmd\array{\unpatch@array}%
\myapptocmd\@yargarraycr{\my@hline}%
\mypatchcmd\@xarraycr{\cr}{\cr\my@hline}%
\mypatchcmd\@xargarraycr{\cr}{\cr\my@hline}%
\myapptocmd\@@array{\hline\hline}%
}%<<<
\newcommand*\backup@array%>>>
{%
\let \@@array@original      \@@array
\let \@arraycr@original     \@arraycr
\let \@xarraycr@original    \@xarraycr
\let \@xargarraycr@original \@xargarraycr
\let \@yargarraycr@original \@yargarraycr
\let \tabular@original      \tabular
\let \endtabular@original   \endtabular
\let \array@original        \array
\let \endarray@original     \endarray
}%<<<
\newenvironment{mytabular}[2][c]%>>>
{%>>>
\backup@array
\patch@array
\begin{tabular@original}[#1]{#2}%
}%<<<
{%>>>
\crcr
\hline
\hline
\end{tabular@original}%
}%<<<
%<<<
\newcommand\my@hline%>>>
{%
\noalign{\ifnum0=}\fi
\futurelet\reserved@a\my@xhline
}%<<<
\newcommand\my@xhline%>>>
{%
\ifx\reserved@a\end
\expandafter\my@yhline
\else
\hrule\@height\arrayrulewidth
\ifx\reserved@a\hline
\vskip\doublerulesep
\fi
\expandafter\my@xhline@fin
\fi
}%<<<
\newcommand*\my@xhline@fin%>>>
{%
\ifnum0={\fi}%
}%<<<
\newcommand\my@yhline[2]%>>>
{%
\begingroup
\def\tmpa{#2}%
\def\tmpb{mytabular}%
\expandafter
\endgroup
\ifx\tmpa\tmpb
\else
\hrule\@height\arrayrulewidth
\fi
\ifnum0={\fi}%
\end{#2}%
}%<<<
\newcommand\mytabularx@expandhelper[2]%>>>
{%
#2#1%
}%<<<
\NewEnviron{mytabularx}%>>>
{%
\backup@array
\patch@array
\edef\BODY{\expandafter\mytabularx@striplast\expandafter{\BODY}}%
\expandafter\mytabularx@expandhelper\expandafter{\BODY}{\begin{tabularx}}%
\crcr
\hline
\hline
\end{tabularx}%
}%<<<
\newcommand\myifempty[1]%>>>
{%
\if\relax\detokenize{#1}\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}%<<<
\newcommand\myifnobbackslash[1]%>>>
{%
\myifbbackslash@#1\\\endmyifbbackslash@
}%<<<
\def\myifbbackslash@#1\\#2\endmyifbbackslash@%>>>
{%
\myifempty{#2}%
}%<<<
\newcommand\mytabularx@striplast[1]%>>>
{%
\myifnobbackslash{#1}
{%
\unexpanded{#1}%
}
{%
\mytabularx@striplast@#1\endmytabularx@striplast
}%
}%<<<
\def\mytabularx@striplast@#1\\#2\endmytabularx@striplast%>>>
{%
\myifempty{#2}
{%
\unexpanded{#1}%
}%
{%
\unexpanded{#1\\}%
\mytabularx@striplast{#2}%
}%
}%<<<
\makeatother

\begin{document}
\noindent
\begin{mytabularx}{\linewidth}{XX}
Row 1 & Row 1 \\
Row 2 & Row 2 \\
\rowcolor{red}
Row 3 & Row 3 \\
Row 4 & Row 4 \\
\end{mytabularx}

\bigskip
\begin{mytabular}{ll}
a & b\\
c & d\\
\multicolumn{2}{c}{ab}\\
e & f\\
\begin{tabular}{@{}l@{}}a\\b\end{tabular} & cd \\
\end{mytabular}
\end{document}
`

• My sincere compliments. – Sebastiano Mar 23 at 22:49