4

I am wondering if there is a way to change the math font temporarily, similar to the way it can be done in text mode:

{\ttfamily Some text in typewriter font.}

Is there a way to do $\ttfamily a + b = c$ for example?

I know I could do $\mathtt{a + b = c}$, just like I could do \texttt{text}, but sometimes the other way results in cleaner code.

Please mind I am not looking for something like $\texttt{monospace} \textit{ in math.}$ I want to be able to write complicated formulas without having to mention the font in every word, as I would do in, for example, $\Sigma_{\mathtt{i} = 1}^{\mathtt{n}}$.

GuM
  • 21,558
dow
  • 648
  • 4
  • 9
  • @CroCo I don't see that this is a duplicate of the linked question, what makes you think so? Seems like the OP wants the best way of making the math font in their equation a monospace font, rather than to embed text into an equation? – Au101 Aug 06 '17 at 02:35
  • @CroCo as Au101 commented. I could of course do $\texttt{monospace} \textit{ in math}$ but that's not what I am looking for. – dow Aug 06 '17 at 02:38
  • @Au101, I don't know where the OP said the best way. – CroCo Aug 06 '17 at 02:43
  • @CroCo well i'm interpreting the OP's comments about cleaner code to mean that they know how to get a letter to be monospace in math, but they want a clean way, in any case, I personally think it is not a dupe? – Au101 Aug 06 '17 at 02:45
  • @dow, thanks for the update. Take a look at this Change math font only in some parts of a document? – CroCo Aug 06 '17 at 02:51
  • @Au101, according to the OP's update. You are right and the question is still a possible duplicate of Change math font only in some parts of a document? – CroCo Aug 06 '17 at 02:56
  • 1
    No, there is no equivalent of \ttfamily etc. for maths. The assumption is that maths should be mostly maths and not text. Hence, only the odd word should need to be in text mode. If you are changing fonts in order to signify something, you should declare suitable maths macros and use those. Otherwise, you should use consistent fonts throughout else your maths will just be confusing. – cfr Aug 06 '17 at 02:56
  • @CroCo I'm not sure that's true. It isn't clear the OP wants to change the mathematical maths fonts. They're actually asking about text. We are not talking fonts providing maths symbols here. Usually, these fonts are designed to match the text fonts e.g. cmr, cmss etc. – cfr Aug 06 '17 at 02:58
  • @cfr, well that was my first guess but the OP said that is not the case. Please see the first comment. I may misunderstand the question or the question is not clear. – CroCo Aug 06 '17 at 03:03
  • @CroCo I think it really isn't clear, but the examples are all text ... Have to wait for the OP to clarify, really, though. – cfr Aug 06 '17 at 03:14
  • What I was looking for is a way to write, for example, $a \xrightarrow{l} b$ where $a$, $l$ and $b$ would be typeset in a specific font. I don't care (or want, for that matter) that the arrow itself is in a different font. – dow Aug 14 '17 at 21:40

4 Answers4

4

If all you need is to use the monospaced alphabet for variables:

\documentclass{article}
\usepackage{xpatch}

\DeclareMathVersion{ttmath}
\SetSymbolFont{letters}{ttmath}{OT1}{\ttdefault}{m}{n}

\xapptocmd{\ttfamily}{\mathversion{ttmath}}{}{}

\begin{document}

This is normal math: $a+b=c$

{\ttfamily This is tt math: $a+b=c$}

\texttt{This is tt math: $a+b=c$}

\end{document}

enter image description here

For a more complete setup, you can complement the monospaced text font with the concrete fonts (however, they are only available in bitmap format).

\documentclass{article}
\usepackage{xpatch}

\DeclareMathVersion{ttmath}
\DeclareSymbolFont{latinletters}{OT1}{\ttdefault}{m}{n}
%\SetSymbolFont{latinletters}{ttmath}{OT1}{\ttdefault}{m}{n}
\SetSymbolFont{letters}{ttmath}{OML}{ccm}{m}{it}
\SetSymbolFont{symbols}{ttmath}{OMS}{ccsy}{m}{n}
\SetSymbolFont{largesymbols}{ttmath}{OMX}{ccex}{m}{n}

