定義自己的 \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

作為 package 的替代方案tabutabularraypackage 提供了一種設定行字體和顏色的簡單方法:

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

相關內容