> On 2 Nov 2024, at 20:02, Joel via ntg-context <ntg-context@ntg.nl> 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

___________________________________________________________________________________
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