\newcommand{\changeletters}{%
  \count255=`a
  \advance\count255 -1
  \loop\ifnum\count255<`z
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
  \count255=`0
  \advance\count255 -1
  \loop\ifnum\count255<`9
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
}

\xapptocmd{\ttfamily}{\mathversion{ttmath}\changeletters}{}{}

\begin{document}

This is normal math: $a+b=\alpha$

{\ttfamily This is tt math: $a+b=\alpha$}

\texttt{This is tt math: $a+b=\alpha$}

\bigskip

$\displaystyle\frac{1}{2\pi i}\oint \frac{1}{z}\,dz$
{\ttfamily$\displaystyle\frac{1}{2\pi i}\oint \frac{1}{z}\,dz$}

\end{document}

enter image description here

egreg
  • 1,121,712
  • What about $a+b=\pi$? See also this quetion. – GuM Aug 08 '17 at 20:14
  • @GuM “If all you need is to use the monospaced alphabet for variables”… – egreg Aug 08 '17 at 20:18
  • Mmmh… I don’t know: as you can see, another user has already found this misleading, in less than 48 hours. I really think you should at least add some words of advice. (I too had briefly considered this solution, and I had refrained from posting it exactly to avoid confusing inexperienced users…) – GuM Aug 08 '17 at 20:28
  • @GuM Does the new version please you more? – egreg Aug 08 '17 at 20:50
  • To be really fussy, letters and digits should be of class 7… Why to disable the ability to use a custom math font? – GuM Aug 08 '17 at 20:57
  • @GuM I see no reason for adding such a support (there is no boldface cmtt, by the way, which could be the only meaningful variation). Please, go forth and add your own solution, if you think so. – egreg Aug 08 '17 at 21:11
  • I’ve posted my (variation of your) answer, as you suggested. – GuM Aug 10 '17 at 16:39
1

You can use the old font command \tt.

CAVEAT: It is ill-advised to use these old font commands (\rm, \tt, etc.), especially because some documentclasses explicitly ban their use (KOMA, memoir).

\documentclass{article}

\begin{document}

$\tt a + b = c$

\end{document}
Henri Menke
  • 109,596
1

I regret to say that @egreg seems to have taken my criticism, that was meant to be constructive, amiss (I must admit, on the other hand, that I like to tease him by pointing out minor flaws in his answers, on those rare cases on which this happens to be possible). I’m referring in particular to a comment in which I remarked that he had forgotten to assign the new \mathcodes for latin letters to class 7 (\mathalpha, in NFSS parlance). The reason why I think this is a flaw lies in the fact that it inhibits the possibility to use an explicitly specified math alphabet with those letters, as illustrated by the following sample code:

\documentclass{article}
\usepackage{xpatch}

% \DeclareMathVersion{ttmath} \DeclareSymbolFont{latinletters}{OT1}{\ttdefault}{m}{n} % % \SetSymbolFont{latinletters}{ttmath}{OT1}{\ttdefault}{m}{n} % \SetSymbolFont{letters}{ttmath}{OML}{ccm}{m}{it} % \SetSymbolFont{symbols}{ttmath}{OMS}{ccsy}{m}{n} % \SetSymbolFont{largesymbols}{ttmath}{OMX}{ccex}{m}{n}

\newcommand{\changeletters}{% \count255=a \advance\count255 -1 \loop\ifnum\count255&lt;z \advance\count255 1 \mathcode\count255=\numexpr\number\symlatinletters256+\count255\relax \repeat \count255=0 \advance\count255 -1 \loop\ifnum\count255&lt;9 \advance\count255 1 \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax \repeat }

\xapptocmd{\ttfamily}{% % \mathversion{ttmath}% \changeletters }{}{}

\begin{document}

This is normal math: $a+b+C=\alpha+\mathit{b}+\mathcal{C}$

{\ttfamily This is tt math: $a+b+C=\alpha+\mathit{b}+\mathcal{C}$}

