TransWikia.com

Command to convert an input into sequential numbering

TeX - LaTeX Asked by user243088 on March 3, 2021

Is there a command able to convert an input into sequential numbering (and when that input is used again the command will recall the previously assigned number)?

Context: I have various scientific experiments discussed. In the source TEX file, I’m using the experiment number referred to my lab notebook, but in the final PDF I would like them to be sequentially numbered. Clearly, I would like the same number to be used for the same experiment. The first time the experiment is mentioned it should receive a new progressive number, otherwise it should recall the number it was assigned to him.

% the magicnumbering is a command name that I invented for this purpose!

documentclass{report}

begin{document}

I did magicnumbering{experimentA} before magicnumbering{experimentB}, but magicnumbering{experimentC} was done even before magicnumbering{experimentA}.

end{document}

I would like this MWE to print out:

I did 1 before 2, but 3 was done even before 1.

I’m sure there is a very simple solution that I can’t find. I’m sorry for my ignorance, I wish you could help.

One Answer

The following does this by storing the keys in a property list (using expl3). To change the formatting of the output, you can change the definition of magicnumbering_output:n.

documentclass[]{report}

ExplSyntaxOn
prop_new:N g_magicnumbering_numbers_prop
int_new:N g_magicnumbering_current_int
tl_new:N l_magicnumbering_output_tl
cs_new_protected:Npn magicnumbering_output:n #1
  {
    #1
  }
cs_generate_variant:Nn magicnumbering_output:n { V }
cs_new_protected:Npn magicnumbering_parse:n #1
  {
    prop_get:NnNTF
      g_magicnumbering_numbers_prop {#1} l_magicnumbering_output_tl
      { magicnumbering_output:V l_magicnumbering_output_tl }
      {
        int_gincr:N g_magicnumbering_current_int
        prop_put:NnV
          g_magicnumbering_numbers_prop {#1} g_magicnumbering_current_int
        magicnumbering_output:V g_magicnumbering_current_int
      }
  }
NewDocumentCommand magicnumbering { m } { magicnumbering_parse:n {#1} }
ExplSyntaxOff

begin{document}
I did magicnumbering{experimentA} before magicnumbering{experimentB}, but
magicnumbering{experimentC} was done even before
magicnumbering{experimentA}.
end{document}

enter image description here

Answered by Skillmon on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP