T-F-S / csvsimple

A LaTeX package for lightweight CSV file processing.
http://www.ctan.org/pkg/csvsimple
LaTeX Project Public License v1.3c
24 stars 5 forks source link

Feature Request: Make the tabular env name configurable #12

Closed muzimuzhi closed 2 years ago

muzimuzhi commented 3 years ago

Currently, csvsimple supports only three hard-coded tabular-like environments: tabular, longtable, and tabbling. But there exist other tabular-like envs and the number of them slowly grows. For example, see newly emerged packages nicematrix and tabularray.

Hence the request to make the tabular env name configurable. For example, a new option table env=<env name> defined (and internally used) by

\keys_define:nn { csvsim }
  {
    table~env    .tl_set:n  = \l__csvsim_table_env_tl,
    table~env    .initial:n = tabular,
    % ...
    tabular      .meta:n =
      {
        _table_ = { \g__csvsim_before_table_tl\begin{\l__csvsim_table_env_tl}{#1}\g__csvsim_table_head_tl }
                  { \g__csvsim_table_foot_tl\end{\l__csvsim_table_env_tl}\g__csvsim_after_table_tl },
        late~after~line = \\
      },
  }
muzimuzhi commented 3 years ago

Why not use \begin{<table env>}{...} \csvreader... \end{<table env>}:

Package tabularray splits the body of its tblr env into cells by \\ and &, so the above usage is not supported. See https://github.com/lvjr/tabularray/issues/23#issuecomment-866469939 and this TeX-SX question.

Edit: This is a fake reason because adding table env will not solve the tabularray problem.

muzimuzhi commented 3 years ago

Some related thoughts:

(Was my original proposal an X-Y problem?)

lvjr commented 3 years ago

As the author of tabularray package, I will add a new feature in next version for expanding \tblrbody command once before parsing \\ and &, therefore you only need to \let \tblrbody = \csvoutput, if csvsimple could store the output in the macro.

muzimuzhi commented 3 years ago

@lvjr It seems the issue can be resolved on the csvsimple side, and I've made a naive attempt. Also I opened

T-F-S commented 3 years ago

I'm not convinced about a configurable tabular environment name, because the differerent environments (existing or yet to be written) need different treatment of options and settings and just changing the name is not enough. You realized that and also proposed to configure the arguments, but this also not enough for tabularry which needs body collection. Other environments may also need special things further.

So, I think, a common architecture with table~env and further keys will be a moving target whenever a new environment comes up with new unforseen feature.

What I would love to have and implement is specialized support for tabularray (and, maybe, other packages) with a tabularray options which also sets necessary actions to collect the needed data. I have to admit that I did not know that package before, but the documentation looks very promising. @lvjr I think a new feature to set the content of the table by a macro which is expanded once, as you wrote, could be very helpful.

lvjr commented 3 years ago

@T-F-S Glad to hear that you will implement specialized support for tabularray package, I have added the feature of expanding \tblrbody once in the latest source code. Now the following example should work:

\def\tblrbody{
 \hline
  20 & 30 & 40\\
  50 & 60 & 70\\
 \hline 
}
\begin{tblr}{|c|c|c|}
  \tblrbody
\end{tblr}
T-F-S commented 3 years ago

@lvjr I'm still experimenting with the user interface to avoid expansion specific things as far as possible.

T-F-S commented 3 years ago

With version 2.1.0 (2021/07/06), there is support for tabularray with an equally named option. I also added some examples.

lvjr commented 3 years ago

Thanks. I have tried the new release. It's wonderful!

lvjr commented 3 years ago

It would be nice if csvsimple could also support long tabularray. Not much change, only need to change the environment name from tblr to longtblr.

lvjr commented 3 years ago

Also in the example in page 52, \SetCell and \SetRow were protected by \csvexpnot. It was because they were defined only inside tblr environment.

In the above commit https://github.com/lvjr/tabularray/commit/8185714d459f3a921eb519fca73f29021e2e2487, I have defined all \Set*** commands outside tblr environment. Then with the next release 2021M of tabularray planned on 1st August, users don't need to protect these commands.

BTW, \tablenum also need not to be protected by \csvexpnot. Only \hline, \cline, etc need to be protected.

  \ifcsvstrcmp{\group}{A}{\csvexpnot\SetRow{brown7}}{} 
  \csvexpnot\SetCell{bg=purple7} 
  \csvexpval\land 
& \csvexpval\group
& \csvexpval\amount
& \csvexpnot\tablenum[exponent-mode=scientific, round-precision=3, round-mode=places, table-format=1.3e1]{\fpeval{pi*\amount}}
T-F-S commented 3 years ago

Your package evolves with large steps, because I did not see longtblr the last time I looked into the documentation - and that was not long before 👍

Yes, I will add a long tabularray option to use this fine new feature.

According \tablenum you are right that \csvexpnot is not needed. I going add some text in the documentation about when protection is needed.

lvjr commented 3 years ago

Tabularray could not redefine \hline and \cline commands globally since other packages need them. If csvsimple could redefine and protect these two commands before fully expanding contents inside \csvreader (need to restore the original definitions afterwards), then users need not to protect them either.

T-F-S commented 3 years ago

Redefining \hline, \cline et.al. could be dangerous, because one does not know how user apply them exactly.

In many (most?) cases, a refinition or protection is not needed, because \hline is likely to be used inside options like late after line which are collected unexpanded. Only command is (and needs to be) expanded.

lvjr commented 3 years ago

Thanks for the explanation. Sorry, I am still not enough familiar with csvsimple.

lvjr commented 3 years ago

I have submitted version 2021M (2021-08-01) of tabularray to CTAN.

lvjr commented 2 years ago

By now tabularray has several predefined table environments: tblr, longtblr, talltblr (similar to threeparttable) and booktabs. And users can define new table environments with \NewTblrEnviron commands.

Maybe it would be better to make tabular env name configurable. Also there are more and more specifications in the optional arguments of various tabularray environments. So it would be nice if users could set the contents of the optional argument too.

Spreadtab, a package for doing some spreadsheet calculation for tables, provides the following syntax for supporting various tabular envrionments:

\begin{spreadtab}{{<envname>}{<parameter>}}
......
\end{speadtab}

And after calculation, the package converts the above code to

\begin{<envname>}{<parameter>}
......
\end{<envname>}

Although it is an old package, I succeeded in writing the following:

\begin{spreadtab}{{tabularray}[<options>]{<parameter>}}
......
\end{speadtab}

Package nicematrix has several environments with two optional arguments. In my opinion something like env head = {{<envname>}{<parameter>}} maybe useful for supporting various tabular enviroments. Although different tabulars have different syntax, csvsimple only need to generates cell contents, not need to touch others.

T-F-S commented 2 years ago

OK, I will think about adding a generic table option with configurable environment name. I still fear that tomorrow a new thing comes up which does not fit, but for the current tables (with and without data collection) a generalization is possible.

T-F-S commented 2 years ago

Implemented with version 2.2.0 (2021-09-09). There are new options generic table, generic collected table, and generic table options.

For example, to use talltblr, something like the following can be used:

  generic collected table = talltblr,
  generic table options   = {[caption={Test}]{colspec={XXX}}},
lvjr commented 2 years ago

Great. Thank you!