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

Improvement space for one auto-tabular `tabularray` example #29

Closed muzimuzhi closed 7 months ago

muzimuzhi commented 10 months ago

At first, I found the example displaying "a tabularray variant of the \csvautotabular command" on page 59 (csvsimple-l3.pdf, v2.4.0 (2023/05/09)) could be improved to using only tabularray inner options.

Then I realized that there're no auto-tabular command for tabularray tables yet, hence tried to implement one.

https://github.com/T-F-S/csvsimple/blob/e32ef0539140d6cfa79caf633fe50f9db1d9842d/doc/latex/csvsimple/csvsimple-l3.tex#L2844-L2864

\begin{filecontents}[noheader]{data.csv}
land,group,amount
Bayern,A,1700
Baden-Württemberg,A,2300
Sachsen,B,1520
Thüringen,A,1900
Hessen,B,2100
\end{filecontents}

\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{tabularray}
\usepackage{xcolor}

\ExplSyntaxOn
\keys_define:nn { csvsim }
  {
    _autotabularray_ .meta:n =
      {
        file                    = {#1},
        command                 = \csvlinetotablerow,
        no~head,
        generic~collected~table = tblr,
      },
    autotabularray   .meta:n =
      {
        _autotabularray_        = {#1},
        column~names~detection  = false,
        generic~table~options   = { { vlines, hline{1,2,Z} } }
      },
    autotabularray*  .meta:n =
      {
        _autotabularray_        = {#1},
        generic~table~options   = { { vlines, hline{1,Z} } }
      }
  }

\NewDocumentCommand \csvautotabularray { s +O{} m }
  {
    \IfBooleanTF {#1}
      {
        \keys_set:nn { csvsim } { default, every~csv, autotabularray*={#3}, #2}
      }
      {
        \keys_set:nn { csvsim } { default, every~csv, autotabularray={#3}, #2}
      }
    \__csvsim_loop:
  }
\ExplSyntaxOff

\begin{document}

Currently in \verb|csvsimple-l3| manual
\csvreader[
    no head,
    centered tabularray =
      {
        row{odd} = {blue!85!gray!7},
        row{1}   = {blue!50!gray!25, font=\bfseries},
      },
    table head = {\hline[0.1em,blue!50!black]},
    table foot = {\hline[0.1em,blue!50!black]},
    late after first line = {\\\hline[blue!50!black]}
]{data.csv}{}{%
  \csvlinetotablerow%
}

Proposal 1, using only \verb|tabularray| (inner) options
\csvreader[
    no head,
    centered tabularray =
      {
        row{odd}   = {blue!85!gray!7},
        row{1}     = {blue!50!gray!25, font=\bfseries},
        hline{1,Z} = {0.1em, blue!50!black},
        hline{2}   = {blue!50!black}
      }
]{data.csv}{}{%
  \csvlinetotablerow
}

Proposal 2, using newly defined \verb|\csvautotabularray|
\csvautotabularray[table centered, generic table options={
  {
    row{odd}   = {blue!85!gray!7},
    row{1}     = {blue!50!gray!25, font=\bfseries},
    hline{1,Z} = {0.1em, blue!50!black},
    hline{2}   = {blue!50!black}
  }
}]{data.csv}

Testing \verb|\csvautotabularray|

\csvautotabularray{data.csv}
\quad
\csvautotabularray*{data.csv}
\end{document}

image image

T-F-S commented 10 months ago

Thank you for this proposal. It looks good, but I have to test it later with more time.

T-F-S commented 10 months ago

I like the new \csvautotabularray very much and I think this is a very valuable addition to the package.

Many people ask for customization of the \csvauto... macros which are quite limited. The tabularray variant opens much more possibilties for customization due to the power of the tabularray package.

I will add some modified version of your proposal to the next version. Thank you!

muzimuzhi commented 10 months ago

Reviewing the change in 3cb41f7 (2.5.0, 2023-10-16), I found the updated tabularray example can be a little bit better.

\begin{filecontents}[noheader]{data.csv}
land,group,amount
Bayern,A,1700
Baden-Württemberg,A,2300
Sachsen,B,1520
Thüringen,A,1900
Hessen,B,2100
\end{filecontents}

\documentclass{article}
\usepackage{csvsimple-l3}
\usepackage{tabularray}
\usepackage{xcolor}

\begin{document}
In v2.5.0
\csvautotabularray[table centered]{data.csv}
  [
    row{odd}   = {blue!85!gray!7},
    row{1}     = {blue!50!gray!25, font=\bfseries},
    hline{1,Z} = {0.1em, blue!50!black},
    hline{2}   = {blue!50!black}
  ]

Proposed, cells in first row prepended by \verb|\MakeUppercase|
\csvautotabularray[table centered]{data.csv}
  [
    row{odd}   = {blue!85!gray!7},
    row{1}     = {blue!50!gray!25, font=\bfseries, preto=\MakeUppercase},
    hline{1,Z} = {0.1em, blue!50!black},
    hline{2}   = {blue!50!black}
  ]
\end{document}

image

According to texdoc usrguide, sec. 6 "Case changing",

Upper- and lower-casing are well-understood in general conversation. [...] The full range of Unicode UTF-8 input can be supported, with the proviso that at present the characters set up with 8-bit engines match those available in standard input encodings (T1, T2, LGR).

Hence maybe preto=\MakeUppercase can also be safely added to autotabularray and autolongtabularray meta keys, like generic table options={ row{1} = {..., preto=\MakeUppercase}, ... }.

PS: Since in tabularray package \begin{longtblr}{...} ... \end{longtblr} is the same as \begin{tblr}[long]{...} ... \end{tblr} (see its doc for v2023A (2023-03-01), sec. 3.2.2 "Long and Tall Tables"), theoretically autolongtabularray can be declared on top of autotabularray, and \csvautolongtabularray{<file name>} can be just \csvautotabularray{<file name>}[long][]. The first way requires the outer and inner specifications of a tblr table to be independently set, and the second way requires the outer and inner specifications to be additive. (\begin{tblr}[<outer spec>]{<inner spec>})

Current design is not flexible enough. I definitely forgot the outer spec part when opening this issue.

PS2: If the intention is to mimic the default rule widths of \toprule (.08em), \midrule (.05em), and \bottomrule (.08em) in booktabs package (just a guess), then hline{1,Z} = {0.08em}, hline{2} = {0.05em} would be the goal (I have no preference).

T-F-S commented 10 months ago

Hence maybe preto=\MakeUppercase can also be safely added to autotabularray and autolongtabularray meta keys, like generic table options={ row{1} = {..., preto=\MakeUppercase}, ... }.

Yes, I will add this.

PS: Since in tabularray package \begin{longtblr}{...} ... \end{longtblr} is the same as \begin{tblr}[long]{...} ... \end{tblr} (see its doc for v2023A (2023-03-01), sec. 3.2.2 "Long and Tall Tables"), theoretically autolongtabularray can be declared on top of autotabularray, and \csvautolongtabularray{<file name>} can be just \csvautotabularray{<file name>}[long][]. The first way requires the outer and inner specifications of a tblr table to be independently set, and the second way requires the outer and inner specifications to be additive. (\begin{tblr}[<outer spec>]{<inner spec>})

I thought about this when creating the code and I also took a very quick look into the source code of tabularray. Despite the statement in the documentation, tblr and longtbl are defined independendly, i.e. longtbl is not build upon tblr using the outer spec. I decided to follow the same approach and also define \csvautotabularray and \csvautotabularray by itself.

PS2: If the intention is to mimic the default rule widths of \toprule (.08em), \midrule (.05em), and \bottomrule (.08em) in booktabs package (just a guess), then hline{1,Z} = {0.08em}, hline{2} = {0.05em} would be the goal (I have no preference).

Indeed, I wanted that it looks booktabish. I will adapt the line width for the next version.

muzimuzhi commented 10 months ago

Despite the statement in the documentation, tblr and longtbl are defined independendly, i.e. longtbl is not build upon tblr using the outer spec.

From related source lines in tabularray.sty

in which \SetTblrOuter { <outer specs> } without opt-arg is equivalent to \SetTblrOuter [ tblr ] { <outer specs> }.

Hence defined by the same factory command \NewTblrEnviron, these three envs (and most other similarly-defined envs) only differ in their initial outer (\SetTblrOuter) and inner (\SetTblrInner) specs.

T-F-S commented 7 months ago

I think is was done (?).