# How to make the correct hash-symbol in C Sharp (C#)

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

C\# it looks like this:

When it should look more like this:

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:

-
Use \texttt{C\#} for your second picture. And you'll find this question useful: Prettiest way to typeset “C++”? –  Leo Liu Feb 14 '12 at 13:03
If it's about music, \newcommand{\textsharp}{$\sharp$} –  egreg Feb 14 '12 at 13:07
Don't use \texttt{C\#}. 'C' must be set in the normal font. –  Andrey Vihrov Feb 14 '12 at 13:24
My opinion is that you should not fiddle with the symbol placement, because it will disrupt the read flow. Instead you should find/create a good-looking hash symbol that will not stand out. While most popular fonts have such a symbol, Computer Modern Roman/Sans Serif, unfortunately, does not. –  Andrey Vihrov Feb 14 '12 at 14:07
@FilipEkberg Actually, it should be a sharp symbol as in a musical sharp note, it was intended to be this way, the only reason for a hash symbol can be summed up in ASCII. I believe some modern books use the originally intended musical sharp character. –  Ayman Elmasry Feb 14 '12 at 14:46

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.

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}


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).

-
It's a nice start, I need it to work with pdfText though, maybe someone else has some ideas on this too. Thanks! –  Filip Ekberg Feb 14 '12 at 14:52
@Filip You might consider switching anyway. While pdfTeX has served well, I would say that we might soon start to call it obsolete. Modern TeX implementations are simply superior in so many regards. –  Konrad Rudolph Feb 14 '12 at 15:30
@KonradRudolph, pdflatex was what came with texlive/texniccenter. You're saying I should upgrade to something else? –  Filip Ekberg Feb 14 '12 at 17:09
@Filip TeXnicCenter is an editor (and a pretty crappy one at that, speak Unicode support). TeXLive and other distributions all ship with modern TeX renderers (XeTeX, LuaTeX, ConTeXt). –  Konrad Rudolph Feb 14 '12 at 17:27
@KonradRudolph: I wouldn't lump in ConTeXt with XeTeX and LuaTeX. ConTeXt is - like LaTeX - a macro package that sits on top of TeX while XeTeX and LuaTeX are implementations of TeX itself –  kahen Feb 15 '12 at 3:41

Like egreg said earlier in his comment, use \newcommand{\textsharp}{$\sharp$}

The hash symbol was used out of necessity since ASCII did not contain a sharp symbol, but it was intended as a sharp note symbol as the language name denotes.

Addendum: You could always use $^\sharp$ for superscript.

-
Yup! That’s why it’s called “C Sharp” and not “C Pound Sign” or “C Octothorpe”. –  Todd Lehman Feb 16 '12 at 3:22
+1 seeing as this is the actual answer to the question. –  Smalltown2k Apr 3 '13 at 12:42
Both the C# language specification and the LaTeX paper on C# I'm citing (Variance and Generalized Constraints for C♯ Generics) use a superscript, and $^\sharp$ is the closest I can get. –  Blaisorblade Jun 6 '13 at 17:41

You might do with

