LaTeX mathtt and Math Fonts: mathsf, mathcal, Bold Math

November 11, 2026 · 10 min read

LaTeX mathtt and Other Math Fonts: Sans Serif, Calligraphic and Bold

The LaTeX mathtt command, \mathtt{...}, sets letters and digits inside math mode in a typewriter (monospace) font. It's built into LaTeX, so no package is needed. This reference also covers the alphabets around it: \mathsf for sans serif, \mathcal and \mathscr for script, \mathfrak for Fraktur, and the right way to get bold math.

LaTeX mathtt: Typewriter Letters in Math Mode

\mathtt is one of seven math alphabets that the LaTeX font selection guide predefines. Inside its argument, Latin letters, digits and capital Greek switch to the typewriter face, which is Computer Modern Typewriter (cmtt) in a default document. Operators, relations and lowercase Greek stay as they are.

You'll reach for \mathtt when a formula mentions something from code: a variable name, a file name, or a function you'd otherwise have to explain.

$\mathtt{x\_max} = \max_i x_i$ and $\mathtt{grad}(f) = \nabla f$

That prints x_max and grad in monospace, followed by ordinary italic math. Two details catch people out. Spaces inside the argument vanish, because TeX ignores spaces in math mode, so $\mathtt{a b}$ prints ab. And the underscore needs a backslash, or TeX reads it as a subscript.

Every LaTeX Math Alphabet at a Glance

Each command below takes one argument and only works in math mode. The character column is what pdfLaTeX gives you with the default Computer Modern fonts. We rendered every sample with KaTeX 0.16 and compiled the kernel and AMS alphabets with pdfLaTeX on TeX Live 2025.

CommandSamplePackageCharacters it changesKaTeX
\mathnormalAbxAbxnonethe default math italicYes
\mathrmAb1\mathrm{Ab1}noneletters, digits, capital GreekYes
\mathitAb1\mathit{Ab1}noneletters, digits, capital GreekYes
\mathbfAb1\mathbf{Ab1}noneletters, digits, capital GreekYes
\mathsfAb1\mathsf{Ab1}noneletters, digits, capital GreekYes
\mathttAb1\mathtt{Ab1}noneletters, digits, capital GreekYes
\mathcalABC\mathcal{ABC}noneuppercase onlyYes
\mathscrABC\mathscr{ABC}mathrsfsuppercase onlyYes
\mathfrakAg1\mathfrak{Ag1}amssymb or amsfontsupper, lower and digitsYes
\mathbbRZ\mathbb{RZ}amssymb or amsfontsuppercase (number sets)Yes

The first seven come from the LaTeX kernel itself. \mathrm is the one you'll use most after \mathbf, for upright subscripts and the differential in dx\mathrm{d}x. Symbols a command doesn't touch, such as lowercase Greek in \mathsf, keep their normal font without an error. Lowercase letters in \mathcal are a different story, covered below. The \mathbb row has its own guide to blackboard bold in LaTeX.

What Is the Difference Between LaTeX mathtt and texttt?

\texttt is a text command. It keeps spaces, works in a normal paragraph, and also works inside a formula. \mathtt is a math alphabet: math mode only, spaces dropped, and only letters, digits and capital Greek change.

In a default document both print cmtt, so they look the same. They split apart when only the text typewriter font changes. Packages such as lmodern update both, but a plain \ttdefault change doesn't. The LaTeX kernel declares \mathtt with cmtt fixed, while \texttt follows \ttdefault. We tested this: after \renewcommand{\ttdefault}{lmtt}, \texttt printed Latin Modern Mono and \mathtt still printed cmtt. One preamble line brings them back in step, and a small macro saves typing:

