# Odd interaction between hyperref, breqn, and underscores

My problem may have a similar root cause as this question, and although I've got a working solution I'm happy with, I'm puzzled as to why this happens at all. MWE:

\documentclass{article}
\usepackage{mathtools}
\usepackage{breqn}
\usepackage{hyperref}
\begin{document}
\section{Content}
\begin{figure}
\mbox{Figure content}
%%% Version 1, causes "Rerun to get cross-references right" warning every time
%%% if hyperref, mathtools, and breqn loaded simultaneously.
%\caption{dirac\_example.m}
%%% Version 2, works fine
\caption{dirac\textunderscore{}example.m}
%%% Version 3, works fine
%\newcommand{\mycaption}{dirac\_example.m}
%\caption{\mycaption{}}
\label{dirac}
\end{figure}
\end{document}


As noted in the comments, the first version of my figure caption causes pdflatex to constantly throw "Rerun to get cross-references right" warnings. Removing either of hyperref or breqn made the problem go away, and obviously, replacing the escaped underscore with a \textunderscore{} or another definition fixed the problem as well.

1. Tried with original TL 2010 DVD.
2. Tried with current packages, updated this morning from CTAN.
3. Tried moving breqn after hyperref.

No difference on any of those. All exhibited the error, and though I've not tried reinstalling the original DVD, I assume what works on my updated install would work there, too.

• First of all mathtools is irrelevant (it seems), and breqn is just evil. I'll pass your example on the the breqn maintainers (I'm one, but I mostly work with mathtools) Mar 3, 2011 at 20:25
• I could have sworn that I had to include mathtools to make breqn work, but on further review, I must have had something else going on. breqn does stand alone. Mar 3, 2011 at 21:50

The problem is that when the .aux file is read at document start, the catcode of _ is 11, while at the end it's 12. Indeed, putting \catcode_=11 just before\end{document} "cures" the problem.
I suspect this has something to do with the LaTeX3 part of breqn, where the catcode of _ is indeed set to 11. And putting
\showthe\catcode\_

after loading breqn shows 11, while after \begin{document}` it shows 12.