# Does a current workaround for babel and indexing Roman-numeral page numbers exist?

Until recently, one could redefine \textlatin to expand to its argument in order to allow index entries using Roman numerals and babel when a non-ASCII encoding is loaded together with ASCII encodings.

The recent changes to babel now make that obsolete. If any non-ASCII encoding is present, an encoding selection is made, along with a font switch. This is done via \AtBeginDocument and the process related to \ensureascii.

Consider this MWE:

%!TEX program=pdflatex

\documentclass{memoir}

\usepackage[utf8]{inputenc}
\usepackage[LGR,T1]{fontenc}
\usepackage[greek,english]{babel}
\usepackage{makeidx}
\makeindex

\begin{document}
\frontmatter
\meaning\thepage

\index{\string\thepage}

\mainmatter
\meaning\thepage
\index{\string\thepage}

\backmatter
\printindex
\end{document}


The .idx file will have:

\indexentry{\string\thepage}{{\fontencoding  {T1}\selectfont  i}}
\indexentry{\string\thepage}{1}


That will not play well with makeindex for similar reasons as the former use of \textlatin. One will lose all one's index entries in the front matter.

Is there any elegant workaround that anyone has discovered? Or perhaps stick with an old version of babel?

I hesitate to use sed to stroke the .idx file in conjunction with latexmk, which otherwise does a marvelous job at creating the document on which I am working.

Although I understand why this design decision is quite helpful in many places, it does complicate indexing.

• Depending on how the LGR encoding is used, just something like \renewcommand\BabelNonASCII{} in the preamble could do the trick. Interestingly this issue has never been reported as a bug (and I think it's a bug). I'll investigate a general solution. – Javier Bezos Nov 6 '16 at 18:47
• Sorry I didn't report it as a bug; there had been previous internet chatter on the matter of \textlatin so I assumed it simply is what it is. – Charles P. Schaum Nov 7 '16 at 23:32

You can make \ensureascii robust against \edef and \write expansion; then use a terrible hack to get rid of \ensureascii when the index file is written out.

\documentclass{memoir}

\usepackage[utf8]{inputenc}
\usepackage[LGR,T1]{fontenc}
\usepackage[greek,english]{babel}
\usepackage{etoolbox}
\usepackage{makeidx}
\makeindex

\makeatletter
\AtBeginDocument{%
\robustify\ensureascii
\def\@@@wrindexm@m#1#2{%
\if@filesw
\global\let\@gtempa\@idxfile
\endgroup % end \@sanitize
\begingroup
\let\ensureascii\@firstofone
\everyeof{\noexpand}%
\immediate\write\@gtempa{\string\indexentry{#1}{\scantokens{#2\@empty}}}%
\fi
\endgroup
}%
}
\makeatother

\begin{document}

\frontmatter

\meaning\thepage

\index{\string\thepage}

\mainmatter
\meaning\thepage
\index{\string\thepage}

\backmatter
\printindex
\end{document}


The contents of the .idx file will be

\indexentry{\string\thepage}{i}
\indexentry{\string\thepage}{1}


Some words of explanation. memoir writes the index entries in the aux file and makes the idx file(s) only at end document. When the index entry is first written out, \ensureascii is expanded. So the first thing to do is making it robust against expansion during a write operation. Next we want to get rid of it when the idx file is written. Here the problem is that the arguments to \@@@wrindexm@m are read verbatim, so we need to kill the scope of \@sanitize and reread the second argument with the standard setup, locally making \ensureascii.

This will only work with memoir, of course.

• Sorry if I was unclear. It seems that makeindex doesn't want to resolve forms of \indexentry where there is anything but numbers\letters in the second argument. The Latex Companion p.649f. puts formatting, etc. for \indexentry in the first argument. Its solution is either to use | followed by a command, |see |seealso |hyperpage or to redefine \index or put it in a user-defined macro. The old way using \textlatin allowed for a quick and dirty workaround that effectively got rid of it in the second argument. Memoir, etc., also redefine \index. – Charles P. Schaum Nov 7 '16 at 23:15
• @user32053 The hacks get even more horrible, but this works for memoir. – egreg Nov 8 '16 at 0:22
• @egreg How would the horrible hack look with \usepackage[makeindex]{imakeidx} and a normal class (scrbook in my case, but imho it is would be the same as with book). – Ulrike Fischer Dec 22 '16 at 9:04

An alternative solution is to zap babel's redefinition of \@roman and \@Roman.

...
\makeatletter
\let\save@roman\@roman
\let\save@Roman\@Roman
\usepackage[greek,english]{babel}
\let\@roman\save@roman
\let\@Roman\save@Roman
\makeatother
...


This won't work if you want to create Roman numerals when the language is switched to Greek, but at least it is simple and self-documenting.

• This also works with memoir, as well as with non-memoir documents. BTW, I link to this question page in the nameauth documentation because the issue has come up. Both solutions here should be helpful to people. – Charles P. Schaum Mar 15 '17 at 0:08