[Tex/LaTex] Changing the format of ordinal numbers (\the…) and related strings

formattinglanguagesnumbering

I develop a package that improves LaTeX support for the Latvian language. One of the things I have here is different style used for things like Chapter 1, Theorem 1.1 etc. First, the phrase is written using ordinal numbers. Second, there is an ordinal indicator in a form of a period. By these rules, Chapter 1 becomes 1. nodaļa, which literally means 1st chapter. Accordingly, in the package I have to:

  • swap the name and the number in places like \@makechapterhead and \@part;
  • make the period appear everywhere where the number is printed or referenced, such as headings, \ref-s etc.

Of course, I want to support not only the standard classes, but also packages the user might load. I can see three ways how to do this:

Method 1: Redefine all relevant \the… commands to contain the period, and also swap \the… and text in appropriate places.

Cons:

  • This is not a universal solution — we add the \the… commands to the list manually
  • Per every new object type to be supported, up to two redefinitions may be needed — one for the number and one for the macro typesetting the heading/caption text
  • When a number not ending with a digit is constructed by some third-party package, it might end up with a period in the middle (Figure 1.a), which is wrong. In general, if a \the… macro we don't know about tries to use a \the… macro we have redefined, the output may contain superfluous periods.

Method 2: Eliminate the first method's Con #1 and partially Con #2 by altering \arabic instead of the \the… macros. But this will produce a period in all places where \arabic is used, meaning that there is a good chance to break something elsewhere.

Method 3: Do not change the number format directly, but rather add the period in places where the number is typeset, for example, figure/table captions, \@seccntformat, \ref, \numberline.

Pros:

  • The number of places where the value of \the… is output is largely limited, and packages usually don't introduce new ways to do it. This looks better than redefining all \the… commands.
  • If, however, a package has a custom place where the number is typeset, then it's most likely the new heading (like amsthm defines a macro to output Theorem 1). But we need to swap the number and the text, thus we have to change the new heading anyway! When we do this, we might as well add the period. Therefore, now only one redefinition per package (more precisely, per object type) suffices.

Cons:

  • Since now \ref, \numberline and similar things have to add the period, they have to expand and analyze the number to tell if it ends with a digit (we shouldn't put a period after the number 1a).
  • Not very semantic

Currently I have implemented method 3. But I don't particularly like the implementation. On the other hand, I also don't see any more convenient way to achieve the goal. So, the question: can the task specified in the first paragraph be done better than in the described solutions?

Best Answer

It seems that what you want to do is to change the number format from \arabic to \latord where \latord outputs a Latvian-style ordinal number when given a counter as input. From a semantic point of view, the best way to do this would be to change the relevant \the... commands as per your suggestion 1.

That is, you could do it the other ways (although bear in mind Martin's warning about redefining \arabic), but it's clearer for someone else reading your code what you are doing if you do directly what you are trying to do...

A number of packages might give you some clues as to how to build a \latord like macro:

  • fmtcount is quite sophisticated and includes several language options
  • nth is much simpler
  • engord more sophisticated than nth but without the language support of fmtcount

There aren't really that many \the... commands to redefine, and packages like titlesec can help automate some of the redefinitions...

Related Question