improving spacing between math expressions automatically?

I know one can force extra space using \, but I am working with auto-generated Latex code. Sometimes the spacing between math expressions is too small and sometimes it is OK. Here is an example

Generated using

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$\sqrt{y -1} y$

$\sin(x)\tan(y)$

\end{document}


I am wondering if there is a setting, that can be put in preamble only to help with this problem? (to tell Latex to automatically add a bit of extra space between 2 different separate math expressions, like it did with the \sin and the \tan?

Again, this is autogenerated Latex (CAS generated by Maple in this case) and it is not possible to manually edit the Latex each time to add \, where it is needed.

Would using different Latex font than default help with this? Or will this happen with any font used?

To answer comments

These are autogenerated. This is how CAS Maple generates them, regardless of orginal form, y \sqrt() or \sqrt() y or any other.

expr:=sqrt(y-1)*y;
latex(expr,output=string)
"\sqrt {y-1}y"

expr:=y*sqrt(y-1);
latex(expr,output=string)
"\sqrt {y-1}y"

expr:=y*(sqrt(y-1));
latex(expr,output=string)
"\sqrt {y-1}y"

expr:=(sqrt(y-1))*y;
latex(expr,output=string)
"\sqrt {y-1}y"


It is ok if there is no automatic way to help with this, I just thought to ask just in case there might be some parameter one can adjust in Latex to improve on the spacing for such cases.

ps. I use lualatex if it makes any difference.

• \sqrt{y-1}y is very bad notation anyway and no space will improve it. – egreg Jul 13 '20 at 15:47
• The spacing between ) and \tan comes from the fact that the former is a Close atom, while the latter is an Op atom. Completely different situation. – egreg Jul 13 '20 at 16:10
• can you not generate (\sqrt{y-1})y or y\sqrt{y-1} or \sqrt{y-1} \times y any of which would be far easier to understand than the original, even if spaced. The spacing you are getting is not font dependent or configurable as y is a mathord so gets no space in this context. – David Carlisle Jul 13 '20 at 16:14
• Adding a closing symbol to sqrt does improve the spacing (and looks much nicer IMHO), and is easily achieved by a few lines in a LaTeX preamble. Herein lies the question, is Maple outputting LaTeX which you then input in a document, or is Maple using LaTeX for its own purposes (e.g. plotting windows)? If it's the former, then the solution linked would work. If it is the latter, perhaps there is a way to tell Maple to run some LaTeX preamble. I don't know about Maple but the python equivalent is text.latex.preamble in mpl.rcParams. – oliversm Jul 13 '20 at 18:23
• The styles Maple uses can be found in maplestd2e.sty in your Maple installation folder, so if you add the preamble or redefine sqrt there you could hack a solution together. – oliversm Jul 13 '20 at 18:25

1 Answer

If you are using the output of Maple in a LaTeX document then one method which slightly improves the spacing, (and I think looks nicer), is to close off the sqrt symbol. How to do this is mentioned here: "Closed" (square) root symbol and gives the following output

\documentclass{article}
\usepackage{amsmath}
\usepackage{letltxmacro}

% Give a nicer sqrt symbol.
\makeatletter
\let\oldr@@t\r@@t
\def\r@@t#1#2{%
\setbox0=\hbox{$\oldr@@t#1{#2\,}$}\dimen0=\ht0
\advance\dimen0-0.2\ht0
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}}
\LetLtxMacro{\oldsqrt}{\sqrt}
\renewcommand*{\sqrt}[2][\ ]{\oldsqrt[#1]{#2}}
\makeatother

\begin{document}
$$\text{Compare} \quad \sqrt{y - 1}y \quad \text{to} \quad \oldsqrt{y - 1}y$$
\end{document}

• Thanks, I will add this to my preamble and try it. Yes, this is what I want. I can add anything to the preamble OK, but I can't edit the Latex generated by hand, since it is auto-generated each time. – Nasser Jul 13 '20 at 19:04
• After testing it more, I found it does not work with Asna font, which I like. If you add the following \usepackage{unicode-math} \setmathfont{Asana Math}[Scale=MatchLowercase] at the top of your example, you will find it no longer works. I do not know if you know a workaround. It seems this is dependent on what math font is used. Using default font, it works with no problem. Thanks. – Nasser Jul 13 '20 at 19:35
• @Nasser that is then a matter of adapting the closing sqrt to work with XeLaTeX or LuaLaTeX, but this is not something I know, and is perhaps a separate question, getting this to integrate with your desired font. – oliversm Jul 13 '20 at 20:48