6

Since the future of tabu is not that safe I’m trying to define an own \rowfont macro. This is what I’ve got so far:

\documentclass{article}

\usepackage{array,tabularx}
\usepackage[table]{xcolor}
\usepackage{xparse,etoolbox}

\ExplSyntaxOn\makeatletter

\int_new:N \l_@@_last_rownum_int

\cs_new_nopar:Npn \@@_current_rowfont: { }

\NewDocumentCommand { \rowfont } { m } {
   \cs_gset_nopar:Npn \@@_current_rowfont: { #1 }
   #1
}

\newcolumntype { L } {
   >{
      \int_compare:nNnTF { \int_use:N \l_@@_last_rownum_int } = { \number \rownum } {
         \@@_current_rowfont:
      } {
         \cs_gset_nopar:Npn \@@_current_rowfont: { }
         \@@_current_rowfont:
      }
   }
   l
   <{
      \int_gset:Nn \l_@@_last_rownum_int { \number \rownum }
   }
}
\newcolumntype { x } {
   >{
      \int_compare:nNnTF { \int_use:N \l_@@_last_rownum_int } = { \number \rownum } {
         \@@_current_rowfont:
      } {
         \cs_gset_nopar:Npn \@@_current_rowfont: { }
         \@@_current_rowfont:
      }
   }
   X
   <{
      \int_gset:Nn \l_@@_last_rownum_int { \number \rownum }
   }
}

\rowcolors { 0 } { } { }

\ExplSyntaxOff\makeatother

\begin{document}
tabular:
\begin{tabular}{LLL}
   \rowcolor{black}\rowfont{\color{red}}
   1 & 2 & 3 \\
   1 & 2 & 3 \\
\end{tabular}

\bigskip

tabularx:
\begin{tabularx}{0.5\textwidth}{xxx}
   \rowcolor{black}\rowfont{\sffamily\footnotesize\color{red}}
   1 & 2 & 3 \\
   1 & 2 & 3 \\
\end{tabularx}
\end{document}

result

Problems

  • In {tabularx} the row using \rowfont gets enlarged when the argument of \rowfont contains \color.
  • It only works when using \rowcolors.
  • It can be used with special column types only.

Maybe it is possible to solve the problems with another definition …

4
  • I don't know why its future is unsafe but since it is LPPL licensed, why don't you just make a copy and rename it? Or just see how tabu codes it? I guess that I'm not understanding the question. Is the point to do this in l3 syntax because the current code will be incompatible with LaTeX 3? (This is just a guess which tries to make sense of the motivation for the question. If that's the case, doubtless others less ignorant than me will not need the clarification.)
    – cfr
    May 13, 2014 at 2:39
  • @cfr: The future is unsafe because there will be a new implementation incompatible with the old one (see tex.stackexchange.com/q/121841/4918). Sure I can duplicate the whole package but I’m interested in how a stand alone implementation works to improve my LaTeX skills further … copying the code form tabu is not easy, because it redefines the cell-handling by adding hooks for \rowfont
    – Tobi
    May 13, 2014 at 7:44
  • Are there any news here? Did you find a solution?
    – TeXnician
    Apr 18, 2018 at 7:49
  • @TeXnician No, unfortunately not ..
    – Tobi
    Apr 19, 2018 at 12:35

1 Answer 1

2

An an alternative to tabu package, tabularray package provides a easy way to set row fonts and colors:

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\begin{document}
\begin{tblr}{
   width = 0.5\textwidth, colspec = {X[1]X[2]X[3]}, hlines,
   row{1} = {bg=gray9, fg=red3, font=\sffamily\footnotesize},
}
   1 & 2 & 3 \\
   1 & 2 & 3 \\
\end{tblr}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .