[Tex/LaTex] unicode-math breaks \DeclareMathOperator

amsmathmath-modeunicode-math

Problem Statement

I'd like to introduce a new math operator (the divergence operator). Unfortunately the macro I'd like to use is already defined, so I "free" it using

\let\div\undefined

and declare the new operator

\DeclareMathOperator{\div}{div}

If I am using only amsmath everything works fine, but as soon as I add unicode-math everything breaks.

Implementation

Consider the following MWE

\documentclass{scrartcl}\pagestyle{empty}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Pagella Math}
\let\div\undefined
\DeclareMathOperator{\div}{div}
\begin{document}
\[ \int_V \div \vec{F} \, \mathrm{d}V = \oint_A \vec{F} \cdot \vec{n} \, \mathrm{d}A \]
\end{document}

Output

The output I get contains the output of the original \div macro.

real

Expected Output

expected

Best Answer

unicode-math does most of its work at \begin{document}, so you have to delay your declarations too:

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Pagella Math}

\newcommand{\diff}{\mathop{}\!\mathrm{d}} % better than using \,\mathrm{d}

\AtBeginDocument{
  \let\div\relax
  \DeclareMathOperator{\div}{div}
}

\begin{document}
\[
\int_V \div \vec{F} \diff V = \oint_A \vec{F} \cdot \vec{n} \diff A
\]
\end{document}

enter image description here

Note that this is only needed because you want to override a command defined by unicode-math. Using \DeclareMathOperator{\Div}{div} wouldn't need special precautions.