0

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

enter image description here

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.

Nasser
  • 20,220
  • \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
  • you can control maple's output – David Carlisle Jul 13 '20 at 18:35
  • @oliversm I do not use maplestd2e.sty? why do I need it for? I convert single expressions like I showed above, and write these to my latex file directly, then compile that with lulatex. How is using maplestd2e.sty will help in this? Or do you mean editing this file affect how Maple generates latex in the first place? Again, I do not export my worksheet to Latex from maple at all. I only export single expressions one at a time. – Nasser Jul 13 '20 at 18:47
  • @DavidCarlisle how can one control Maple's output? I showed above the command I use. There is no other options in it to change the output. If you know, please feel free to give an answer showing how and I will be happy to accept it. – Nasser Jul 13 '20 at 18:52
  • @Nasser, If you're taking the output of the maple expressions and using them directly in some latex file (either by copy and pasting or using something like listings) then use the closing sqrt symbol. The style file is is Maple is using LaTeX (e.g. fancy labels on maple plots perhaps), but it seems that is not the case for you in this instance. – oliversm Jul 13 '20 at 18:56
  • @Nasser I haven't used maple for 20 years but it was certainly possible, the maple code that generated the latex output was available and customisable – David Carlisle Jul 13 '20 at 19:00
  • @Nasser here https://www.maplesoft.com/support/help/Maple/view.aspx?path=latex%2ffunctions you just need to define maple functions each fuction that you are converting to latex. – David Carlisle Jul 13 '20 at 19:02
  • @DavidCarlisle thanks for the link. I will try to see if I can the information given there to improve the latex output from Maple. – Nasser Jul 13 '20 at 19:08

1 Answers1

1

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

enter image description here

\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} \begin{equation} \text{Compare} \quad \sqrt{y - 1}y \quad \text{to} \quad \oldsqrt{y - 1}y \end{equation} \end{document}

oliversm
  • 2,717
  • 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