ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] How can a macro check how many items are in a list?
       [not found] <692582251.5119511.1730577725919.ref@mail.yahoo.com>
@ 2024-11-02 20:02 ` Joel via ntg-context
  2024-11-03  7:19   ` [NTG-context] " Wolfgang Schuster
  2024-11-03 16:53   ` Bruce Horrocks
  0 siblings, 2 replies; 6+ messages in thread
From: Joel via ntg-context @ 2024-11-02 20:02 UTC (permalink / raw)
  To: Mailing List for ConTeXt Users; +Cc: Joel


[-- Attachment #1.1: Type: text/plain, Size: 735 bytes --]

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

[-- Attachment #1.2: Type: text/html, Size: 2599 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: How can a macro check how many items are in a list?
  2024-11-02 20:02 ` [NTG-context] How can a macro check how many items are in a list? Joel via ntg-context
@ 2024-11-03  7:19   ` Wolfgang Schuster
  2024-11-03 11:04     ` vm via ntg-context
  2024-11-03 16:53   ` Bruce Horrocks
  1 sibling, 1 reply; 6+ messages in thread
From: Wolfgang Schuster @ 2024-11-03  7:19 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Joel via ntg-context

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: How can a macro check how many items are in a list?
  2024-11-03  7:19   ` [NTG-context] " Wolfgang Schuster
@ 2024-11-03 11:04     ` vm via ntg-context
  0 siblings, 0 replies; 6+ messages in thread
From: vm via ntg-context @ 2024-11-03 11:04 UTC (permalink / raw)
  To: ntg-context; +Cc: vm

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


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: How can a macro check how many items are in a list?
  2024-11-02 20:02 ` [NTG-context] How can a macro check how many items are in a list? Joel via ntg-context
  2024-11-03  7:19   ` [NTG-context] " Wolfgang Schuster
@ 2024-11-03 16:53   ` Bruce Horrocks
  2024-11-04 17:52     ` Hans Hagen via ntg-context
  1 sibling, 1 reply; 6+ messages in thread
From: Bruce Horrocks @ 2024-11-03 16:53 UTC (permalink / raw)
  To: ntg-context mailing list



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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: How can a macro check how many items are in a list?
  2024-11-03 16:53   ` Bruce Horrocks
@ 2024-11-04 17:52     ` Hans Hagen via ntg-context
  2024-11-05 18:21       ` Bruce Horrocks
  0 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen via ntg-context @ 2024-11-04 17:52 UTC (permalink / raw)
  To: ntg-context; +Cc: Hans Hagen

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [NTG-context] Re: How can a macro check how many items are in a list?
  2024-11-04 17:52     ` Hans Hagen via ntg-context
@ 2024-11-05 18:21       ` Bruce Horrocks
  0 siblings, 0 replies; 6+ messages in thread
From: Bruce Horrocks @ 2024-11-05 18:21 UTC (permalink / raw)
  To: ntg-context mailing list


On 4 Nov 2024, at 17:52, Hans Hagen via ntg-context <ntg-context@ntg

> 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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-11-05 18:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <692582251.5119511.1730577725919.ref@mail.yahoo.com>
2024-11-02 20:02 ` [NTG-context] How can a macro check how many items are in a list? 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
2024-11-04 17:52     ` Hans Hagen via ntg-context
2024-11-05 18:21       ` Bruce Horrocks

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).