[Tex/LaTex] Strange disappearing colored rules in beamer

beamercolorcolortblrules

I have received a booktabs bug report of \cmidrules that disappear when coloured rules are used inside beamer (together with colortbl). The problem can be illustrated with the following simple code and looks like an interaction between the beamer colour setup and \leaders. In the normal Latex classes the code is fine.

I know to little about the inner workings of beamer and hope there is someone here who can help or that can give a workaround. Note that the colour need to be grouped inside booktabs to prevent leaking.

EDIT: In an e-mail Michael Palmer suggested the addition of a \kern at the end of the grouping. This seems to work but the question is WHY?

Note: The standard tabular \cline also disappears inside beamer when the line color is set with the \arrayrulecolor of the colortbl package. So this is now also David Carlisle's problem 🙂

Beamer class

\documentclass{beamer}
\newcommand\tfillA[3]{\makebox[#1]{#2\leaders\hrule\hfill#3}}
\newcommand\tfillB[3]{\makebox[#1]{\color{red}#2\leaders\hrule\hfill#3}}
\newcommand\tfillC[3]{\makebox[#1]{#2{\color{red}\leaders\hrule\hfill}#3}}
\newcommand\tfillD[3]{\makebox[#1]{#2{\color{red}\leaders\hrule\hfill\kern0pt}#3}}%<--Edit

\begin{document}
\begin{frame}
    INSIDE BEAMER CLASS\par\smallskip
    \tfillA{2cm}{A}{B}: Works fine without colour\par
    \tfillB{2cm}{A}{B}: Works fine with ungrouped colour\par
    \tfillC{2cm}{A}{B}: Rule disappears with grouped colour\par
    \tfillD{2cm}{A}{B}: Rule reappears with kern at end ?????
\end{frame}
\end{document}

enter image description here

Article class

\documentclass{article}
\usepackage{xcolor}
\newcommand\tfillA[3]{\makebox[#1]{#2\leaders\hrule\hfill#3}}
\newcommand\tfillB[3]{\makebox[#1]{\color{red}#2\leaders\hrule\hfill#3}}
\newcommand\tfillC[3]{\makebox[#1]{#2{\color{red}\leaders\hrule\hfill}#3}}

\begin{document}
    INSIDE ARTICLE CLASS\par\smallskip
    \tfillA{2cm}{A}{B}: Works fine without colour\par
    \tfillB{2cm}{A}{B}: Works fine with colour  everywhere\par
    \tfillC{2cm}{A}{B}: Works fine with grouped colour
\end{document}

enter image description here

Best Answer

Beamer, for whatever reason, redefines the latex color command \reset@color to do an unskip. That normally removes preceding white space, which you can save and restore with \lastskip but unfortunately \lastkip and \unskip "work" with \leaders but \lastskip only saves the glue component and so no record that there are leaders there is possible in classic tex (or etex).

All LaTeX \leader commands have a trailing \kern or box to prevent this, otherwise several standard commands including \newline which similarly do an \unskip would destroy \dotfill etc. (The LaTeX definition of \dotfill adds this, the original plain TeX one does not have it).

So while it is a bit surprising that Beamer does this, it is generally the case that a leaders followed by unknown commands ought to have the \kern there. \reset@color is inserted by \aftergroup at the closing } which is why the problem only affects the version that closes the group immediately after the leader. One could argue (not unreasonably) that \reset@color should not be an "unknown" command. Obviously it would be easy to patch it not to do that but without knowing the details of beamers workings and why it was changed, that may not be the best thing to do.