Enumeration with reversed numbering

Enumeartion with reversed numbering is very useful for listing items such as publications in CV. Latex has the package etaremune to do this. I suppose I can create a custom macro for this but wonder if somebody has already done this.

1 Like

I think one would be able to do that if one would be able to find out which is the last item number in a list, and I have not been able to do so.
It is not last-item-nr. Does anyone have an idea?

This is indeed a good problem. A not-so-perfect solution can be this:

(document 
  (new-list "sub-rev-enumerate" 
     (value "aligned-dot-item") 
     (macro "name" 
       (concat "[" 
               (minus (get-binding "rev-list-size") (arg "name"))
                "]")))
  (assign "rev-enumerate" 
     (macro "body" 
        (document (sub-rev-enumerate (arg "body"))
              (set-binding "rev-list-size" (plus (value "last-item-nr") "1"))))) "")

but it is using a global counter to keep track of who many item one has and then use it in a second typesetting passage. This works like references, so one needs multiple passes to have the correct numbering. I guess there is no better solution. The problem with this macro is that it is using a single counter which will not work when one has multiple reverse lists in a document.
Exercise for the reader: modify it to use a different counter every time the reverse-enumeration environment is invoked.

1 Like

I am revisiting this problem. Looking at std-list.ts I arrived at this so far:


It works as intended and the numbers updates dynamically wherever I insert new items. Now I just need to replace the number 10 by the total number of items in current level. My plan is to set up a new counter, say
image
and update it whenever a new item is created or removed, and keep it only in the scope of the list. But I haven’t understand the overall logic of std-list.ts and some syntax of the source.

What about my proposed solution with get-binding? You just need a way to figure out a new counter for every enumerate environment, but this should not be difficult since you can keep a global counter of such environment and use the variable rev-list-size-X (where X is the ordinal of the environment) to save the number of items there. A variable name rev-list-size-X can be constructed from rev-list-size- and X via merge I think. There are many examples in the .ts files on how to construct programmatically new identifiers via macros.

I’ll try to understand the code. Besides the global scope, your code works but with a minor flaw, there are two new lines after the list, which can’t be removed manually:
image
I didn’t find where the newlines come from.

1 Like

Maybe the set-binding should be inserted with a surround instead of document.

What’s the function of set-binding and get-binding? I find no documentation or definition of them.

They define variables in the environment of the document. I just looked at some code in the various packages. For example equation labels are set in this way


  <assign|equation-lab|<\macro|body|lab>
    <\surround|<set-binding|<arg|lab>>|<space|5mm><with|mode|text|font-shape|right|(<arg|lab>)>>
      <\equation*>
        <arg|body>
      </equation*>
    </surround>
  </macro>>

This is also an example on how to use surround with set-binding, I guess this will get rid of unwanted space.

1 Like

assign also sets environment variables, what’s the difference with set-binding. Also there must be difference between value and get-binding.

Still I can’t find a way to have it work if I have multiple reverse lists. I think it’s not about the list level. It will not work if i have two level-1 lists.

I checked that last-item-nr displays the number of current item, which is the same as item-nr and the-item. How does
(set-binding "rev-list-size" (plus (value "last-item-nr") "1")))
find the total number of items?

My understanding is also superficial. Bindings persist across typesettings of the document, while variables are reset when TeXmacs restart to typeset the document. References are stored in bindings, that is why one can use references to things which appear later in the document. Since you need some info which is available also later (the number of items is known only when the list environment is closed) you have to store in such a way that in the next run it will be available. As far as I understand, standard macro variables are not while bindings are. At the end of the item list last-item-nr counts how many items you have.

Putting the set-binding in surround will change the layout (the list becomes very compact) so it will not match those of the normal lists. I didn’t figure out a corret place to use the surround so that the layout will not change.

For record, I think I can live with the following work-around if I am not changing the list very often. Define as many reverse enum environment as I need in the preamble. Set the numbers after minus to be n+1 if your list size is n, which can be found by setting the number to 0 first if I don’t know it a priori.

<new-list|publications-1|<value|aligned-dot-item>|<macro|name|<enum-tag|[<minus|21|<arg|name>>]>>>

<assign|publications|<\macro|body>
  <\with|enumerate-level|<plus|<value|enumerate-level>|1>>
    <compound|<merge|publications-|<enumerate-reduce|<value|enumerate-level>>>|<arg|body>>
  </with>
</macro>>

\;

<new-list|grants-1|<value|aligned-dot-item>|<macro|name|<enum-tag|[<minus|11|<arg|name>>]>>>

<assign|grants|<\macro|body>
  <\with|enumerate-level|<plus|<value|enumerate-level>|1>>
    <compound|<merge|grants-|<enumerate-reduce|<value|enumerate-level>>>|<arg|body>>
  </with>
</macro>>

The numbers get updated immediately, so I don’t have to update the whole document (or recompile several times as in LaTeX), which is very nice.