Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NewDocumentCommand to generate row content #18

Closed
c-alpha opened this issue Jun 8, 2021 · 7 comments
Closed

NewDocumentCommand to generate row content #18

c-alpha opened this issue Jun 8, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@c-alpha
Copy link

c-alpha commented Jun 8, 2021

I would like to be able to define a new command to generate table row content according to a defined scheme. In the old days I would do something like this in my class file:

\newcommand{\tabentry}[4]{
  \printdate{#1} & #2 & (#3) & #4 \\
}

And in my documents:

\begin{tabu}   % or whatever table package you were using
   \tabentry{2021-05-12}{Alpha}{Beta}{Gamma Delta}
   \tabentry{2021-06-01}{Epsilon}{Rho}{Phi Nu Mu}
\end{tabu}

But with tblr I get a "misplaced alignment tab character &" error.

I tried my luck with \NewTableCommand, but it seems that whatever I define is inserted at the beginning, or at the end of a row only. It seems \NewTableCommand is for hline-type use cases only?

When I escape the alignment tab as \& or \string& I get literal ampersands in the rendered output.

Also, the way of specifying the arguments to \NewTableCommand seems to be quite specific to how it is used in tabularray itself.

So I guess my ideal solution would be a way of using the standard \NewDocumentCommand (or \NewExpandableDocumentCommand if need be), and enjoy the power of xparse, to define a function producing table row content.

I would not mind whether the \\ is inserted by the newly defined command (as shown i the example above), or in the table, btw.

@lvjr lvjr added the enhancement New feature or request label Jun 9, 2021
@lvjr
Copy link
Owner

lvjr commented Jun 9, 2021

The major limitation of tabularray package is that, you can not hide & and \\ in the commands. Maybe we can find another way to generate table content later.

@lvjr
Copy link
Owner

lvjr commented Jun 9, 2021

At this moment, the above example can be written as follows (with version 2021K of tabularray package):

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\newcommand\printdate[1]{\texttt{#1}}
\begin{tblr}{
  cell{-}{1}={preto=\printdate\bgroup,appto=\egroup},
  cell{-}{3}={preto=(,appto=)},
}
  2021-05-12 & Alpha   & Beta & Gamma Delta \\
  2021-06-01 & Epsilon & Rho  & Phi Nu Mu \\
\end{tblr}
\end{document}

image

@c-alpha
Copy link
Author

c-alpha commented Jun 10, 2021

You are so totally right, of course: separate content from styling! I was clinging to my old mental model still. And your solution is also so much more elegant, because it saves me from making that same function call again and again for each row. Super cool!

The selectors (row, column, and cell) are extremely powerful tools.

The prefix/postfix approach you have now is easy to understand, and straightforward to use. So I guess it is well suited for most users.

What do you think about generalising this by also allowing to specify a function, that will be invoked for each selected cell? The function would take a single, mandatory argument, in which it would receive the cell's content. If the selection is for a row, or column, the function would be invoked for each cell in the selected row or column. With such a mechanism, the MWE could hypothetically be written as:

\documentclass{article}
\usepackage{tabularray}
\ExplSyntaxOn
\cs_new:Npn \my_datepr:n #1 { \texttt{#1} }
\cs_new:Npn \my_braces:n #1 { (#1) }
\ExplSyntaxOff
\begin{document}
\begin{tblr}{
  column{1}={proc=\my_datepr:n},
  column{3}={proc=\my_braces:n},
}
  2021-05-12 & Alpha   & Beta & Gamma Delta \\
  2021-06-01 & Epsilon & Rho  & Phi Nu Mu \\
\end{tblr}
\end{document}

@lvjr
Copy link
Owner

lvjr commented Jun 10, 2021

Good idea. I will add this option for rows/columns/cells later (maybe with a better name?).

@c-alpha
Copy link
Author

c-alpha commented Jun 11, 2021

Cool! Yes, please feel free to choose a name.

@lvjr
Copy link
Owner

lvjr commented Jun 19, 2021

I have added cmd option for rows/columns/cells.

@lvjr
Copy link
Owner

lvjr commented Jul 11, 2021

I have added expand option for expanding every occurrence of the specified macro once:

\newcommand{\printdate}[1]{\texttt{#1}}
\newcommand{\tabentry}[4]{
  \printdate{#1} & #2 & (#3) & #4 \\
}
\begin{tblr}[expand=\tabentry]{ccc}
  \tabentry{2021-05-12}{Alpha}{Beta}{Gamma Delta}
  \tabentry{2021-06-01}{Epsilon}{Rho}{Phi Nu Mu}
\end{tblr}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants