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:
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.
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}
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
[1]).
xetex
or xelatex
) or LuaTeX (no idea what the commands are called, never used it yet …). - Konrad Rudolph
C#
in Evince. - Andrey Vihrov
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$}}}}
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:
\usepackage[libertine]{newtxmath}
and then use $\sharp$
. - musarithmia
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:
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/3714pdflatex
and you should get the PDF. You can use PDF files as images, if that wasn't clear. - theseion
graphicx
's scalebox
. - It'sNotALie.
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 Bad
\footnotesize
uses a smaller slash; the smallest three make do with their respective native slashes.The Ugly
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}
\@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
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.
\#
. - Andrey Vihrov
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.
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}
The even simpler C\scalerel*{\#}{X}
typesets as
You can try C\verb|#|
, if this form suits your purpose.
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
\texttt{C\#}
for your second picture. And you'll find this question useful: Prettiest way to typeset “C++”? - Leo Liu\newcommand{\textsharp}{$\sharp$}
- egreg\texttt{C\#}
. 'C' must be set in the normal font. - Andrey Vihrov\newcommand{\csharp}{C\raisebox{.8ex}{\scriptsize\#}}
?:)
- Paulo Cereda#
needs to be aligned with the C instead. - Filip Ekberg.8ex
in Paulo's comment to adjust the height. 0.5 gets it pretty close to level with the top of the C. - qubyte