\texttt{This is tt math: $a+b+C=\alpha+\mathit{b}+\mathcal{C}$}

\bigskip

$\displaystyle\frac{1}{2\pi i}\oint \frac{1}{z},dz$ {\ttfamily$\displaystyle\frac{1}{2\pi i}\oint \frac{1}{z},dz$}

\end{document}

This code reproduces @egreg’s second solution, with two differences:

  • I have disabled the concrete fonts, in order to avoid the use of these bitmapped fonts (indeed, they are not relevant to the point being made, while, on the other hand, they litter $TEXMFVAR/fonts/);

  • I (try to) make use of the math alphabets \mathit and \mathcal.

Well, as you can check, the output is

Output of @egreg’s solution

You see that, on the right-hand side of the two equations typeset in teletype, the letter “b” (lowercase) fails to comply with the request for the \mathit font (it stays in teletype), while the letter “C”—which, being uppercase, has not been subjected to @egreg’s \mathcode modifications—does change to the \mathcal font. Note that one could hardly claim that preserving the ability to use different math alphabets is irrelevant, because it provides not only a means to escape the “teletype variables” convention and occasionally get, for example, a variable typeset in math italics, but also to write such things as z\in\mathbb{C}.

This defect (which is actually nothing more than a slight oversight) could be corrected with a trivial modification to @egreg’s code. However, he has declined my suggestion to apply it, while encouraging me to post my own answer. Since almost two days have elapsed without him changing his mind, I’m now accepting this encouragement; I’d like to stress, however, that what follows is simply a minor amendment to @egreg’s code.

Besides correcting the “\mathalpha class” issue, the following code differs from @egreg’s in a few other points:

  • it does not use the concrete fonts, for the reasons explained above; if you want them, uncomment the lines that contain a double percent sign (% %);

  • it applies the \mathcode change to uppercase letters too;

  • it “decouples” the selection of the math font for letters from that of the main text font, by providing a \teletypemath declaration that you must invoke explicitly;

  • it precomputes the “class+family” prefix, instead of recalculating it on each iteration;

  • it uses a subsidiary macro to apply the patch to several ranges of characters.

The code:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc} % Not always necessary, but recommended. % End of standard header. What follows pertains to the problem at hand.

\usepackage{amsmath,amsfonts} % compatibility test

\DeclareSymbolFont{latinletters}{\encodingdefault}{\ttdefault}{m}{n} % % \DeclareMathVersion{teletype} % % \SetSymbolFont{letters}{teletype}{OML}{ccm}{m}{it} % % \SetSymbolFont{symbols}{teletype}{OMS}{ccsy}{m}{n} % % \SetSymbolFont{largesymbols}{teletype}{OMX}{ccex}{m}{n}

\makeatletter

