3

When there are words consisting of several letters in an equation, I have to add \mathit to brace each word up or the inter-letter space will vary. See the difference between the two instances of "CDF" and "erf" in below MWS? But such a practice is really tedious and adds difficulty to fast reading an equation. Is there a package that automatically uniformizes the inter-letter space for words in an equation so I don't have to add hundreds \mathit in one paper?

\documentclass[journal]{IEEEtran}

\usepackage{amsmath} \begin{document} \begin{align}
\label{cdf} CDF &= \frac{1}{2}(1 + erf(\frac{x-\mu}{\sigma\sqrt{2}})) \end{align}

 \begin{align}      
 \label{cdf}
     \mathit{CDF} &= \frac{1}{2}(1 + \mathit{erf}(\frac{x-\mu}{\sigma\sqrt{2}}))
 \end{align}    

\end{document}

enter image description here

Mico
  • 506,678
  • Welcome to TeX.SE. – Mico Aug 19 '23 at 07:43
  • 2
    Off-topic: It's poor typographic practice to use an align environment for single-line equations. I suggest you use equation environments for numbered single-line equations. – Mico Aug 19 '23 at 07:46
  • 2
    A thought that's prompted by your expressed desire to avoid having "to add hundreds \mathit in one paper". One of the many deep beauties of TeX is that it's a macro programming language. Thus, rather than write \mathit{erf} hundreds of times, you could (a) replace all instances of erf with \erf and (b) define once -- presumably in the preamble -- what \erf is supposed to do. You could set \newcommand\erf{\mathit{erf}}, or you could pursue the approach I suggest in my answer, viz., run \DeclareMathOperator{\erf}{erf}, giving \erf the same status as \det, \sin, \log, etc. – Mico Aug 19 '23 at 15:09

1 Answers1

14

In fine math typesetting, it's very common to use an upright font shape for operators such as "log", "sin", "det", "abs", and so on. "erf" is no different in this regard. Since you already make use of the amsmath package, you can use its \DeclareMathOperator macro to declare a new operator called, say, \erf, as follows:

\DeclareMathOperator{\erf}{erf}

Then, perform a global, one-time-only search and replace from erf to \erf in the body of the TeX document.

The case for giving special typographic treatment to "CDF" is actually different to the one for "erf". Here, "CDF" is not an operator but an acronym. Since "CDF" definitely doesn't denote the product of variables named "C", "D", and "F", it's customary to use either \mathit or (I believe, more commonly) mathrm to affect the way the acronym is typeset. Better still, define a macro called, say, \vn (short for "variable name") as follows:

\newcommand\vn[1]{\mathrm{#1}}

and then write \vn{CDF} instead of just CDF. Using \vn is not only shorter than \mathrm, but also affords far greater typesetting flexibility. Suppose one journal requires math-roman notation for variable names, whereas an other journal requires math-italic notation. All you'll have to do in your paper is to change the argument of \vn.

Building on these thoughts, I'd rewrite your test document as follows:

enter image description here

\documentclass[journal]{IEEEtran}
\usepackage{amsmath} % for '\DeclareMathOperator' macro
\DeclareMathOperator{\erf}{erf} % "error function" operator
\newcommand\vn[1]{\mathrm{#1}}  % for typesetting variable names

% Optional -- load a Times Roman math font package \usepackage[lite]{mtpro2} % cf. https://pctex.com/mtpro2.html

\begin{document}

\begin{equation} \label{cdf} \vn{CDF} = \frac{1}{2}\biggl( 1+\erf\biggl( \frac{x-\mu}{\sqrt{2\sigma^2}} \biggr) \biggr) \end{equation}

\end{document}

Mico
  • 506,678
  • Thanks. So as of now there is no such a feature available in Latex as I requested - we have to put "\mathit" or "\mathrm" to modify every word in the mathematic environment (Using a defined var '\vn' might make things a bit better but doesn't really fix the issue). Personally I think this is a very desirable feature and wonder if the Latex community shall consider adding it. To recap what I'm asking for: – Sherman Chen Aug 20 '23 at 09:37
  • Create a package that automatically identifies the space before/after a word in the math environment.
  • The package will treat the identified word as a single variable (rather than each letter as a variable) by default.
  • – Sherman Chen Aug 20 '23 at 09:41
  • BTW, CDF stands for Cumulative Distribution Function therefore is preferred to be treated as a var – Sherman Chen Aug 20 '23 at 09:45
  • 1
    @ShermanChen - Is something stopping you from (a) doing a global search and replace of erf for \erf and (b) providing a suitable definition of \erf in the preamble? Incidentally, as an econometrician, I'm quite familiar with the acronym "CDF", and I'm also quite familiar with the notion that "erf" is not a variable name but an operator name. BTW, is it challening or burdensome to have to write \sin, \tan and \log instead of sin, tan, and log? – Mico Aug 20 '23 at 09:58
  • @Micro - Performing a global replacement is easy but the problem is it will also replace all "CDF", "erf", etc. in the text body which is not always wanted I believe. I'm still curious why Latex does not provide the feature I am proposing here. Is there some strong reason against doing so? – Sherman Chen Aug 20 '23 at 12:57
  • 3
    @ShermanChen I think Mico is recommending that in future you use exactly the same approach for "erf" as you currently do for "log" (having defined an appropriate macro). In my experience this is an extremely widely used and convenient approach. Yes, there is a little work involved if you want to convert previously existing documents. For that, if you want to avoid interactive search-and-replace, there are also editors that enable replacement only within mathmode; see for example https://tex.stackexchange.com/questions/65295/editor-that-replaces-only-text-inside-math-environments . – James Martin Aug 20 '23 at 13:08
  • @JamesMartin Yes, I appreciate Mico's advice and plan to go with it since it seems to be the best solution for now. But I still think the way I proposed might be able to save a bit more time and therefore might be worth consideration for the community. – Sherman Chen Aug 20 '23 at 19:26
  • @Mico Apologize for typing your name wrongly. I realized it later on but was already unable to edit my comment – Sherman Chen Aug 20 '23 at 19:28
  • @ShermanChen - No worries about the typo. – Mico Aug 21 '23 at 05:08