3

\DeclareMathOperator works fine without \setmainfont.

\documentclass[a4paper]{standalone}
\usepackage{amsmath}
\DeclareMathOperator{\grad}{\nabla}
\usepackage{unicode-math}
\begin{document}
$\nabla \grad f$
\end{document}

correct output

However, as soon as I include the command \setmainfont, the \DeclareMathOperator stops working.

\documentclass[a4paper]{standalone}
\usepackage{amsmath}
\DeclareMathOperator{\grad}{\nabla}
\usepackage{unicode-math}
\setmainfont{texgyrebonum}
\begin{document}
$\nabla \grad f$
\end{document}

enter image description here

It happens with both XeLaTeX and LuaLaTeX. Why does this happen? How to fix it?

  • I'm not sure \nabla should be treated as an operator. – egreg Jan 11 '16 at 13:33
  • 1
    You don't have to use nabla as a TeX math operator. \newcommand*\grad{\nabla}, \newcommand*\grad[1]{\nabla{#1}} or may be \newcommand*\grad{\mathop{}\!\nabla} (or the other variant). But not a TeX matho operator. – Manuel Jan 11 '16 at 13:33
  • Okay. I'll do that. Doing that solves the problem. – user7013 Jan 11 '16 at 14:21
  • 3
    By the way, \DeclareMathOperator is broken in unicode-math. :( – egreg Jan 11 '16 at 15:10
2

It doesn't need to have such spacing.

\newcommand*\grad[1]{\nabla\mathord{#1}} % and use like \grad{f}

If you want to ensure a small space (like before differentials, for instance) you could add \mathop{}\!

\newcommand*\grad[1]{\mathop{}\!\nabla\mathord{#1}}

Of course you can drop the argument and the \mathord{#1} part, but I think it does no harm and is convenient here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy