Current location - Quotes Website - Personality signature - What are the techniques in TeX programming?
What are the techniques in TeX programming?

We all know that TeX's \romannumeral can convert subsequent Arabic numerals into lowercase Roman numerals. \romannumeral has several properties: it will expand subsequent tokens until a complete number is obtained;

If the number is not a positive integer, its expansion result is empty; 000 is m, 2000 is mm , 10000 is 10 m. How many times it is 1000 is how many ms. This is the main trick of \ltx@CarNumth implementation. Now look at the definition of \ltx@CarNumth: \ltx@CarNumth is started by \romannumeral and uses property 1 to achieve the effect of expanding it twice to get the result. The subsequent three \expandafter expands \ltx@GobbleNum twice, which is also started by \romannumeral: \LTXcmds@num here is \number or \the\numexpr, used to convert parameter #1 to Arabic numerals (recorded as n), connected with the following 000, are converted into n m's by \romannumeral. \LTXcmds@GobbleNum is tail recursive: used to generate n \csname LTXcmds@Gm. Finally, the recursion is terminated. It expands into \LTXcmds@Gm by eating one parameter and then returning an \endcsname, ending the previous \csname. After eating n parameters (\ltx@CarNumth is to get the nth item, and should only eat n-1 parameters, so the definition of \ltx@CarNumth has an empty group at the end for placeholder, avoiding calculation) , it is obtained that \ltx@zero is the character constant 0, which terminates the expansion of \romannumeral. Finally, \LTXcmds@CarNumth does the cleanup: TeX's largest integer is 2147483647, so \ltx@CarNumth can be used up to 2147483. But it may not be so long before TeX's memory is insufficient. Using l3benchmark for performance testing on my old laptop (T9600), \ltx@CarNumth is an order of magnitude faster than \tl_item:nn. The script used for testing is that if the machine performance is better, the difference may not be so obvious. Just take this example. If you are interested, you can take a look at the source code document source3.pdf of LaTeX3. It can be said to be a master of various techniques. For example, the implementation of \prg_replicate:nn used in the test script above is Very technical.