TransWikia.com

Rules for space when using mathrm{}

TeX - LaTeX Asked on July 10, 2021

I am using auto-generated Latex code from CAS system. It uses mathrm{ln} and {mathrm e}.

The problem is the spacing between this and the letter before it, compared to when no mathrm is used. The space is lost in the case with mathrm{ln}

Here is an example

documentclass[12pt]{book}
usepackage{amsmath}
usepackage{mleftright}  
mleftright

begin{document}
[
4 {mathrm e}^{3 x}+3 mathrm{ln}left(x right)
]
                      
[
4 e^{3 x}+3 lnleft(x right)
]                                                                                                              
end{document}

Compiled with lualatex, gives

enter image description here

I prefer the second one. The space before mathrm e has not changed. But the space before mathrm{ln} was lost.

Why is that? Is it then not approrpiate to use mathrm for ln?

TL 2020

2 Answers

Your autogenerated code is bad, sorry. In

3mathrm{ln}left(xright)

there are three atoms, namely

  1. 3 of type Ord
  2. mathrm{ln} of type Ord
  3. left(xright) of type Inner

Between two atoms of type Ord, TeX inserts no space.

To the contrary,

    3lnleft(xright)

contains again three atoms, but with a big difference, namely

  1. 3 of type Ord
  2. ln of type Op
  3. left(xright) of type Inner

TeX inserts a thin space (of the amount of thinmuskip) between an Ord atom and an Op atom.

Sorry, but “garbage in, garbage out”. If the software is able to recognize the string ln to be set in upright type, it could well take the initiative of outputting ln instead of the wrong mathrm{ln}.

By the way, also left and right are out of place. And it's quite interesting that “e” is translated into {mathrm e} instead of the more correct mathrm{e} (if you really insist into printing the Euler constant upright).

A fix.

documentclass[12pt]{book}
%usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
usepackage{amsmath}
usepackage{mleftright}  

mleftright

ExplSyntaxOn
NewCommandCopy{latexmathrm}{mathrm}

RenewDocumentCommand{mathrm}{m}
 {
  str_case:nnF { #1 }
   {
    {e}{e}
    {i}{i}
    {ln}{ln}
    {log}{log}
    {sin}{sin}
    % ... add to the list
   }
   { latexmathrm{#1} }
 }
ExplSyntaxOff

begin{document}
[
4 {mathrm e}^{3 x}+3 mathrm{ln}left(x right)
]
                      
[
4 e^{3 x}+3 lnleft(x right)
]                                                                                                              
end{document}

enter image description here

For LuaLaTeX and unicode-math, the workaround has to be different.

documentclass[12pt]{book}
%usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
usepackage{amsmath}
usepackage{mleftright}  
usepackage[mathit=sym,bold-style=ISO]{unicode-math}
setmathfont{Asana Math}[Scale=MatchLowercase]
setmainfont{Baskervaldx}[
  UprightFont=*-Reg,
  ItalicFont=*-Ita,
  BoldFont=*-Bol,
  BoldItalicFont=*-BolIta,
]

mleftright

ExplSyntaxOn
AtBeginDocument
 {
  NewCommandCopy{latexmathrm}{mathtextrm}
  RenewDocumentCommand{mathtextrm}{m}
   {
    str_case:nnF { #1 }
     {
      {e}{e}
      {ln}{ln}
      {log}{log}
      {sin}{sin}
      % ... add to the list
     }
     { latexmathrm{#1} }
   }
 }% end of AtBeginDocument
ExplSyntaxOff

begin{document}
[
4 {mathrm e}^{3 x}+3 mathrm{ln}left(x right)
]
                      
[
4 e^{3 x}+3 lnleft(x right)
]                                                                                                              
end{document}

enter image description here

Note that Baskervaldx.sty is not the right choice when fontspec is used.

Correct answer by egreg on July 10, 2021

In this case, ln is what’s called a log-like operator.

You can use either ln, operatorname{ln} or mathop{mathrm{ln}} to get spacing like log. You can also add nolimits if you don’t like how subscripts and superscripts get placed above and below in display mode.

If amsmath did not already define it, you could write providecommandln{operatorname{ln}} and then have a ln command you could use in your source, and be able to tweak in one easy-to-find place.

Answered by Davislor on July 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP