process list items differently depending on position in list
Hi, I need to process a comma separated list, and adapt the formatting according to the position of an item in the list (example below). Expected output would be: {\bf foo }, {\em bar} and baz. Is that possible? Can I somehow get the position of an item in the list? (My understanding is that \processcommalist and \processcommacommand apply a command to each list item, but there is no such thing as <position in the list>... I hope I'm wrong here.) Or is that something where using Lua would be a better choice? Best, Denis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \define[1]\command {#1} \define\somelist {foo, bar, baz} \starttext \processcommacommand [\somelist] \command \stoptext %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ok, in lua it's indeed simple:
\startluacode
list = {"foo", "bar", "baz"}
\stopluacode
\starttext
\startluacode
for i, v in ipairs(list) do
if i == #list then
tex.print("last:")
tex.print(v)
elseif i == 1 then
tex.print("first:")
tex.print(v)
else
tex.print("in between:")
tex.print(v)
end
end
\stopluacode
\stoptext
But, it'd still love to hear whether there is a solution on the tex side.
Denis
Von: Maier, Denis Christian (UB)
Gesendet: Montag, 2. Mai 2022 09:15
An: 'mailing list for ConTeXt users'
On 5/2/2022 12:09 PM, Denis Maier via ntg-context wrote:
But, it’d still love to hear whether there is a solution on the tex side. \processtokens {[before]} {[between]} {[after]} {[space]} {{one}{two}{three}}
\def\whatever{a,b,c,d} \getcommacommandsize[\whatever] \scratchcounterone \zerocount \scratchcountertwo \commalistsize \processcommacommand[\whatever] {\advance\scratchcounterone\plusone \ifnum\scratchcounterone=\scratchcountertwo \space and\space \orelse\ifnum\scratchcounterone>\plusone ,\space \fi \commalistelement} but ... there's also: \startlines \commalistsentence[aap,noot,mies] \commalistsentence[aap,noot] \commalistsentence[aap] \commalistsentence[a,b,c] \commalistsentence[a,b,c][{ \& },{ and }] \commalistsentence[a,b,c][+,-] \stoplines which uses presets like \setuplabeltext [nl] [and-1={{, }}, and-2={{ en }}] % 1, 2 en 3 \setuplabeltext [en] [and-1={{, }}, and-2={{, }}] % 1, 2, 3 \setuplabeltext [de] [and-1={{, }}, and-2={{ und }}] % 1, 2 und 3 \setuplabeltext [hr] [and-1={{, }}, and-2={{ i }}] % 1, 2 i 3 maybe wikify ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
-----Ursprüngliche Nachricht----- Von: ntg-context
Im Auftrag von Hans Hagen via ntg-context Gesendet: Montag, 2. Mai 2022 12:34 An: Denis Maier via ntg-context Cc: Hans Hagen Betreff: Re: [NTG-context] process list items differently depending on position in list On 5/2/2022 12:09 PM, Denis Maier via ntg-context wrote:
But, it’d still love to hear whether there is a solution on the tex side. \processtokens {[before]} {[between]} {[after]} {[space]} {{one}{two}{three}}
\def\whatever{a,b,c,d}
\getcommacommandsize[\whatever] \scratchcounterone \zerocount \scratchcountertwo \commalistsize
\processcommacommand[\whatever] {\advance\scratchcounterone\plusone \ifnum\scratchcounterone=\scratchcountertwo \space and\space \orelse\ifnum\scratchcounterone>\plusone ,\space \fi \commalistelement}
Thanks.
but ... there's also:
\startlines \commalistsentence[aap,noot,mies] \commalistsentence[aap,noot] \commalistsentence[aap] \commalistsentence[a,b,c] \commalistsentence[a,b,c][{ \& },{ and }] \commalistsentence[a,b,c][+,-] \stoplines
which uses presets like
\setuplabeltext [nl] [and-1={{, }}, and-2={{ en }}] % 1, 2 en 3 \setuplabeltext [en] [and-1={{, }}, and-2={{, }}] % 1, 2, 3 \setuplabeltext [de] [and-1={{, }}, and-2={{ und }}] % 1, 2 und 3 \setuplabeltext [hr] [and-1={{, }}, and-2={{ i }}] % 1, 2 i 3
maybe wikify
I've just checked, \commalistsentence is already on the wiki, but you have to know it exists. (I'll check whether I can link to that page from somewhere.) But there's no (high-level) way to apply certain commands to this list elements, right? Background: My real use case is a bit more complex. I use comma separated lists to store author lists, but the authors themselves are saved in structured variables. So, I'll need to reassemble the different name parts first. So, nothing like \commalistprocessandmakesentence[a,b,c]\commandforfirst\commandforinbetween\commandforlast Probably to specific, right? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \startbuffer[test] <?xml version='1.0' standalone='yes?> <document> <title>This is the title</title> <author> <family-name>Doe</family-name> <given-name>John</given-name> </author> <author> <family-name>Smith</family-name> <given-name>Jane</given-name> </author> <p>This is a first sentence</p> </document> \stopbuffer \startxmlsetups xml:setup \xmlsetsetup{\xmldocument}{*}{-} \xmlsetsetup{\xmldocument}{document|p}{xml:*} \stopxmlsetups \xmlregistersetup{xml:setup} \startxmlsetups xml:document \xmlfilter{#1}{/title/command(xml:title)} \xmlfilter{#1}{/author/command(xml:author)} \startdocument \xmlflush{#1} \stopdocument \stopxmlsetups \startxmlsetups xml:title \setupdocument[title={\xmlflush{#1}},author={\AuthorList}] \stopxmlsetups \startxmlsetups xml:author:family-name \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:author:given-name \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:author \defineauthor[\xmlfilter{#1}{/family-name/command(xml:author:family-name)}\xmlfilter{#1}{/given-name/command(xml:author:given-name)}][family-name={\xmlfilter{#1}{/family-name/command(xml:author:family-name)}},given-name={\xmlfilter{#1}{/given-name/command(xml:author:given-name)}}] \addtocommalist {\xmlfilter{#1}{/family-name/command(xml:author:family-name)}\xmlfilter{#1}{/given-name/command(xml:author:given-name)}} \AuthorList \stopxmlsetups \startxmlsetups xml:p \xmlflush{#1}\par \stopxmlsetups \definenamespace [documentauthor] % name of internal varialbles [type=module, name=author, command=yes, % Create \defineauthor style=yes, % Create \useauthorstyleandcolor setup=list, % Create \setupauthor parent=documentauthor, ] \def\AuthorList{} \define[1]\useauthor {\edef\currentauthor{#1}% {\authorparameter{given-name} \space \authorparameter{family-name}}} \startsetups document:start \documentvariable{title}\endgraf \blank[medium] \processcommacommand[\documentvariable{author}]\useauthor \endgraf \stopsetups \xmlprocessbuffer{main}{test}{}
participants (2)
-
denis.maier@unibe.ch
-
Hans Hagen