Code Review
latex3 add tag
13 days ago निरंजन

Suppose there are two sequences and I want them to always have the same length in order to match their items later. I want two separate commands for adding items to them. I came up with this:

It results in:

and this is correct considering my need.

Does this look okay? Am I violating any standard conventions? Is there any further optimisation possible?

Top Answer
8 days ago Skillmon

With the conditions

  • that seq A is always assigned first and
  • that seq A is guaranteed to have more meaningful entries than seq B (which somewhat follows from the first point)

you can optimise your code a bit:

Alternative methods of input for this syntax could be a key=value based input (though this puts all entries together in a single argument – so doesn’t meet your requirements “I want two separate commands for adding items to them” – nevertheless I think the interface is pretty clean, and quite easy to implement, so here goes nothing):

Output (regardless of whether the key=value or the first interface is used):

foo.png


If your use case doesn’t guarantee the above two constraints you could still implement something similar to the first variant, just with a state variable (we can’t use a boolean here since there are three states, initial, A, and B – we use an integer with the values 0, 1, and 2 for these):

Output:

foo.png


Note that \seq_set_item:Nnn <seq-var> { -1 } is a rather slow operation. If there was a finalising call for the setup block we could further optimise our first idea by introducing a state variable that monitors how many items overhead are already built up (this is close to your code, but a bit faster by simply counting the number of items while we go, instead of every time we add to b):

foo.png


Lastly, we can reintroduce the constraint that we don’t know which sequence is added first and which second, and which one contains more items. For that we use positive values to indicate an overhead of A, and negative ones to indicate an overhead of B:

foo.png

4 days
निरंजन — Last Wednesday 12:05

Thanks for all the help.

निरंजन replying to Skillmon — Last Wednesday 12:04

Ahh, great. I will see if that part can be added o the syntax.

2 days
Skillmon replying to निरंजन — Last Monday 21:12

that one (basically) trades speed for a tiny bit of memory space (which is often a trade off in programming). Still I personally prefer it (and if you can put a start and end command in the syntax you can even reuse some internal int you’re likely have laying around anyway, so it might not even need more memory)

13 hours
निरंजन — Last Monday 08:23

The last approach, with state variables is pretty new to me. It has raised many questions in my mind. I am reading about it. Will ask my doubts one by one. Thanks for all the work and suggestions.

2 hours
निरंजन — Last Monday 05:54

@Skillmon about the key-val interface, I agree. The syntax I am currently working with is non-standard, but it’s not public code, it’s at my workplace and is some private production code, so redesigning it would not be possible. A lot of other things are dependent on it. I would not have designed the syntax like this in the first place. Thanks for the suggestion nevertheless. I have a package idea for which I can make use of this 😃

निरंजन — Last Monday 05:52

@Skillmon Lovely idea!! I loved your design that uses \seq_set_item:Nnn. It suits my need. I will use it.

2 days
निरंजन replying to Skillmon — Saturday, 14th Jun 2025 13:38
  1. Yes, seq ‘A’ is guaranteed to have more items explicitly set, but I would love to know the alternative also where it may or may not have the most number of items.
  2. Yes, seq ‘A’ will always get an item before seq ‘b’.
निरंजन replying to Skillmon — Saturday, 14th Jun 2025 13:34

deleted

3 days
Skillmon — Wednesday, 11th Jun 2025 19:38

@निरंजन, re: your question, is seq A guaranteed to be the one getting more items explicitly set? Is seq A guaranteed to always have the item assigned prior to B getting the matching item? Knowing more about the constraints and edge cases to consider better code optimisations can be suggested.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.