# How to render a sharp sign that denotes the number of a set?

In math, # usually denotes the number of elements in a set (cardinality).

For example, in this equation

the #D{x} operator denotes the number of elements in the set D that satisfies property x.

I am trying to render this character in TeX, and this code

$\#D$


gives this symbol:

What am I missing?

• Related: How to write a nice # symbol Nov 12, 2019 at 5:41

It seems the font used in the image is Adobe Times/URW Nimbus as provided by the txfonts package (or some very similar variant of it):

\documentclass{article}
\usepackage{txfonts}

\begin{document}
$\hat\thetaup_{ijk} = \hat P(X_i=x_{ij}\mid Y=y_k) = \frac{\#D\{X_i=x_{ij} \wedge Y=y_k\}}{\#D\{Y=y_k\}}$
\end{document}


The font used in your image is not the default Computer Modern font. You could use \text{\ttfamily\#}, which is closer to the symbol in the image, but still not the same:

\documentclass{article}
\usepackage{amsmath}

\newcommand\tthash{\text{\ttfamily\#}}

\begin{document}
$\#D$ vs. $\tthash D$
\end{document}


Wikipedia also just lists the standard \# as a symbol for cardinality.

For the exact same output, you need to find the font that provides this special hash character glyph.

• txfonts has many weaknesses, avoid it and prefer newtxtext,newtxmath. Nov 12, 2019 at 8:38