独自の \rowfont を定義する(タブーを使用せずに)

独自の \rowfont を定義する(タブーを使用せずに)

の将来はtabuそれほど安全ではないので、独自のマクロを定義しようとしています\rowfont。これがこれまでのところ私が得たものです:

\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}

結果

問題点

  • の引数に が含まれている場合、を{tabularx}使用する行が拡大されます。\rowfont\rowfont\color
  • を使用する場合にのみ機能します\rowcolors
  • 特殊な列タイプでのみ使用できます。

別の定義で問題を解決できるかもしれません…

答え1

tabuパッケージの代替として、tabularrayパッケージは行のフォントと色を設定する簡単な方法を提供します。

\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}

関連情報