This MWE produces repeated Label(s) may have changed warnings despite the output files not changing. The same problem has been discussed in How to diagnose a permanent "Label(s) may have changed" warning?, but lacking an MWE could only symptomatically be solved (by removing the underscore from the \caption). Now, I do not want to remove the underscore from the caption.
\documentclass{article}
\usepackage{hyperref}
\AtBeginDocument{
\catcode`_=12
\begingroup\lccode`~=`_
\lowercase{\endgroup\let~}\sb
\mathcode`_="8000
}
\begin{document}
\begin{figure}
\caption{$x_y$}
\label{z}
\end{figure}
\end{document}
An even more minimal MWE:
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\catcode`_=12
\begin{figure}
\caption{$x_y$}
\label{z}
\end{figure}
\end{document}
Some background: I use the
\catcode`_=12
hack after the discussion (and egreg's answer) at Underscore in textmode vs mathmode, among other things, to allow use of underscores in DOIs and bibtex keys. But it seems to interfere with hyperref and captions.
What can I do?
\catcodewithin thedocumentenvironment, and that the.auxis usually read\AtBeginDocument. So, there's no correspondence between the_read in by the.auxand written out by\caption. – Werner May 19 '15 at 23:11\catcodebefore\begin{document}and be done? Seems to work. So what is the\AtBeginDocument{...}for then? Just to prevent the\catcodefrom interfering with anything else in the preamble? Is there anything such as\BeforeBeginDocument? – bers May 19 '15 at 23:15etoolboxprovides some environment hooks which you can use (like\BeforeBeginEnvironment{document}{<stuff>}and\AtBeginEnvironment{document}{<stuff>}). – Werner May 19 '15 at 23:20\AfterEndPreamblefrometoolboxdid not work. – bers May 19 '15 at 23:28