# (Nonfatal but Symptomatic) Conflict of amsmath and mathstyle Packages

This was brought to my attention by our colleague, Lover of Structure, but since it affects my scalerel package (which began using the mathstyle package to detect the current math style, as of version 1.4) it is now my problem too.

I even think I know why it happens, and I have a workaround, but I was hoping for a more satisfying solution. Here is the MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathstyle}
\newcommand*{\testcmd}{$$M_{\texttt{k}}$$}
\begin{document}
$$M_{\texttt{k}}$$  \par
\testcmd
\end{document}


and here is the result.

In it you see two different sized subscripts (call them small and large, respectively), which would not seem obvious from code. Investigating, I found that:

1) with neither package loaded, both subscripts are large

2) with only amsmath loaded, both subscripts are small

3) with only mathstyle loaded, both subscripts are large

4) with both packages loaded (as in my MWE), the first is small, the second is large

5) with both packages loaded, if the definition of \testcmd is moved after \begin{document}, both subscripts are small (this is what I call the WORKAROUND).

6) If I load mathstyle before amsmath, the code breaks, saying \dfrac is already defined.

"Aha," I say to myself. It would appear that one of these packages is using a "feature" I've seen mentioned wherein certain commands are loaded just prior to \begin{document}. But this would seem to put a significant constraint on users of these two packages (or users of amsmath and scalerel); namely, one would be forbidden from defining math command shortcuts in their document preamble.

So, my questions are these: am I understanding the nature of the problem properly? Is there a fix that would still allow users to define math shortcuts in their preamble, while using the package combination?

-
early in the user's guide for mathstyle (texdoc mathstyle) it says "If you want to use this package with amsmath, it is important mathstyle is loaded after amsmath." some reasons for this are given later in the documentation. (the author of mathstyle isn't currently active with latex as far as i know.) – barbara beeton May 15 '13 at 16:21
\usepackage{mathtools} loads the full mh bundle and amsmath after which example looks fine for me. – texenthusiast May 15 '13 at 16:36
@texenthusiast Actually, replacing the amsmath with mathtools in my MWE left the behavior unchanged. – Steven B. Segletes May 15 '13 at 16:44
@StevenB.Segletes i don't know much, but see workaround and myouput – texenthusiast May 15 '13 at 16:59
Why not use \mathchoice in your package rather than use mathstyle. TeX would have been better without mathchoice but it is what it is but redefining every command that makes a sub or superscript or fraction just to save yourself three boxing operations is a high price to pay and will make your package incompatible with other things and have weird load order constraints. – David Carlisle May 15 '13 at 17:05

mathstyle does

\AtBeginDocument{\catcode\^=12\relax \catcode\_=12\relax}%


If you need definitions in the preamble you could just execute that earlier.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathstyle}
\catcode\^=12\relax \catcode\_=12\relax
\newcommand*{\testcmd}{$$M_{\texttt{k}}$$}
\begin{document}
$$M_{\texttt{k}}$$  \par
\testcmd
\end{document}

-
Thanks so much (to you and the removed but like-minded comment of Herr Vogt)! In addition, the scalerel manual compiles with this option set, appearing not to break anything. Are there any drawbacks/restrictions when employing this option? Let me attempt to see if this resolves Lover of Structure's larger problem before accepting the answer. – Steven B. Segletes May 15 '13 at 17:03
@StevenB.Segletes see my comment on your question. Stuff will break. – David Carlisle May 15 '13 at 17:06
Note to self: executing that \catcode command in package xyz means that package xyz needs to be loaded after \usepackage{verbatim} – Steven B. Segletes May 15 '13 at 17:13
@StevenB.Segletes That will be the first of a long list of incompatibilities and ordering issues make sure you have a big piece of paper for your notes:-) – David Carlisle May 15 '13 at 17:15