\DeclareMathAlphabet{\mathtt}{\encodingdefault}{\ttdefault}{m}{n}
\newcommand{\code}[1]{\mathtt{#1}}

After that, $\code{x\_max} + 1$ matches your text code font. Use a macro like this instead of switching all math to typewriter. It also gives you one place to restyle every code name later. For inline code in running text, \texttt stays the right tool. If these formulas are headed for Markdown, our Markdown equation guide explains the dollar-sign delimiters.

Sans Serif in LaTeX: mathsf, textsf and Whole Documents

Sans serif in LaTeX works at three levels. In a formula, \mathsf{A} gives an upright sans letter. In text, \textsf{...} or the \sffamily declaration does the same job. For a sans serif LaTeX document, change the default family in the preamble:

\documentclass{article}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
Sans-serif text, but $f(x) = x + 1$ is still serif.
\end{document}

That switches every paragraph to sans serif and leaves the math alone. We compiled it and checked the fonts: the text used cmss, while x and + still came from the serif math fonts. To make the math match, load the sfmath package after any change to \sfdefault, under pdfLaTeX. The package source says all math then uses sans-serif fonts, with no way to switch back. Under XeLaTeX or LuaLaTeX, use unicode-math with a sans-serif OpenType math font instead.

We prefer \mathsf only for objects that need their own type style, such as a named matrix or a category, and leave ordinary variables in italic. A whole formula in \mathsf is harder to scan, because the upright letters lose the italic cue that marks a variable.

Calligraphic, Script and Fraktur: mathcal, mathscr, mathfrak

\mathcal ships with LaTeX and covers capital letters only. It's common for a loss or Lagrangian, L\mathcal{L}, for big-O notation, O(n)\mathcal{O}(n), and for the normal distribution, N(0,1)\mathcal{N}(0, 1).

\mathscr gives a more ornate formal script. In a .tex file it needs \usepackage{mathrsfs}, and the RSFS fonts behind it contain capitals only. Authors often use it when they need a second script alphabet next to \mathcal, for example for sheaves or sigma-algebras. If you'd rather have Euler script, \usepackage[mathscr]{eucal} maps \mathscr to that instead.

\mathfrak comes from amsfonts, which amssymb loads, and it has lowercase letters and digits too. You'll see g\mathfrak{g} for a Lie algebra and p\mathfrak{p} for a prime ideal.

Does \mathcal work for lowercase? No. In pdfLaTeX, $\mathcal{abc}$ printed ⊣⌊⌋ in our test, and $\mathcal{123}$ printed ∞∈∋, because the calligraphic letters live in the symbol font. KaTeX quietly falls back to ordinary italic letters and upright digits instead, so a Markdown preview can hide a mistake that breaks your PDF.

How Do You Get Bold in LaTeX Math Mode?

Bold in LaTeX math mode depends on what you're making bold. \mathbf gives upright bold Latin letters, digits and capital Greek, the usual style for vectors and matrices. It doesn't touch lowercase Greek: $\mathbf{\alpha}$ prints a plain α in pdfLaTeX and in KaTeX.

CommandPackageWhat you get
\mathbf{x}noneupright bold letters and digits
\boldsymbol{x}amsmathbold italic letters, bold Greek, bold symbols
\bm{x}bmthe same as \boldsymbol, with more care for spacing
\boldmathnoneswitches a whole formula to bold, used outside math
\pmb{x}amsmathfake bold by overprinting, a last resort

Here's the usual setup:

\usepackage{amsmath}
\usepackage{bm}

$\mathbf{A}\mathbf{x} = \mathbf{b}$ and $\bm{\alpha} + \boldsymbol{\beta}$
{\boldmath $E = mc^2$}

The bm package documentation says \bm preserves the spacing of the symbol it bolds, and it defines \boldsymbol as another name for \bm. Load bm after your font packages, because it checks which bold fonts exist when it's read. \boldmath selects the bold math version, so it has to go before the $, never inside the formula.

For bold Greek, \boldsymbol{\alpha} and \bm{\alpha} are the two commands to remember. \textbf also works inside math, but it gives text-font bold, and a Greek command such as \alpha inside it is an error.

Math alphabets don't stack. In pdfLaTeX, \mathbf{\mathsf{A}} prints a regular-weight sans A, because the inner command wins. \bm{\mathsf{A}} does give bold sans serif there, since bm switches to the bold math version. KaTeX printed a regular sans A for both in our test, so check bold sans serif in a real compile before you rely on it.

Math Fonts in KaTeX and Markdown

Markdown math on the web is usually rendered by a JavaScript library such as KaTeX, not by a TeX engine. There's no preamble and no \usepackage, so a command either exists in the library or fails. The KaTeX support table lists the font commands, and our own tests agree with it.

  • Render in KaTeX: \mathtt, \mathsf, \mathrm, \mathit, \mathnormal, \mathbf, \mathcal, \mathscr, \mathfrak, \mathbb, \boldsymbol, \bm, \pmb, \textsf, \texttt and \textbf.
  • Fail with an undefined control sequence: \boldmath, \mathds, \mathbbm, \mathbfit, \symbf, \symsf, \sffamily and \ttfamily.

KaTeX also accepts \bold and \Bbb as shortcuts, plus the old two-letter switches such as {\bf x} and {\tt x}. Those switches date from LaTeX 2.09 and stay in the standard classes for compatibility, so write the \math... forms in anything you'll also compile. One more gap: \mathds needs the dsfont package in real LaTeX and has no KaTeX version, so use \mathbb on the web.

Our editor renders math with KaTeX, inline $...$ and display $$...$$. The \(...\) and \[...\] delimiters aren't supported. Edit any line below and watch the preview update:

Typewriter: x=42\mathtt{x = 42} and f(n)\mathtt{f(n)}
Sans serif: Ax=b\mathsf{A}\mathsf{x} = \mathsf{b}
Roman: dx\mathrm{d}x and eiπ\mathrm{e}^{i\pi}
Bold vector: v=(1,2,3)\mathbf{v} = (1, 2, 3)
Bold Greek: α+β\boldsymbol{\alpha} + \bm{\beta}
Calligraphic: L\mathcal{L} and O(n)\mathcal{O}(n)
Script: L\mathscr{L}
Fraktur: g\mathfrak{g} and su(2)\mathfrak{su}(2)

L(θ)=∥y−Xθ∥2\mathcal{L}(\boldsymbol{\theta}) = \lVert \mathbf{y} - \mathsf{X}\boldsymbol{\theta} \rVert^2
46 words478 characters12 lines
Markdown

If a document needs packages, custom fonts or a bibliography, it has outgrown Markdown math. Our Markdown vs LaTeX comparison helps you decide when to switch to a full .tex file.

Changing the Math Font for a Whole Document

Every command above picks an alphabet inside the current math font. To change the math font itself, compile with XeLaTeX or LuaLaTeX and load the unicode-math package. Its \setmathfont command selects an OpenType math font, and it adds \symbf, \symit, \symsf, \symtt and \symbfit for single-letter styles. Our Times New Roman in LaTeX guide uses the same package to match math to Times text.

\usepackage{unicode-math}
\setmathfont{latinmodern-math.otf}

$\symbf{v} \cdot \symbfit{\alpha}$

The limitation is that fonts don't travel. Not every alphabet exists in every math font. KaTeX also ships one fixed set of fonts, so a math font you pick in a LaTeX preamble won't show up in Markdown math. If you only wanted bigger or smaller text, that's a size question, covered in our guide to font size in Markdown. In a .tex file, the LaTeX font size commands do that job.

Quick Answers on LaTeX Font Commands

The LaTeX mathtt command is the quick fix for code names in formulas, and the other alphabets follow the same one-argument pattern: \mathsf for sans serif, \mathcal and \mathscr for script capitals, and \mathfrak for Fraktur. For bold, use \mathbf on Latin letters and \bm for Greek and symbols. Test a formula in the LaTeX equation editor, then paste it into the editor to see it in your notes.