[Tex/LaTex] Gradient symbols and letter f mtpro2

gradientletterspacingmtprospacing

So I notice that the spacing between \nabla and f
in math mode when using mtpro2 is really wide (for me, at least). Here is the result of typing \nabla f with mtpro2.

gradient of f

I wonder if there is a way we can adjust the symbol \nabla
based on the next non-space letter, say:

  1. If the next non-space letter is f, then execute \kern -0.2 em f,
  2. else, stay at usual.

I have been looking around, but could not find a satisfying solution. Any help
is highly appreciated.

Best Answer

Here's a LuaLaTeX-based solution. It sets up a Lua function called nabla_f that does most of the work, and it assigns this function to the process_input_buffer callback, making it operate on all inputs before TeX starts is usual processing.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{times,mtpro2} % text and math fonts
\usepackage{luacode}
\begin{luacode}
function nabla_f ( s )
   return ( s:gsub ( "\\nabla%s*f" , "\\nabla\\mkern-4mu f" ) )
end
\end{luacode}
\directlua{luatexbase.add_to_callback("process_input_buffer", nabla_f , "nablaf" )}

\begin{document}
$\nabla{}f$ $\nabla f$ $\nabla h$ $\nabla k$
\end{document}

Addendum: If you also want to change the amount of whitespace between \partial and f, while keeping the instruction to change the distance between \nabla and f, I suggest you replace

   return ( s:gsub ( "\\nabla%s*f" , "\\nabla\\mkern-4mu f" ) )

with

   s = s:gsub ( "\\nabla%s*f" ,  "\\nabla\\mkern-4mu f" ) 
   s = s:gsub ( "\\partial%s*f", "\\partial\\mkern-4mu f" )
   return s

Of course, you can (and should) change the new instance of -4mu to whatever the optimal adjustment amount may be.

Related Question