3

How can we use teletypewriter fonts in math mode? In the following code, $X_i$ still comes italic and non-typewriter font. $\texttt{fnfoo(X_i)}$ gives errors "Missing $ inserted".

\documentclass{article}    
\begin{document}
\texttt{fnfoo()}

\texttt{fnfoo($X_i$)}   
\end{document}

enter image description here

Mico
  • 506,678
pba
  • 527
  • 5
  • 10

2 Answers2

5

As its name suggests, \texttt operates on text-mode material; by design, it does not operate on math-mode material that may occur in its argument.

To typeset math-mode material using the (math) monospaced font, you need to encase it in a \mathtt instruction.

enter image description here

\documentclass{article}   
\begin{document}
\texttt{fnfoo()}

\texttt{fnfoo($X_i$)}           % not good

\texttt{fnfoo($\mathtt{X_i}$)}  % better
\end{document}
Mico
  • 506,678
1

You can use \mathop to declare the function and \mathtt to select the tt font.

\documentclass{article}

\newcommand{\fnfoo}{\mathop{\mathtt{fnfoo}}\nolimits}

\begin{document}

$\fnfoo()$

$\fnfoo(X_i)$

\end{document}
user94293
  • 4,254