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<z
\advance\count255 1
\mathcode\count255=\numexpr\number\symlatinletters256+\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+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

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@<#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:

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.
\ttfamilyetc. 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:56cmr,cmssetc. – cfr Aug 06 '17 at 02:58