I am copying a long book with musical pieces throughout, set with gregorio tex. Each musical piece has a \label and is entered in one of several indexes created using imakeidx.

The document has multiple formats of page numbers: roman, arabic, and then several other notations, e.g. [3], 7*, and <<22>>, based on different divisions of the overall document. Each section starts with a value of 1, but formatted differently.

I'd like \label/\pageref and \index to function seamlessly to give the right page with the right format based on the location of the chant.

I have previously posted about this problem here: More than two page formats for \label / \ref and \index with imakeidx

I was given the tip by David Carlisle that I can use \index{item|foo} to apply a format to the index page using a function \foo that I define. This is definitely on the way to solving the problem.

The problem is that there is basically one \index statement in the program - buried in a function \dostuff called by the gregorio tex package when a musical piece is loaded.

So I have an existing function


I'd like to change the format of the index by redefining \foo at the start of each section in the document where the page number format changes, such as

% Switch to star pages now....

Instead, when I do this, the index only uses the most recent definition of foo at the time the \printindex executes, so every page number gets displayed in the same format, in this case, e.g. [7].

Is there a way to fix the definition of \foo or the \index command to get the page reference to get written to the index file at the time the index statement is given, so to use the 'current' definition of \foo and thus make sure the right format is applied?

I could redefine all of the macro \dostuff at the start of each document division, but it is a long macro and that would be a pain. If it is possible, just changing \foo would be a lot simpler, since that's all that changes.

  • Okay, nice lengthy description. How about supplying a base, minimal document (piece of code) we can work with, that contains all of your setup across multiple page number formats, together with the \index commands... something we can work and play with. Can you do that? Remember, it doesn't have to contain everything, just enough so we can see the issue you're dealing with... like two different page formats and an index. Can you do that?
    – Werner
    Commented Jan 14, 2022 at 1:53
  • It's a book with many macros. So, no, I can't really. But it seems it is the usual annoying TeX question about timing of execution of various parts of code. If \index is itself buried in a macro, when does the formatting macro after the bar get executed and how can I have that expanded at the time of the writing of the \index command and not at the time of the \printindex?
    – Rob LEduc
    Commented Jan 14, 2022 at 4:47
  • you really could make an example, anyone trying to answer will need an example to test. Your book macros are not relevant just make a small document with some sections and a command that calls \index and a few entries. One question do your sections where you switch definition start a new page (easier case) or do you start the sections mid page so need two definitions of the index on the same page and if so which page format does the page use, first or last section? Commented Jan 14, 2022 at 8:12
  • basically instead of redefining \foo change your macro to call \myindex{..} then define \myindex in each section as \index{#1|fooa} or \index{#1|foob} where \fooa and \foob are your two (or more) page number decorations. Commented Jan 14, 2022 at 8:15
  • Yes, I was seeking to avoid that because \myindex is very long and the only change in the code is to exchange three characters at each division e.g. #1* to [#1] .
    – Rob LEduc
    Commented Jan 14, 2022 at 10:34

1 Answer 1


This question was a follow-on question, as noted in the post. The answer was posted in the original question thread, linked in the top post here, but I repeat it for completeness.

The above solution in the comments thanks to David Carlisle solves the problem. In my function, I define


where foo is defined, e.g.


to get starred page numbers, and similarly for other formats.

I have to then redefine the function \bigfunction at the beginning of each section to get different versions of foo for each section.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .