64

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

15
  • 12
    Use \texttt{C\#} for your second picture. And you'll find this question useful: Prettiest way to typeset “C++”?
    – Leo Liu
    Commented Feb 14, 2012 at 13:03
  • 5
    If it's about music, \newcommand{\textsharp}{$\sharp$}
    – egreg
    Commented Feb 14, 2012 at 13:07
  • 6
    Don't use \texttt{C\#}. 'C' must be set in the normal font. Commented Feb 14, 2012 at 13:24
  • 3
    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. Commented Feb 14, 2012 at 14:07
  • 4
    @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. Commented Feb 14, 2012 at 14:46

11 Answers 11

28

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

17
  • 1
    It's a nice start, I need it to work with pdfText though, maybe someone else has some ideas on this too. Thanks! Commented Feb 14, 2012 at 14:52
  • 2
    @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. Commented Feb 14, 2012 at 15:30
  • 1
    @KonradRudolph, pdflatex was what came with texlive/texniccenter. You're saying I should upgrade to something else? Commented Feb 14, 2012 at 17:09
  • 1
    @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). Commented Feb 14, 2012 at 17:27
  • 4
    @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
    Commented Feb 15, 2012 at 3:41
45

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

2
  • 16
    Yup! That’s why it’s called “C Sharp” and not “C Pound Sign” or “C Octothorpe”. Commented Feb 16, 2012 at 3:22
  • 1
    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. Commented Jun 6, 2013 at 17:41
25

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:

enter image description here

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$}}}}

enter image description here

3
  • 2
    When I try this, I find that I have to manually insert a space by typing \, after \Csharp. The result doesn't look quite right. Could you modify your MWE to use \Csharp in the middle of a sentence?
    – littleO
    Commented Jun 28, 2017 at 3:00
  • 3
    @littleO Like every LaTeX command, it ”eats“ spaces after it. Use ”backslash space“ or {}
    – egreg
    Commented Jun 28, 2017 at 7:51
  • 1
    Oh ok, thank you. I actually didn't realize I could just use "backslash space" to make a smaller space than I get with \,. This answer was really useful to me!
    – littleO
    Commented Jun 28, 2017 at 7:55
6

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

\newcommand{\csharp}{C\nolinebreak[4]\raisebox{.6ex}{\includegraphics[scale=.8]{hash-symbol}}}

Which looks like this:

1

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/

MWE:

\RequirePackage{luatex85}
\documentclass[border=0pt]{standalone}
\usepackage{fontspec}
\begin{document}% I get extra space without this comment
\fontspec{Liberation Serif}\#
\end{document}
3
  • 1
    Where do I get the hash-symbol image? Can you give a MWE?
    – littleO
    Commented Jun 28, 2017 at 3:09
  • 2
    Added MWE. Run that through pdflatex and you should get the PDF. You can use PDF files as images, if that wasn't clear.
    – theseion
    Commented Jun 29, 2017 at 10:43
  • If you're using lualatex (and I assume also xelatex), you don't need the external document; you can just use graphicx's scalebox. Commented Jan 19, 2021 at 16:39
6

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 a proper sharp character:

Screenshot of different methods of rendering C# using XeTeX

1
  • 1
    You can get a better-matched sharp symbol in Libertine if you add \usepackage[libertine]{newtxmath} and then use $\sharp$. Commented Jun 23, 2016 at 17:11
5

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.

examples

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:

\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}
1
  • 4
    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
    Commented Feb 16, 2012 at 10:12
4

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.

1
  • 1
    Copying is somewhat possible: overlay the drawing on a zero-sized white \#. Commented Feb 14, 2012 at 16:35
4

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.

3

Here, I take a \# and, in superscript mode, make it the same vertical footprint as ig.

\documentclass{article}
\usepackage{scalerel}
\newcommand\myhash{$^{\scalerel*{\#}{ig}}$}
\begin{document}
C\myhash
\end{document}

enter image description here

The even simpler C\scalerel*{\#}{X} typesets as

enter image description here

2

You can try C\verb|#|, if this form suits your purpose.

0
-1

How about these?

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .