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

`data collection=<macro>` is not reset per csv reader #39

Closed muzimuzhi closed 3 months ago

muzimuzhi commented 3 months ago

Found in https://tex.stackexchange.com/q/717500.

\documentclass{article}
\usepackage[l3]{csvsimple}

\begin{filecontents*}[overwrite]{test.csv}
ArtNr,  EAN,        Description,    Ha,     Ea
123456, 123456789,  Product1,   Header1a,   Entry1a
\end{filecontents*}

\csvstyle{every csv}{
    head to column names, 
    collect data,
    %% workaround
    % data collection = \csvdatacollection
}

\begin{document}
\csvreader[data collection=\HeSave, range=1]
  {test.csv}{}{\csvexpval\csvcolv}

colv=\HeSave % \HeSave holds "Entry1a"

\csvreader{test.csv}{}{a}

colv=\HeSave % now \HeSave holds "a"
             % because "data collection=\HeSave" set for the previous \csvreader
             % is still in effect
\end{document}

Moving setting of data~collection = \csvdatacollection to the default meta key (between resetting collect data and consume collected data) should fix the problem. https://github.com/T-F-S/csvsimple/blob/94632b980ff7f5beed9ca7a1361cf05c2481ac8b/tex/latex/csvsimple/csvsimple-l3.sty#L1040-L1043 https://github.com/T-F-S/csvsimple/blob/94632b980ff7f5beed9ca7a1361cf05c2481ac8b/tex/latex/csvsimple/csvsimple-l3.sty#L1595-L1599

Patch

```diff From 19da6ef6bb18f59a34cc4f6bdbeea08f95a77d5c Mon Sep 17 00:00:00 2001 From: Yukai Chou Date: Thu, 16 May 2024 03:15:21 +0800 Subject: [PATCH] Reset data collection macro (#39) --- tex/latex/csvsimple/csvsimple-l3.sty | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tex/latex/csvsimple/csvsimple-l3.sty b/tex/latex/csvsimple/csvsimple-l3.sty index 9aa9b2c..4759f7c 100644 --- a/tex/latex/csvsimple/csvsimple-l3.sty +++ b/tex/latex/csvsimple/csvsimple-l3.sty @@ -1037,12 +1037,6 @@ } -\keys_set:nn { csvsim } - { - data~collection = \csvdatacollection, - } - - %---- catcodes @@ -1607,6 +1601,7 @@ head~to~column~names~prefix = , head~to~column~names = false, collect~data = false, + data~collection = \csvdatacollection, consume~collected~data = false, column~count = 0, on~column~count~error =, -- 2.45.0 ```

muzimuzhi commented 3 months ago

Also although the doc of csvsim/consume collected data reads

Otherwise, if set to true, the collected data is not saved, but directly used after reading the CSV file, see csvsim/generic collected table.

The code actually globally clears the data collection macro when consume collected data=true, which is an undocumented behavior. See line 994 in code snippet below https://github.com/T-F-S/csvsimple/blob/94632b980ff7f5beed9ca7a1361cf05c2481ac8b/tex/latex/csvsimple/csvsimple-l3.sty#L988-L1000

IMO the global clearing needs either be documented or removed.

T-F-S commented 3 months ago

Originally, I had a single global setting for data collection in mind. But the usage in https://tex.stackexchange.com/q/717500 is quite straight-forward and the failure not obvious to the user.

So, I agree to change the behaviour and reset data collection to \csvdatacollection for every CSV file. To get back the old behaviour, one would need to set

\csvstyle{every csv}{
    data collection = \mymacro
}

consume collected data consumes the data. After consumation, it is 'gone', i.e. the macro becomes empty (free memory). That is supposed to be the etymology for the naming, but I will add a sentence in the documentation.

grafik

T-F-S commented 3 months ago

Resolved with https://github.com/T-F-S/csvsimple/releases/tag/v2.6.1

muzimuzhi commented 3 months ago

Thanks!