lvjr / tabularray

Typeset tabulars and arrays with LaTeX3
https://ctan.org/pkg/tabularray
247 stars 22 forks source link

Style for spanned cells #380

Open sergiokapone opened 1 year ago

sergiokapone commented 1 year ago

In my project I have some spaned cells with code \SetCell[c=2]{c, bg=azure5, fg=white, font=\bfseries}

It is possible o set some style, and use it? For example \NewCellStyle{mystyle}{c, bg=azure5, fg=white, font=\bfseries}

In code \SetCell[c=2]{style=mystyle}

muzimuzhi commented 1 year ago

Try this:

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}

\ExplSyntaxOn
% \NewCellStyle {<style>}{<key-val>}  % create a new cell style
% \NewCellStyle*{<style>}{<key-val>}  % update an existing cell style
\NewDocumentCommand \NewCellStyle { s m }
  {
    \IfBooleanTF {#1} 
      { % renew
        \keys_if_exist:nnTF { tblr-cell-spec } { #2 }
          { \__tblr_new_style:nnn { cell } { #2 } }
          { \msg_error:nnnn { tabularray } { undefined-style } { cell } { #2 } }
      }{% new
        \keys_if_exist:nnTF { tblr-cell-spec } { #2 }
          { \msg_error:nnnn { tabularray } { existing-style } { cell } { #2 } }
          { \__tblr_new_style:nnn { cell } { #2 } }
      }
  }

\cs_new_protected:Nn \__tblr_new_style:nnn
  {
    \keys_define:nn { tblr-#1-spec }
      { #2 .meta:n = { #3 } }
  }

\msg_new:nnn { tabularray } { undefined-style }
  { #1 ~ style ~ ``#2'' ~ is ~ undefined. }

\msg_new:nnn { tabularray } { existing-style }
  { #1 ~ style ~ ``#2'' ~ already ~ exists. }
\ExplSyntaxOff

\begin{document}
\NewCellStyle{mystyle}{c, bg=blue!70, fg=white, font=\bfseries}
\begin{tblr}{hlines, vlines}
  a & b & c \\
  a & b & c \\
  \SetCell[c=2]{mystyle} ab & c \\
\end{tblr}

\NewCellStyle*{mystyle}{c, bg=red!70, fg=white, font=\bfseries}
\begin{tblr}{hlines, vlines}
  a & b & c \\
  a & b & c \\
  \SetCell[c=2]{mystyle} ab & c \\
\end{tblr}

\NewCellStyle{mystyle}{} % error, style existed
\NewCellStyle*{mystyleX}{} % error, style undefined
\end{document}

image

Similar: