tabularray icon indicating copy to clipboard operation
tabularray copied to clipboard

Style for spanned cells

Open sergiokapone opened this issue 2 years ago • 2 comments

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}

sergiokapone avatar Mar 13 '23 15:03 sergiokapone

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:

  • #207 which will end with a new command \DefTblrMetaKey{inner | outer}{<meta key>}{<key-vals>}

muzimuzhi avatar Mar 13 '23 18:03 muzimuzhi

Based on #547 and #207, maybe it is a better solution to define meta keys under most key tree paths, so that after

\DeclareTblrMetaKey{mystyle}{c, bg=azure5, fg=white, font=\bfseries}

you can write

\SetCell[c=2]{meta=mystyle}

All key tree paths share the same \DeclareTblrMetaKey command.

lvjr avatar Dec 10 '24 06:12 lvjr