\documentclass{article}
\usepackage{graphicx}
\newcommand{\Csharp}{%
{\settoheight{\dimen0}{C}C\kern-.05em \resizebox{!}{\dimen0}{\raisebox{\depth}{\#}}}}
\begin{document}
\Huge\Csharp
\end{document}


Here's the result; the second line has \fontseries{b}\selectfont\#, but the strokes seem to be too heavy:

If you prefer to use the music sharp symbol, it might be

\newcommand{\Csharp}{%
{\settoheight{\dimen0}{C}C\kern-.05em \resizebox{!}{\dimen0}{\raisebox{\depth}{$\sharp$}}}}


-

Here’s a version using \ooalign to combine an equal sign with two tightly kerned slashes. It works for all 10 standard sizes from \tiny up to \Huge.

The Good

• The line stroke widths are consistent and feel right (to me).

• As you can see from the code below, it’s rather a bit of a hack. I’m sure there are probably more elegant ways to do this. Perhaps someone can improve it and post a follow-up.
• You may need to tweak the positioning parameters if you’re using non-CMR or non-LMR fonts.
• The reason the implentation is so complicated is because the regular slash character is too tall for this, necessitating the use of a smaller sized slash in each case. Everything above \footnotesize uses a smaller slash; the smallest three make do with their respective native slashes.

The Ugly

• Because of calculation rounding issues, the display of this may not look quite right on screen; you may notice the // and the = appearing to be 1 pixel out of alignment in an on-screen PDF viewer at low dpi. It should be fine in print, however. This problem is common with many hand-composited symbols like this one.

Here’s the code with an (almost) MWE:

\documentclass{article}

\begin{document}

\makeatletter
\def\Csharp@tiny{5}
\def\Csharp@scriptsize{7}
\def\Csharp@footnotesize{8}
\def\Csharp@small{9}
\def\Csharp@normalsize{10}
\def\Csharp@large{12}
\def\Csharp@Large{14.4}
\def\Csharp@LARGE{17.28}
\def\Csharp@huge{20.74}
\def\Csharp@Huge{24.88}
\newcommand{\Csharp}{%
\mbox{%
C%
\ooalign{%
\noalign{%
\ifx\f@size\Csharp@tiny\vskip-1.11ex\fi%
\ifx\f@size\Csharp@scriptsize\vskip-1.11ex\fi%
\ifx\f@size\Csharp@footnotesize\vskip-1.07ex\fi%
\ifx\f@size\Csharp@small\vskip-1.07ex\fi%
\ifx\f@size\Csharp@normalsize\vskip-1.07ex\fi%
\ifx\f@size\Csharp@large\vskip-1.07ex\fi%
\ifx\f@size\Csharp@Large\vskip-1.07ex\fi%
\ifx\f@size\Csharp@LARGE\vskip-1.07ex\fi%
\ifx\f@size\Csharp@huge\vskip-1.07ex\fi%
\ifx\f@size\Csharp@Huge\vskip-1.07ex\fi%
}%
\hss{=}\hss\cr%
\noalign{%
\ifx\f@size\Csharp@tiny\vskip-0ex\fi%
\ifx\f@size\Csharp@scriptsize\vskip-0ex\fi%
\ifx\f@size\Csharp@footnotesize\vskip-0ex\fi%
\ifx\f@size\Csharp@small\vskip-.06ex\fi%
\ifx\f@size\Csharp@normalsize\vskip-.10ex\fi%
\ifx\f@size\Csharp@large\vskip-.10ex\fi%
\ifx\f@size\Csharp@Large\vskip-.10ex\fi%
\ifx\f@size\Csharp@LARGE\vskip-.10ex\fi%
\ifx\f@size\Csharp@huge\vskip-.10ex\fi%
\ifx\f@size\Csharp@Huge\vskip-.10ex\fi%
}%
\hss{%
\ifx\f@size\Csharp@tiny\tiny\fi%
\ifx\f@size\Csharp@scriptsize\scriptsize\fi%
\ifx\f@size\Csharp@footnotesize\footnotesize\fi%
\ifx\f@size\Csharp@small\footnotesize\fi%
\ifx\f@size\Csharp@normalsize\footnotesize\fi%
\ifx\f@size\Csharp@large\normalsize\fi%
\ifx\f@size\Csharp@Large\large\fi%
\ifx\f@size\Csharp@LARGE\Large\fi%
\ifx\f@size\Csharp@huge\LARGE\fi%
\ifx\f@size\Csharp@Huge\huge\fi%
{/}\kern-.26em{/}%
}\hss\cr%
}%
}%
}
\makeatother

\emergencystretch=2em
\narrower\narrower\narrower\narrower\narrower\narrower\narrower
\noindent\Csharp\ (pronounced see sharp'') is a multi-paradigm programming language
encompassing strong typing, imperative, declarative, functional, generic,
object-oriented (class-based), and component-oriented programming disciplines.
\vskip 1em
\noindent\tiny\Csharp\,
\scriptsize\Csharp\,
\footnotesize\Csharp\,
\small\Csharp\,
\normalsize\Csharp\par\vskip-.25em
\noindent\large\Csharp\,
\Large\Csharp\,
\LARGE\Csharp\par\vskip-.25em
\noindent\huge\Csharp\,
\Huge\Csharp\par

\end{document}

-
Defining those sizes seems pointless. The LaTeX kernel defines them as: \@vpt, \@viipt, \@viiipt, \@ixpt, \@xpt, \@xiipt, \@xivpt, \@xviipt, \@xxpt, \@xvpt and I don't think anything ever modifies them (because that would be crazy) –  kahen Feb 16 '12 at 10:12

Using TikZ it's possible to draw the desired symbol manually:

\def\Csharp{C\tikz[x=1em,y=\baselineskip]%
\draw (0.125,0.15) -- ++(0.15,0.5)%
(0.325,0.15) -- ++(0.15,0.5)%
(0.05,0.3) -- ++(0.45,0.0)%
(0.1,0.5) -- ++(0.45,0.0);}


This has some issues though. For example the symbol doesn't scale all that well with changing font size, but the difference between \Large and \normalsize isn't too bad. Additionally it's impossible to copy "C#" from the resulting PDF file into the clipboard which may or may not be an issue.

-
Copying is somewhat possible: overlay the drawing on a zero-sized white \#. –  Andrey Vihrov Feb 14 '12 at 16:35

For pfdtex I prefere:

\documentclass{article}
\usepackage{graphicx}
\newcommand{\Csharp}{%
{\settoheight{\dimen0}{C}C\kern-.05em \resizebox{!}{\dimen0}{\raisebox{\depth}{\textbf{\#}}}}}
\begin{document}
\Huge\Csharp
\end{document}


Similar to the above but with thick #, because the thin one does not fit to the C in my eyes.

-

If you use XeTeX and your font supports it, you can put the Unicode character directly into your source file, or use \symbol{"266F}. With fonts such as Linux Libertine, this looks much nicer than \sharp, which seems to use raw TeX rather than looking for proper sharp character:

-