# What causes this strange interaction between glossaries and amsmath?

MWE: greatly trimmed from a large document; XeLaTeX; full TeXLive2012 completely up-to-date:

\documentclass{memoir}
\usepackage{fixltx2e}
\usepackage{amsmath}
\usepackage{mathspec}
\usepackage{glossaries}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\end{document}


Fails with "! Package mathspec Error: 'amsmath' must be loaded earlier than mathspec'."

Now, if I eliminate glossaries, or move it above mathspec in the file, things work. But it isn't very convenient to have to sort ones packages whenever anything gets a new dependency. And in any case, I do load amsmath before mathspec, don't I?

Really puzzled by this one. Any illumination would be most welcome.

(BTW, not strictly MWE, as it also fails with article.

• glossaries (in particular datatool-base) requires amsmath. The check performed by mathspec consists in redefining \RequirePackage to check whether the argument is amsmath (which is a very poor trick, IMO). – egreg Dec 5 '12 at 23:36
• @egreg: If you'd like to turn a brilliant comment into a brilliant answer (as usual!), I'll happily accept it. – Brent.Longborough Dec 5 '12 at 23:44

The problem is that one of the subpackages loaded by datatool, namely datatool-base has the line

\RequirePackage{amsmath}


and mathspec sets up a (very poor) check for possible loading of amsmath after it, which triggers the error even if amsmath has already been loaded when another package requires it.

The normal setup of LaTeX is not reloading packages, so the call by datatool-base should be completely harmless, as far as mathspec is concerned. This is undoubtedly a bug.

The package mathspec says

\let\original@RequirePackage\RequirePackage
\renewcommand\RequirePackage[2][]{
\ifstrequal{#2}{amsmath}
{\PackageError{mathspec}
{amsmath' must be loaded earlier than mathspec'}
{Edit the document so that amsmath' is required earlier than mathspec'.}}
{\relax}
\original@RequirePackage[#1]{#2}}
\@onlypreamble\RequirePackage
\let\usepackage\RequirePackage
\@onlypreamble\usepackage


which is quite bad code, in my opinion: \RequirePackage is redefined to check if its mandatory argument is exactly amsmath. So, if one says

\usepackage{mathspec}
\usepackage{amsmath,amsthm}


the check will fail and amsmath will be happily loaded after mathspec.

Judging from the comments in the package, the only reason why amsmath should be loaded before mathspec is a problem with the definition of \varTheta. This can be solved much more easily by changing lines 103–106, which read

103 \ifdef{\varTheta}
104   {\let\eu@cm@varTheta\varTheta}
105   {\relax}
106 \let\varTheta\Theta


into

\AtBeginDocument{
\ifdef{\varTheta}
{\let\eu@cm@varTheta\varTheta}
{}
\let\varTheta\Theta
}


and removing the final lines with the redefinition of \RequirePackage.

A workaround until the mathspec package is fixed:

\documentclass{memoir}
\usepackage{fixltx2e}
\usepackage{amsmath}
\usepackage{mathspec}

\makeatletter % undo the wrong changes made by mathspec
\let\RequirePackage\original@RequirePackage
\let\usepackage\RequirePackage
\makeatother

\usepackage{glossaries}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\end{document}

• Thanks. @egreg, perfect as usual. It fixes my problem, and I've forwarded it, with a reference to here, to the Maintainer. – Brent.Longborough Dec 6 '12 at 14:57
• Does this only work when used in the preamble? I tried to use it within a cls file (commenting out the \let\usepackage\RequrePackage line) but it seemed to only produce (a tonne of) errors. I finally was able to figure out a workaround such that I could include it properly within the preamble, by using this before I \usepackage{mathtools}, but leaving the other stuff in the class file. To clarify…mathspec needed to be loaded in the cls, and it caused no problems, but when mathtools was used in the preamble all hell broke loose. – mpacer Mar 30 '16 at 22:32
• @mpacer This is too vague, I think. You should remove \makeatletter and \makeatother, as far as I can see. The \let\usepackage\RequirePackage line is important. – egreg Mar 30 '16 at 22:34
• @egreg, tried that too, thought they looked out of place in a class file. But unfortunately, no dice. You shouldn't be able to use \usepackage before the declaration of a document class with (for example) \documentclass{book} in the .tex file itself, no? – mpacer Mar 30 '16 at 22:35
• Hmmm maybe what I should have done is add the \let\usepackage\RequirePackage in the .tex preamble, but not in the class file itself. – mpacer Mar 30 '16 at 22:38