juh+ntg-context--- via ntg-context schrieb am 03.10.2024 um 10:49:
Am 02.10.24 um 14:29 schrieb Henning Hraban Ramm:
Oh, and of course you need a \bTABLEhead, otherwise there’s nothing to repeat. That means AFAIK, the header can’t be part of the CSV, using this simple module.

I tried to put the header into the before part of setupseparatedlist, but this does not work. I also fear that I cannot do it with this module. Are there other means to build a table from csv?

ConTeXt has a better parser for csv files since many years (which is used by the already mentioned handlecsv module) but there is no TeX interface for it.

The example below shows how the parser can be used to store csv data in a Lua table, you can even split of the first line of data to use it as header etc.

Afterwards you can use a loop to create a TeX table out of the stored Lua table.

%%%% begin example
\setuplayout[tight]

\setuppapersize[A8,landscape]

\starttext

\startbuffer[cattle]
"Year","Cattle, total","Dairy cows"
"2009","3968","1489"
"2010","3975","1479"
"2011","3885","1470"
"2012","3879","1484"
"2013","4000","1553"
"2014","4068","1572"
"2015","4134","1622"
"2016","4251","1745"
"2017","4096","1694"
\stopbuffer

% \savebuffer [list=cattle,prefix=no,file=cattle.csv]

\startluacode

local csvsplitter = utilities.parsers.rfc4180splitter()

local tablecontent, tableheader = csvsplitter(buffers.getcontent("cattle"),true) -- local tablecontent, tableheader = csvsplitter(io.loaddata("cattle.csv"),true)

context.bTABLE{ split = "repeat" }
  context.bTABLEhead()
    context.bTR()
      for _, tablecell in next, tableheader do
        context.bTH()
        context(tablecell)
        context.eTH()
      end
    context.eTR()
  context.eTABLEhead()
  context.bTABLEbody()
    for _, tablerow in next, tablecontent do
      context.bTR()
        for _, tablecell in next, tablerow do
          context.bTD()
          context(tablecell)
          context.eTD()
        end
      context.eTR()
    end
  context.eTABLEbody()
context.eTABLE()

\stopluacode

\stoptext
%%%% end example

Wolfgang
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / 
https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

Reply via email to