[Tex/LaTex] How to make the correct hash-symbol in C Sharp (C#)

symbols

I want to make C# look nice in my book, but with the following:

C\# it looks like this:

enter image description here

When it should look more like this:

enter image description here

For the second one, I used verbatim, but I don't want that and I've also used a macro like the following everywhere so it should be easy to replace:

\def\Csharp{C\#}

Any suggestions on how I make this look correct?

Edit

I am writing a programming book, in the C# Language Specification, it looks like this:

enter image description here

Best Answer

Since Computer Modern Roman does not have a fitting hash symbol, we need to look somewhere else. I experimented with several popular fonts and found that the hash symbol from Liberation Serif does not stand out, has approximately the same brush width and isn't too wide.

Result

Unfortunately, as Liberation Serif is a TrueType font, it can't be readily used with pdfTeX. Below is the code to use it with XeTeX/LuaTeX.

\documentclass{article}

\usepackage{fontspec}

\newfontface\lserif{Liberation Serif}

\newcommand{\Csh}{C{\lserif\#}}

\begin{document}

Some text \Csh{} some text.

\end{document}

Workaround for pdfTeX

Process this file with XeTeX/LuaTeX and save the result as hash-symbol.pdf:

\documentclass[border=0pt]{standalone}
\usepackage{fontspec}
\begin{document}% I get extra space without this comment
\fontspec{Liberation Serif}\#
\end{document}

Then use this code to include the symbol with pdfTeX:

\documentclass{article}

\usepackage{graphicx}

\newcommand{\Csh}{C\includegraphics{hash-symbol}}

\begin{document}

Some text \Csh{} some text.

\end{document}

enter image description here

The positioning of the symbol might be slightly off, use \kern and \raisebox to fine-tune. Also note that this will only work for one font size; to remedy this, \scalebox might be useful (manual).