# How to change math font to monospace, only inside \texttt

Related with "Can I change all math output to use monospaced text?", is it possible to use the monospace math only inside \texttt (or other custom command)?

I tried to include the \everymath inside a group, but it seems it's not working until after I use some math:

\documentclass{minimal}
\usepackage{amsmath}
\newcommand{\ttmath}{\everymath{\mathtt{\xdef\tmp{\fam\the\fam\relax}\aftergroup\tmp}}}
\newcommand{\val}[1]{\bgroup\ttmath\texttt{#1}\egroup}
\begin{document}

\val{1\ensuremath{\ensuremath{\cdot}10\ensuremath{^{-6}}}}

\ensuremath{a_0}, \ensuremath{m_{\text{e}}}).

\val{1\ensuremath{\ensuremath{\cdot}10\ensuremath{^{-6}}}}

\end{document}


Note that the first 10^-6 does not use monospace font, but the second does. a_0 and m_e use normal math font, which is fine.

I can get it fixed by adding \ensuremath{} between \ttmath and \texttt, but is there a cleaner way?

PS. Please forgive the ugly code full of \ensuremath, it's being generated from XML.

PPS. Without XeTeX or LuaLaTeX, please.

-

You just need to encourage latex to initialise the font setup in scriptstyle as well as the main textstyle. As you notice, it doesn't do this until it thinks it is needed:

\documentclass{minimal}
\usepackage{amsmath}
\newcommand{\ttmath}{%
\everymath{%
{\scriptstyle\mathtt{}}%
{\scriptscriptstyle\mathtt{}}%
\mathtt{\xdef\tmp{\fam\the\fam\relax}\aftergroup\tmp}}}
\newcommand{\val}[1]{\bgroup\ttmath\texttt{#1}\egroup}
\begin{document}

\val{1\ensuremath{\ensuremath{\cdot}10\ensuremath{^{-6}}}}

\ensuremath{a_0}, \ensuremath{m_{\text{e}}}).

\val{1\ensuremath{\ensuremath{\cdot}10\ensuremath{^{-6}}}}

\end{document}

-
Thanks, that does it! – Jellby Jul 22 '12 at 10:14