TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


Any suggestions on how I make this look correct?


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

enter image description here

share|improve this question
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
up vote 18 down vote accepted

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.



\newfontface\lserif{Liberation Serif}



Some text \Csh{} some text.


Workaround for pdfTeX

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

\begin{document}% I get extra space without this comment
\fontspec{Liberation Serif}\#

Then use this code to include the symbol with pdfTeX:





Some text \Csh{} some text.


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

share|improve this answer
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.

enter image description here

share|improve this answer
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

  {\settoheight{\dimen0}{C}C\kern-.05em \resizebox{!}{\dimen0}{\raisebox{\depth}{\#}}}}

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

enter image description here

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

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

enter image description here

share|improve this answer

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

The Bad

  • 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:




\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

share|improve this answer
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:

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

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

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:

Screenshot of different methods of rendering C# using XeTeX

share|improve this answer
You can get a better-matched sharp symbol in Libertine if you add \usepackage[libertine]{newtxmath} and then use $\sharp$. – Andrew Cashner Jun 23 at 17:11

Building on the accepted answer and looking at the C# language specification (a .docx...) I came up with this:


Which looks like this:


Note that when using LuaLaTex you'll need to add \RequirePackage{luatex85} to the top of the file that you use for generating the PDF due to compatibility issues with the standalone document class. Also note that the Liberation fonts can be found here: https://fedorahosted.org/liberation-fonts/

share|improve this answer

For pfdtex I prefere:

  {\settoheight{\dimen0}{C}C\kern-.05em \resizebox{!}{\dimen0}{\raisebox{\depth}{\textbf{\#}}}}}

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

share|improve this answer

How about these?

\def\CSH{{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\footnotesize\bf \#}}}

A direct variation of Prettiest way to typeset "C++" (cplusplus)?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.