share
TeX - LaTeXHow to make the correct hash-symbol in C Sharp (C#)
[+64] [11] Filip Ekberg
[2012-02-14 13:00:51]
[ symbols ]
[ https://tex.stackexchange.com/questions/44528/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:

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

(12) Use \texttt{C\#} for your second picture. And you'll find this question useful: Prettiest way to typeset “C++”? - Leo Liu
(5) If it's about music, \newcommand{\textsharp}{$\sharp$} - egreg
It's about programming, I've never seen the music-style on it before though. - Filip Ekberg
So it's not the note symbol and the question title should reflect this. - egreg
I thought it was, I'll change the title. - Filip Ekberg
(6) Don't use \texttt{C\#}. 'C' must be set in the normal font. - Andrey Vihrov
(1) Perhaps \newcommand{\csharp}{C\raisebox{.8ex}{\scriptsize\#}}? :) - Paulo Cereda
@PauloCereda, It does look exactly like the title page for the C# Language Specification, but it doesn't look good in normal paragraphs unfortunately. Maybe the # needs to be aligned with the C instead. - Filip Ekberg
How about this? \newcommand{\csharp}{C$^\#$} - qubyte
Oops, sorry, should have read your comment first. Mine looks exactly like Paulo's. Note that you can adjust that .8ex in Paulo's comment to adjust the height. 0.5 gets it pretty close to level with the top of the C. - qubyte
(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. - Andrey Vihrov
@AndreyVihrov, Agreed, do you have an example perhaps to make it look good? - Filip Ekberg
(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. - Ayman Elmasry
@egreg Your solution is correct either-way. - Ayman Elmasry
(1) @AymanElmasry: Indeed! Wikipedia says that the original intent was the musical symbol: en.wikipedia.org/wiki/C_Sharp_%28programming_language%29#Nam‌​e - Andrey Vihrov
[+45] [2012-02-14 14:56:05] Ayman Elmasry

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


(16) Yup! That’s why it’s called “C Sharp” and not “C Pound Sign” or “C Octothorpe”. - Todd Lehman
(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. - Blaisorblade
1
[+28] [2012-02-14 14:38:23] Andrey Vihrov [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.

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 [1]).

[1] http://mirror.ctan.org/macros/latex/required/graphics/grfguide.pdf

(1) 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
(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. - Konrad Rudolph
(1) @KonradRudolph, pdflatex was what came with texlive/texniccenter. You're saying I should upgrade to something else? - Filip Ekberg
(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). - Konrad Rudolph
(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
@FilipEkberg: I updated the answer with a workaround for pdfTeX. - Andrey Vihrov
@kahen True, my mistake. - Konrad Rudolph
Only problem is that it will not be searchable nor will it by possible to copy/paste it. - Filip Ekberg
@KonradRudolph, I don't want a TeX reader, I need it to produce a PDF for me to upload for publishing on CreateSpace, they take either PDF or Word Doc. - Filip Ekberg
@Filip Not sure what you mean by that. All the modern processors produce PDFs. - Konrad Rudolph
@KonradRudolph, I thought that was what pdfLatex was doing, producing the PDF? - Filip Ekberg
@Filip So does XeTeX (command xetex or xelatex) or LuaTeX (no idea what the commands are called, never used it yet …). - Konrad Rudolph
I'm on Windows, don't know if that came with the installation for TexLive, have to check it out later. - Filip Ekberg
@FilipEkberg: Did you try it at all? I can search for and copy C# in Evince. - Andrey Vihrov
@AndreyVihrov, How is that possible when it's an image and not text? No I haven't tried because I don't have XeTeX (as far as I know anyways) - Filip Ekberg
@FilipEkberg: It's text! Including a PDF does not mean converting it to an image. And you probably have XeTeX installed or easily available, you need to do a bit of research on how to use it. - Andrey Vihrov
Ok. Didn't know that. - Filip Ekberg
2
[+25] [2012-02-14 14:53:53] egreg

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


(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
(3) @littleO Like every LaTeX command, it ”eats“ spaces after it. Use ”backslash space“ or {} - egreg
(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
3
[+6] [2014-03-23 08:45:25] sjy

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 [1], 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] http://www.linuxlibertine.org/

(1) You can get a better-matched sharp symbol in Libertine if you add \usepackage[libertine]{newtxmath} and then use $\sharp$. - musarithmia
4
[+6] [2016-06-23 16:48:17] theseion

Building on the accepted answer [1] 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}
[1] https://tex.stackexchange.com/a/44537/3714

(1) Where do I get the hash-symbol image? Can you give a MWE? - littleO
(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
If you're using lualatex (and I assume also xelatex), you don't need the external document; you can just use graphicx's scalebox. - It'sNotALie.
5
[+5] [2012-02-16 04:44:17] Todd Lehman

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}

(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
6
[+4] [2012-02-14 15:28:17] kahen

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) Copying is somewhat possible: overlay the drawing on a zero-sized white \#. - Andrey Vihrov
7
[+4] [2012-06-13 09:00:28] Tarion

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.


8
[+3] [2017-06-29 13:10:51] Steven B. Segletes

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


9
[+2] [2018-12-11 02:17:39] Inc0gnito

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


10
[-1] [2016-07-02 22:31:41] Néstor Waldyd

How about these?

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

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

[1] https://tex.stackexchange.com/questions/4302/prettiest-way-to-typeset-c-cplusplus

11