lvjr / tabularray

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

Evaluate functions in table specs with functional library #270

Closed lvjr closed 3 weeks ago

lvjr commented 2 years ago

From https://tex.stackexchange.com/questions/648786/how-to-define-new-key-properties-in-l3keys

Recently I found tcolorbox has an interesting IfEmptyTF key, with which you can write

IfEmptyTF = {⟨token list⟩}{key=val1}{key=val2}

And I decided tabularray should have a similar feature, and can make the interface more natural and more powerful to users:

key = \tlIfEmptyTF{⟨token list⟩}{\prgReturn{val1}}{\prgReturn{val2}}

where \tlIfEmptyTF function is from functional package. That is to say, users can use all functions in functional package in the values and tabularray should replace them with their return values. And this feature should only be enabled if functional library is load by users.

lvjr commented 2 years ago

Now I think it is an elegant solution to apply \evalWhole to the second argument of every \keys_set:nn command beforehand. With this solution you can also use functions from functional packages or (defined a new one with \prgNewFunction) in the key:

\someFunc{aaa} = \otherFunc{bbb}

where \someFunc returns a key name, and \otherFunc returns its value.

Also issue #207 can be solved by making a function return both key name and its value.

But we need to keep evaluate and process options safe in making this change.

lvjr commented 3 weeks ago

Finally I added keyvalue library to tabularray. And here is an example.

\documentclass[b5paper]{article}
\usepackage{tabularray}
\usepackage{ninecolors}
\UseTblrLibrary{keyvalue}
\begin{document}

\begin{tblr}{
  row{3} = {bg=\intIfOddTF{\value{page}}{\prgReturn{red7}}{\prgReturn{blue7}}}
}
  Alpha   & Beta  & Gamma  \\
  Epsilon & Zeta  & Eta    \\
  Iota    & Kappa & Lambda \\
  Alpha   & Beta  & Gamma  \\
  Epsilon & Zeta  & Eta    \\
  Iota    & Kappa & Lambda \\
\end{tblr}

\newpage

\begin{tblr}{
  row{3} = {bg = \intIfOddTF{\value{page}}{\prgReturn{red7}}{\prgReturn{blue7}}}
}
  Alpha   & Beta  & Gamma  \\
  Epsilon & Zeta  & Eta    \\
  Iota    & Kappa & Lambda \\
  Alpha   & Beta  & Gamma  \\
  Epsilon & Zeta  & Eta    \\
  Iota    & Kappa & Lambda \\
\end{tblr}

\end{document}

image

lvjr commented 1 week ago

In the above commit, I have merged keyvalue library into functional library, because writing functions in row{i} values is the same as writing them in \SetRow commands.

\documentclass{article}

\usepackage{tabularray}
\usepackage{ninecolors}
\UseTblrLibrary{functional}

\begin{document}

\begin{tblr}{
  hlines,
  row{2} = {bg=\intIfOddTF{\value{page}}{\prgReturn{red7}}{\prgReturn{blue7}}},
}
  Alpha   & Beta  & Gamma  \\
  Epsilon & Zeta  & Eta    \\
  Iota    & Kappa & Lambda \\
\end{tblr}

\begin{tblr}[evaluate=all]{hlines}
  Alpha   & Beta  & Gamma  \\
  \SetRow{bg=\intIfOddTF{\value{page}}{\prgReturn{red7}}{\prgReturn{blue7}}}
  Epsilon & Zeta  & Eta    \\
  Iota    & Kappa & Lambda \\
\end{tblr}

\end{document}
image