Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

enter image description here

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.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

enter image description here

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}
share|improve this answer
    
Thanks, that does it! –  Jellby Jul 22 '12 at 10:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.