# breqn with superscript in a newcommand

I am getting unfortunate behavior with the the most recent version of breqn.sty with regards to how it handles a newcommand that begins with a superscript. Considering the following code

\documentclass{article}
\usepackage{breqn}
\newcommand{\hc}{^\dagger}
\begin{document}
\begin{dmath}
A\hc
\end{dmath}
\end{document}


This code will not stop compiling, and hangs on this error:

! Missing { inserted.
\penalty
l.6   A\hc

! Missing } inserted.
<inserted text>
}
l.7 \end{dmath}


(Note that older iterations of breqn.sty did not give me this issue.)

However, if I just replace \hc with ^\dagger, everything works. Also, if I replace dmath with any of the standard math environments, it works (I've been using this particular newcommand for years).

Is it bad practice to have a superscript begin a newcommand? Is there a way to modify my \hc to make the above code just work?

-

The way breqn works is rather complex, and so I'm not surprised that it fails in the case you give. I'd suggest sticking rigidly to the formally-allowed syntax for superscripts:

\documentclass{article}
\usepackage{breqn}
\newcommand{\hc}{^{\dagger}}
\begin{document}
\begin{dmath}
A\hc
\end{dmath}
\end{document}


As Bruno observes, this is to do with category codes. The following also works

\documentclass{article}
\usepackage{breqn}
\catcode\^=12 %
\newcommand{\hc}{^\dagger}
\begin{document}
\begin{dmath}
A\hc
\end{dmath}
\end{document}


as breqn makes ^ an 'other' character.

which works fine.

-
I strongly suspect that it has to do with the catcode of ^ being set to active by breqn \AtBeginDocument. Can't test at the moment, though. – Bruno Le Floch Oct 11 '11 at 16:54

To complement Joseph's answer and Bruno's comment, here's some analysis of the situation.

breqn changes all mathcodes to "8000 so that characters in math mode behave like macros; and changes also the meaning of commands such as \dagger.

The meanings of ^, A and \dagger become

> ^=macro:
#1->\mathsup {\protect \subsupstyle #1}.

> A=\protected\long macro:
->\@sym A\math_sym_Var:Nn \mg@Latin {41}.

> \dagger=\protected\long macro:
->\@sym \dagger \math_sym_Bin:Nn \mg@bin {79}.


where \mathsup is an alias for the usual category 7 ^.

With \newcommand{\hc}{^\dagger} given in the preamble, ^ still has category 7 and such remains when \hc is expanded. But an important property of category 7 ^ in math mode is that it keeps expanding the next token until an unexpandable one is found (and it has better be a mathchardef token or a chardef token or a left brace). After a good deal of expansions, in the present case ^ is presented \penalty, which is not a good token after it.

The safest definition would be

\begingroup\lccode?=^
\lowercase{\endgroup\newcommand{\hc}{?{\dagger}}}


or

{\catcode^=12 \gdef\hc{^{\dagger}}}


as changing at that point the catcode of ^ could be dangerous. Maybe issuing

\catcode^=12
\newcommand{\hc}{^{\dagger}}


just before \begin{document} may not have unexpected consequences. In any case, defining \hc with a catcode 7 ^ should be avoided, as it can interfere with the workings of breqn: there's quite a difference between

\mathsup{\protect\subsupstyle \dagger}


and

\mathsup{\dagger}


The latter is what LaTeX would see with the catcode 7 ^` definition.

-