Rules for space when using \mathrm{}

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 \providecommand\ln{\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.


Your autogenerated code is bad, sorry. In

3\mathrm{ln}\left(x\right)

there are three atoms, namely

  1. 3 of type Ord
  2. \mathrm{ln} of type Ord
  3. \left(x\right) of type Inner

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

To the contrary,

    3\ln\left(x\right)

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

  1. 3 of type Ord
  2. \ln of type Op
  3. \left(x\right) 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 \ln\left(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 \ln\left(x \right)
\]                                                                                                              
\end{document}

enter image description here

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