[Tex/LaTex] Put non-italic characters in equation

equationsitalicmath-mode

I am writing an equation, and I want some of the characters to not be italicized. All the other questions similar to this seem to be about globally changing italicizing in equations, but I just have 2 letters I don't want to be italic. The equation isn't in line, so I can't just end the equation and start a new one.

For example, say I'm writing $$f(x) = x^2$$, but I want the f, and only the f, to be non-italicized. How can I do this?

Best Answer

As mentioned in the comments, \mathrm{f} will insert a letter from the text font as a math symbol. Here’s an example set in ISO style in legacy NFSS mode. The constants i, e and π are all upright, but the variable x is italic. You’ll notice that the upright letters are the same as from the text font. It would be possible to select another font as \mathrm, for example with isomath.

\documentclass[varwidth, preview]{standalone}

\usepackage[T1]{fontenc}
\usepackage{textcomp, amssymb}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}

\newcommand\upe{\mathord{\mathrm{e}}}
\newcommand\upi{\mathord{\mathrm{i}}}
\newcommand\uppi{\mathord{\mathrm{\pi}}}

\begin{document}
\begin{align*}
  \upe^{\upi x} &= \cos{x} + \upi \sin{x} \\
  \upe^{\upi \uppi} + 1 &= 0
\end{align*}
\end{document}

Example with \mathrm

The unicode-math package distinguishes between text letters in math mode (\mathrm) and upright math symbols (\symup). However, you need to change the upright math alphabet to notice any difference in appearance. Here is an example that replaces the upright math symbols with CMU Serif Upright Italic, an unslanted version of the Computer Modern italic font. (Except for the digits, because $10$ and 10 should stay the same.) Notice that unicode-math is able to tell the difference between upright math symbols and operator names.

\documentclass[varwidth, preview]{standalone}

\usepackage{mathtools}
\usepackage[math-style=ISO]{unicode-math}

\setmathfont{Latin Modern Math}
\setmathfont[range=up/{Latin,latin,Greek,greek}]{CMU Serif Upright Italic}

\newcommand\upe{\symup{e}}
\newcommand\upi{\symup{i}}

\begin{document}
\begin{align*}
  \upe^{\upi x} &= \cos{x} + \upi \sin{x} \\
  \upe^{\upi \uppi} + 1 &= 0
\end{align*}
\end{document}

Example with \symup

I think the result matches pretty well, but Knuth’s Computer Modern italic font was never intended to be unslanted. (The tail of the e did not originally stick out like that.) So here’s another example, which combines Asana Math with upright math letters from Neo Euler. These are clones of two famous fonts by Hermann Zapf (Palatino and AMS Euler) that complement each other very nicely. The text font (also used for operator names) is another clone of Palatino.

\documentclass[varwidth, preview]{standalone}

\usepackage{mathtools}
\usepackage[math-style=ISO]{unicode-math}

\setmainfont{TeX Gyre Pagella}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\setmathfont[range={up/{Latin,latin,Greek,greek}, 
                    bfup/{Latin,latin,Greek,greek}},
             script-features={}, sscript-features={}
            ]{Neo Euler}

\newcommand\upe{\symup{e}}
\newcommand\upi{\symup{i}}

\begin{document}
\begin{align*}
  \upe^{\upi x} &= \cos{x} + \upi \sin{x} \\
  \upe^{\upi \uppi} + 1 &= 0
\end{align*}
\end{document}

Asana/Pagella/Euler sample