How can a macro check how many items are in a list?
Suppose I have a macro like this: \define[1]\thisismylist{ This list has n items. \startitemize #1\stopitemize } And in the document it might have this: \thisismylist{\item cat\item dog\item tree } How can I make n display three, as there are three items? "This list has 3 items: 1. cat2. dog3. tree I will use this as a means to check if the description before a list needs a plural or not: Read the prompts below: - Do you think cats should be banned from university dormitories?- Should public schools provide free breakfast? And that way, if there is only one prompt, it changes the instructions to be: Read the prompt below: - Do you think cats should be banned from university dormitories? --Joel
Joel via ntg-context schrieb am 02.11.2024 um 21:02:
Suppose I have a macro like this:
\define[1]\thisismylist{
This list has n items.
\startitemize #1 \stopitemize
}
And in the document it might have this:
\thisismylist{ \item cat \item dog \item tree }
How can I make n display three, as there are three items?
"This list has 3 items:
1. cat 2. dog 3. tree
I will use this as a means to check if the description before a list needs a plural or not:
Read the prompts below:
- Do you think cats should be banned from university dormitories? - Should public schools provide free breakfast?
And that way, if there is only one prompt, it changes the instructions to be:
Read the prompt below:
- Do you think cats should be banned from university dormitories?
When you pass your itemize entries as comma separated list you can use the \getcommalistsize command to count the list entries. Afterwards use \doloopoverlist to create an itemize entry for each text in the comma separated list. %%%% begin example \starttexdefinition protected thisiymylist [#1] \getcommalistsize[#1] \doifelse{\commalistsize}{1} {Read the prompt below:\par} {Read the prompts below:\par} \startitemize \doloopoverlist{#1}{\startitem\recursestring\stopitem} \stopitemize \stoptexdefinition \starttext \thisiymylist [Do you think cats should be banned from university dormitories?, Should public schools provide free breakfast?] \thisiymylist [Do you think cats should be banned from university dormitories?] \stoptext %%%% end example Wolfgang
and/or add the number of items to be expected: \starttexdefinition protected thisiymylist [#1] \getcommalistsize[#1] \doifelse{\commalistsize}{1} {Read the prompt below:\par} {Read {\em all} the \the\numexpr \commalistsize -1\relax\ prompts below:\par} \startitemize[n] \doloopoverlist{#1}{\startitem\recursestring\stopitem} \stopitemize \stoptexdefinition
On 2 Nov 2024, at 20:02, Joel via ntg-context
wrote: Suppose I have a macro like this:
\define[1]\thisismylist{
This list has n items.
\startitemize #1 \stopitemize
}
And in the document it might have this:
\thisismylist{ \item cat \item dog \item tree }
How can I make n display three, as there are three items?
"This list has 3 items:
1. cat 2. dog 3. tree
I will use this as a means to check if the description before a list needs a plural or not:
Read the prompts below:
- Do you think cats should be banned from university dormitories? - Should public schools provide free breakfast?
And that way, if there is only one prompt, it changes the instructions to be:
Read the prompt below:
- Do you think cats should be banned from university dormitories?
If your actual items are more complex than those in your example and Wolfgang’s suggestion of iterating over a comma separated list won’t work then ... The following code stores the length of each list in a dataset. The `delay` option ensures the values retrieved are those from the completed run of the document, and a unique id is assigned to each list so subsequent lists don’t overwrite the length calculations for earlier ones. \definecounter [MyItemsCounter] \definecounter [MyItemsUniqueID] [way=bytext] \setcounter [MyItemsUniqueID] [1] \definedataset [MyItems] [delay=yes] \defineitemgroup [MyItems] \setupitemgroup [MyItems] [ before={\setcounter[MyItemsCounter][1]}, inbetween={\incrementcounter[MyItemsCounter]}, after={\setdataset[MyItems][\rawcountervalue[MyItemsUniqueID]][count=\rawcountervalue[MyItemsCounter]]% \incrementcounter[MyItemsUniqueID]}, ] \define\listLen{\datasetvariable{MyItems}{\rawcountervalue[MyItemsUniqueID]}{count}} \define\itemOrItems{\doifelse{\datasetvariable{MyItems}{\rawcountervalue[MyItemsUniqueID]}{count}}{1}{item}{items}} \starttext This list has \listLen\ \itemOrItems. \startMyItems \item cat \item dog \item tree \stopMyItems \blank This list has \listLen\ \itemOrItems. \startMyItems \item cow \stopMyItems \stoptext Regards, — Bruce Horrocks Hampshire, UK
On 11/3/2024 5:53 PM, Bruce Horrocks wrote:
\definecounter [MyItemsCounter] \definecounter [MyItemsUniqueID] [way=bytext] \setcounter [MyItemsUniqueID] [1]
\definedataset [MyItems] [delay=yes]
\defineitemgroup [MyItems] \setupitemgroup [MyItems] [ before={\setcounter[MyItemsCounter][1]}, inbetween={\incrementcounter[MyItemsCounter]}, after={\setdataset[MyItems][\rawcountervalue[MyItemsUniqueID]][count=\rawcountervalue[MyItemsCounter]]% \incrementcounter[MyItemsUniqueID]}, ]
\define\listLen{\datasetvariable{MyItems}{\rawcountervalue[MyItemsUniqueID]}{count}} \define\itemOrItems{\doifelse{\datasetvariable{MyItems}{\rawcountervalue[MyItemsUniqueID]}{count}}{1}{item}{items}}
\starttext
This list has \listLen\ \itemOrItems. \startMyItems \item cat \item dog \item tree \stopMyItems
\blank
This list has \listLen\ \itemOrItems. \startMyItems \item cow \stopMyItems
\stoptext
There is actually some info when you are in an itemize: \currentitemindex \currentnofitems So it's not too hard to get the upcoming value of \currentnofitems: (1) an extra helper in strc-itm.mkxl \permanent\tolerant\protected\def\upcomingnofitems[#1]% {\the\numexpr \clf_analyzeupcomingitemgroup {\ifempty{#1}\v!itemize\else#1\fi}% \numexpr\c_strc_itemgroups_nesting+\plusone\relax \c_strc_itemgroups_max_items \relax} (2) a few line patch in strc-itm.lmt local function analyzeitemgroup(name,level,upcoming) local n = counts[name] local u = false if level == 1 then n = n + 1 counts[name] = n u = upcoming and true end local items = 0 local width = 0 local itemgroup = collected[name] if itemgroup then local entry = itemgroup[n] if entry then local l = entry[level] if l then items = l[1] or 0 width = l[2] or 0 end end end if u then counts[name] = n - 1 end texsetcount(c_strc_itemgroups_max_items,items) texsetdimen(d_strc_itemgroups_max_width,width) end plus extra: implement { name = "analyzeupcomingitemgroup", actions = analyzeitemgroup, arguments = { "string", "integer", true } } And then for Bruce to come up with the singular / plural variant for This list has \upcomingnofitems[MyItems] entries \startMyItems \item cat \item dog \item tree \stopMyItems This list has \upcomingnofitems\ entries \startitemize \item cat \item dog \item tree \stopitemize Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 4 Nov 2024, at 17:52, Hans Hagen via ntg-context And then for Bruce to come up with the singular / plural variant for Thanks Hans.
A “pluraliser” for item/item, entry/entries etc is overkill given that only one person has requested the feature so far and, mainly, I have no idea how a multi-lingual version might work. :-)
In English texts it is quite common to write “item(s)” when it is not certain how many are being referred to. This works quite nicely in Context:
\def\item(s){\if\upcomingnofitems 1{item}\else{items}\fi}
which allows:
This list has \upcomingnofitems\ \item(s)
in the source.
So I’m going to suggest that we leave it for individual users to do what’s simplest for them. I’ll add something to the wiki when this comes out in the next release.
—
Bruce Horrocks
Hampshire, UK
participants (5)
-
Bruce Horrocks
-
Hans Hagen
-
Joel
-
vm
-
Wolfgang Schuster