How to make a macro that creates a checklist?

I want to build an item list with the icon replaced with a checkbox. I copied macro itemize-dot and modified the icon to box:

<assign|itemize-check|<\macro|body>
  <compound|<if|<and|<value|<merge|prefix-|itemize-dot>>|<unequal|<value|last-item-nr>|0>>|list*|list>|<macro|name|<aligned-item|<arg|name>
  \ >>|<macro|name|<with|mode|math|\<box\>>>|<arg|body>>
</macro>>

But when I press ENTER inside the list, texmacs won’t add the <item> tag for me. Thus the box icon won’t show.

<\itemize-check>
  test
</itemize-check>

It should be:

<\itemize-check>
  <item>test
</itemize-check>

OK. I got a solution. Add the following to my-init-texmacs.scm:

(define-group custom-itemize-tag
  itemize-check)

(tm-define (custom-itemize-context? t)
  (tree-in? t (custom-itemize-tag-list)))

(tm-define (kbd-enter t shift?)
  (:require (custom-itemize-context? t))
  (if shift? (make-return-after)
    (begin
      (if
        (not (make-return-after))
        (make 'item)))))

Alternatively, replace the definition of itemize-tag-list will also work:

(define-group itemize-tag
  itemize itemize-minus itemize-dot itemize-arrow itemize-check)

But this approach will break if texmacs introduce some other itemized tags in the future.

2 Likes

Despite its name define-group also adds to an existing group, so (define-group itemize-tag itemize-check) should also work as a more future-proof option.

3 Likes

Using your and @jeroen’s message I now can improve my blog post at https://texmacs.github.io/notes/docs/new-lists.html

1 Like

Can’t get the new-list approach work. Don’t know where went wrong.

<assign|checklist|<macro|body|<compact|<itemize-check|<arg|body>>>>>

I want to define this new compact check list without modifying the definition of itemize-tag again. If I invoke this macro, the resulting tree node is <checklist|.... How can I inline this checklist node?

Sorry, I haven’t understood. Could you explain in another way?

Your checklist macro worked for me after I put it in the custom-itemize-tag group.

I don’t want to put it in the custom-itemize-tag group or modify the itemize-tag again. I want to know how to define checklist in such a way that when it’s invoked, the resulting tree node is not checklist. You have to put it in custom-itemize-tag or itemize-tag because the invocation of checklist results in a tree node of checklist, which is not desirable in this case.

I see. Let me think. I have never tried to do such a thing. Here is something that can help: if you enter the macro ref, you get the macro reference; perhaps one can use the same mechanism.

I think this is defined in texmacs/keyboard/latex-kbd.scm

The following worked in the test I did (did not create the first item but I think you can define a command that does it)

(kbd-commands ("chklst" "Make a checklist" (make `itemize-check)))

I used chklst because I had already defined checklist as a macro copying yours, one can adapt all of the names.
I do not know if this is the “proper” way to define this type of command. Maybe someone else will know.

The itemize-check macro could be made to depend on an environment variable itemize-check-compact to render compactly or not.

It should also be possible to have that parameter appear in the menus when inside an itemize-check. Something like this should work.

(tm-define (itemize-check-context? t)
  (tree-is? t 'itemize-check))

(define-group itemize-tag itemize-check)

(tm-define (customizable-parameters t)
  (:require (itemize-check-context? t))
  (list (list "itemize-check-compact" "Compact")))

(tm-define (parameter-choice-list l)
  (:require (in? l (list "itemize-check-compact")))
  (list "false" "true")

Also have a look at the functionality in the Education styles. There is a “Multiple choice” tag there that is essentially a checklist.

Yes, this is the approach I want. Thanks. So here is my solution based on yours and jeroen’s:

my-init-texmacs.scm:

(define-group itemize-tag itemize-check)

(kbd-commands
  ("checklist"
    "Make a checklist"
    (cons (make `compact) (list (make `itemize-check)))))

your style file, or preamble:

<new-list|itemize-check|<value|aligned-space-item>|<macro|name|\<box\>>>
1 Like