ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Bruce Horrocks <ntg@scorecrow.com>
To: ntg-context mailing list <ntg-context@ntg.nl>
Subject: [NTG-context] Re: How can a macro check how many items are in a list?
Date: Sun, 3 Nov 2024 16:53:38 +0000	[thread overview]
Message-ID: <544F4782-4007-4CBC-8F2F-30299E93B906@scorecrow.com> (raw)
In-Reply-To: <692582251.5119511.1730577725919@mail.yahoo.com>



> 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
___________________________________________________________________________________

  parent reply	other threads:[~2024-11-03 16:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <692582251.5119511.1730577725919.ref@mail.yahoo.com>
2024-11-02 20:02 ` [NTG-context] " Joel via ntg-context
2024-11-03  7:19   ` [NTG-context] " Wolfgang Schuster
2024-11-03 11:04     ` vm via ntg-context
2024-11-03 16:53   ` Bruce Horrocks [this message]
2024-11-04 17:52     ` Hans Hagen via ntg-context
2024-11-05 18:21       ` Bruce Horrocks

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=544F4782-4007-4CBC-8F2F-30299E93B906@scorecrow.com \
    --to=ntg@scorecrow.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).