@ifdefinable@teletype@class@family{\mathchardef@teletype@class@family = \numexpr 28672 +\symlatinletters@cclvi \relax} % 28672 = 74096 \newcommand@changerange[2]{% \count@ #1% \advance \count@ \m@ne \loop \ifnum \count@&lt;#2% \advance \count@ @ne \mathcode\count@ \numexpr @teletype@class@family+\count@ \relax \repeat } \newcommand{\changeletters}{% @changerange az% @changerange AZ% @changerange 09% } % The following declaration avoids wasting an entry in TeX's very limited table % of math fonts when the "\mathtt" is explicitly used: \DeclareSymbolFontAlphabet{\mathtt}{latinletters}

\makeatother

\newcommand*{\teletypemath}{% % % \mathversion{teletype}% \changeletters }

\begin{document}

This is normal math: $a+b+C+1=\alpha+\mathit{b}+\mathcal{C}-2$; and $z\in\mathbb{C}$.

{\teletypemath This is tt math: $a+b+C+1=\alpha+\mathit{b}+\mathcal{C}-2$; and $z\in\mathbb{C}$.}

\bigskip

$\displaystyle\frac{1}{2\pi i}\oint \frac{1}{z},dz$ {\teletypemath$\displaystyle\frac{1}{2\pi i}\oint \frac{1}{z},dz$}

\bigskip

Explicit use of \verb|\mathtt|: $x\neq\mathtt{x}$.

\end{document}

The output:

Output of my solution

As you can see, now the requests for different math alphabets are correctly honored.


How it works (or why it doesn’t)

Since this affair of redefining math families has already proved a possible source of confusion, I deem it important to add a few words of explanation and advice.

To begin with, let’s analyze @egreg’s original solution:

\documentclass{article}
\usepackage{xpatch}

\DeclareMathVersion{ttmath} \SetSymbolFont{letters}{ttmath}{OT1}{\ttdefault}{m}{n}

\xapptocmd{\ttfamily}{\mathversion{ttmath}}{}{}

\begin{document}

This is normal math: $a+b=c$

{\ttfamily This is tt math: $a+b=c$}

\texttt{This is tt math: $a+b=c$}

\end{document}

First, the line

\DeclareMathVersion{ttmath}

declares a new math version, analogous to the bold math version, called ttmath; then the line

\SetSymbolFont{letters}{ttmath}{OT1}{\ttdefault}{m}{n}

tells LaTeX that the letters font, which is where letters and digits are drawn from, is to be set, when typesetting math in the ttmath math version, to the combination identified by encoding OT1, family corresponding to the default family for monospaced font (\ttdefault; but note that, actually, there is no guarantee that this family is encoded via the OT1 encoding), medium weight (m), and normal (that is, upright) shape (n).

Why does this setting cause problems? Well, unfortunately, there are other symbols, besides letters and digits, that are drawn from the letters font; for example, the LaTeX kernel contains the declaration

\DeclareMathSymbol{\alpha}{\mathord}{letters}{"0B}

which defines the \alpha symbol as an ordinary symbol that doesn’t change when used within a math alphabet command like \mathcal (indeed, the second argument is \mathord; if the symbol had had to follow math alphabet changes, \mathalpha would have been used here instead) and that is found in slot "0B of the letters font. Now, this relies on the fact that the letters font is encoded through the so-called OML encoding (in which slot "0B actually correspond to the lowercase greek letter “α”), as it is the case in the normal settings, that assign the letters font to the cmmi family; but if we change it to an OT1-encoded font like cmtt,(*) the \alpha command will yield whatever symbol happens to be found in the "0B slot of the new encoding.

My solution (which, let me repeat once more, apart from the “class 7/\mathalpha” issue, in practice coincides with @egreg’s second one) solves this problem by introducing a brand new family, called latinletters, which is linked to the default monospaced font (in the default encoding), and then performing the equivalent of the following series of declarations,

\DeclareMathSymbol{a}{\mathalpha}{latinletters}{`a}
...
\DeclareMathSymbol{z}{\mathalpha}{latinletters}{`z}
\DeclareMathSymbol{A}{\mathalpha}{latinletters}{`A}
...
\DeclareMathSymbol{Z}{\mathalpha}{latinletters}{`Z}
\DeclareMathSymbol{0}{\mathalpha}{latinletters}{`0}
...
\DeclareMathSymbol{9}{\mathalpha}{latinletters}{`9}

that instruct LaTeX to fetch the letters (both uppercase and lowercase) and the digits from our new font, without affecting other symbols drawn from the letters font. In our code, the rôle of the above declarations is played by the \changeletters commands, which exploit the knowledge hof how math font selection is implemented at low level to achieve the same result by iterating through a loop.


(*) Actually, the cmtt family follows that particular variant of the OT1 encoding that Knuth decided to use for monospaced fonts. Strictly speaking, there does not exist a unique “OT1 encoding”, but only variants thereof.

GuM
  • 21,558
0

You can use unicode-math package, which requires the document to be compiled with Xe or LuaLaTeX.

Here is the code:

\documentclass{article}

\usepackage{unicode-math}%Load the package

\setmathfont{TeX Gyre Schola Math}%Set the font you want to use

\begin{document}

    $a+b=c$%regular

    $\symtt{a+b=c}$%some symbols support typewriter series

\end{document}

Here is the result:

enter image description here