자신의 \rowfont 정의(tabu를 사용하지 않고)

자신의 \rowfont 정의(tabu를 사용하지 않고)

의 미래는 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}

결과

문제

  • using 의 인수가 Contains 의 경우 {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}

관련 정보