12

I'm trying to use a mono-font in my text, but it seems that mono texts differ in character map in math and text environment. Is there any solution?

\documentclass[fleqn]{article}
\usepackage{fontspec}
\setmonofont[Mapping=tex-text]{Courier New}
\begin{document}

\texttt{Farschad \% + - * \$  ! }

\[
\mathtt{Farschad \% + - * \$ !}
\]

\end{document}

enter image description here

3
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Aug 10, 2013 at 12:04
  • what is the purpose of your question? do you want to make the output look like a manuscript that was typed on a typewriter, or something else? Aug 10, 2013 at 12:06
  • I want to type computer codes with ttfamily fonts. However, some parts of the code should be clarified with mathematical formula and I want to use the same font in mathematical mode. so I use \mathtt instead of \texttt because I have to use mathematical operations. However, the fonts in two environment differ with each other.
    – Farschad
    Aug 10, 2013 at 13:58

3 Answers 3

8

\mathtt only changes the math alphabets. Just use \texttt in math mode.

\documentclass{article}

\begin{document}

$\texttt{foo-bar!}$

\end{document}
3
  • 1
    I'm afraid it is not a good solution because you can not use mathematical operations in text mode. for example the following text produces error: $\texttt{1^2}$
    – Farschad
    Aug 10, 2013 at 11:34
  • 2
    @Farschad: In fact, mathematical operators are not available in typewriter fonts.
    – Leo Liu
    Aug 10, 2013 at 11:36
  • the solution can be to use \texttt{} always but with the maths operators, right? so, no problems :)
    – Leos313
    Jul 7, 2020 at 7:32
5

this may or may not do all that you want. it requires amsmath to get the sub- and superscripts in the correct size, and you will have to handle all the spacing yourself.

there is probably a package for this, but i don't know it.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
Here is some computer code.

\texttt{%
a + b + c = 123$^{\texttt{2}}$ - x$_{\texttt{3}}$
}

That is all.
\end{document}

output from sample code

1
  • Thank you very much for the solution. The way you use works fine but it requires typing a lot of \texttt. I want to see if a shorter method is available. If we just make both commands (i.e mathtt and texttt) using the same character map, then it would be a good solution. Unfortunately, mathtt uses a few character map.
    – Farschad
    Aug 10, 2013 at 16:17
2

If you are using fontspec you should also be using either mathspec or unicode-math. If you use mathspec, then you can set the font for \mathtt like this:

\documentclass[fleqn]{article}
\usepackage{mathspec}

\setmonofont[Mapping=tex-text]{Courier New}
\setmathtt{Courier New}

\begin{document}

\texttt{Farschad \% + - * \$  ! }

\[
\mathtt{Farschad \% + - * \$ !}
\]

\end{document}

Similarly, if you use unicode-math, then:

\documentclass[fleqn]{article}
\usepackage{fontspec}
\usepackage[math-style=TeX]{unicode-math}

\setmonofont[Mapping=tex-text]{Courier New}
\setmathfont{latinmodern-math.otf}
\setmathfont[range=\mathtt->\mathup]{Courier New}

\begin{document}

\texttt{Farschad \% + - * \$  ! }

\[
\mathtt{Farschad \% + - * \$ !}
\]

\end{document}

Admittedly the results are not perfect – there is no way to change the font of non-alphabetic symbols – but it is a start.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .