# How to set temporary math font?

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}}$.

• Possible duplicate of What is the "correct" way of embedding text into math mode? – CroCo Aug 6 '17 at 2:33
• @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 6 '17 at 2: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 6 '17 at 2:38
• @Au101, I don't know where the OP said the best way. – CroCo Aug 6 '17 at 2:43
• 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 6 '17 at 2:56

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}

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
\loop\ifnum\count255<z
\mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
\repeat
\count255=0
\loop\ifnum\count255<9
\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}

• What about $a+b=\pi$? See also this quetion. – GuM Aug 8 '17 at 20:14
• @GuM “If all you need is to use the monospaced alphabet for variables”… – egreg Aug 8 '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 8 '17 at 20:28
• @GuM Does the new version please you more? – egreg Aug 8 '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 8 '17 at 20:57

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
\loop\ifnum\count255<z
\mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
\repeat
\count255=0
\loop\ifnum\count255<9
\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:


\end{document}

Here is the result:

• While it may work, I don't think that it is good markup to use \symtt in this way. – Ulrike Fischer Aug 6 '17 at 13:24
• @UlrikeFischer Though I have answered the question, I don't think that typewriter math can fit the look of the formula. Or there are examples of good usage? – Michael Fraiman Aug 6 '17 at 15:06
• Thanks, this is a good response but I was hoping to compile with pdflatex. – dow Aug 6 '17 at 21:29