# Different fonts in same math environment

I would like to display text like 0^3 1^5 ... with the exponents (3 and 5 in the example, can be any number) written in usual math, but the bases (0 and 1) in typewriter font.

I tried various combinations of (inline) math, \verb and \texttt, but none looked right.

The result should look like this:

$\text{\texttt{0}}^4 \text{\texttt{1}}^3 \text{\texttt{0}} \text{\texttt{1}}^2$


But this is quite cumbersome to type.

• Do you want all text in the base to be in tt and all text in the scripts to be as normal? If so you could abbreviate the markup a bit more than the given answer (although that might not be wise:-) – David Carlisle Jan 12 '15 at 14:56
• David: Yes, all bases. – mafu Jan 12 '15 at 15:00
• So you want that in $x=1$ also the “x” appears in typewriter style? – egreg Jan 12 '15 at 15:29
• @egreg This does not appear in my use case, so either is fine with me :) – mafu Jan 12 '15 at 15:36

\documentclass{article}
\begin{document}
$\mathtt{0}^4 \mathtt{1}^3 \mathtt{0} \mathtt{1}^2$
\end{document}


• Thanks, that is already much better (and I feel stupid for not knowing mathtt). I suppose this is as far as we can go? – mafu Jan 12 '15 at 14:52
• @mafu As far as we can go! :) – user11232 Jan 12 '15 at 14:54
• @mafu there is always \newcommand \z{\mathtt} to reduce typing, and depending on the answer to the question in comments you may be able to get rid of \z as well:-) – David Carlisle Jan 12 '15 at 14:58
\documentclass{article}
\everymath{%
\mathtt{\xdef\tmp{\the\fam}}%
\textfont0=\textfont\tmp\relax}
\begin{document}

This isn't supported latex syntax, but

$0^4 1^3 0 1^2$

\end{document}

• Hmm, what about $x=1$? – egreg Jan 12 '15 at 15:26
• @egreg I asked in a comment in the question if all non script text to be in \mathtt and my understanding is that the answer is yes. See also the OP's self answer – David Carlisle Jan 12 '15 at 15:28
• I have no idea how, but this seems to work! What do you mean by 'not supported syntax'? – mafu Jan 12 '15 at 15:44
• @mafu I mean if you reported a bug in latex with a document that does \everymath{% \mathtt{\xdef\tmp{\the\fam}}% \textfont0=\textfont\tmp\relax} we'd probably tell you to go away unless you could reproduce the problem using commands found in the latex book, or the latex companion or anything else claiming to be about latex:-) – David Carlisle Jan 12 '15 at 15:48
• @mafu -- this uses tex primitives directly, not documented latex constructs. \textfont means the font that is used for "baseline" text. (the two sizes for sub/superscripts are \scriptfont and \scriptscriptfont'.) the 0 means that this is the basic roman font, which includes letters, digits and basic punctuation. you really don't want to fiddle with these unless you really know what you're doing, and even then, you should check relevant code in the guts of all packages you're using to make sure you don't botch anything. – barbara beeton Jan 12 '15 at 17:26

If you want to preserve the normal math setup for formulas, the only way is to mark up the places where you want these special numbers.

\documentclass{article}
\usepackage{amsmath,etoolbox}

\DeclareSymbolFont{ttnumbers}{OT1}{cmtt}{m}{n}
\DeclareSymbolFontAlphabet{\mathtt}{ttnumbers}

\makeatletter
\DeclareMathVersion{ttn}
\let\mv@ttn\mv@normal
\begingroup
\def\temp#1#2\@nil{\endgroup\def\mv@ttn{\mod@getanddefine@fonts#2}}%
\expandafter\temp\mv@ttn\@nil

\let\mod@getanddefine@fonts\getanddefine@fonts
\patchcmd\mod@getanddefine@fonts
{\string #2}
{OT1/cmtt/m/n}
{}{}
\makeatother

\newcommand{\ttn}[1]{\text{\mathversion{ttn}$#1$}}

\begin{document}
$a+b=\ttn{2^3\cdot3^5}-\log x$
\end{document}


Note that \ttn can also be used in normal text.

This compares what the OP had to my \wackymode.

\documentclass{article}
\usepackage{amsmath}
\let\svcarat^
\catcode^=\active
\def^#1{\ifmmode\svcarat#1\else\textsuperscript{\rmfamily#1}\fi}
\def\wackymode{\catcode^=\active\ttfamily}
\catcode^=7
\def\wackydone{\catcode^=7}
\begin{document}
$\text{\texttt{0}}^4 \text{\texttt{1}}^3 \text{\texttt{0}} \text{\texttt{1}}^2$

\wackymode
0^4%
1^3%
0%
1^2%
\wackydone
\end{document}


The \ifmmode test in my active carat definition allows for a syntax like

\wackymode
\ensuremath{x^4}%
1^3%
0%
1^2%
\wackydone


to yield

Just for reference: If space between the symbols is acceptable, it is possible to write

\texttt{0$^4$ 1$^3$ 0 1$^2$}
`

• (not sure if this is an intended use or a terrible hack) – mafu Jan 12 '15 at 15:04
• the latter :- ) – David Carlisle Jan 12 '15 at 15:23