# Prettiest way to typeset “C++” (cplusplus)?

Writing "C++" in plain text results in an ugly setting, as the '+' signs are too big and too spaced:

I've seen around the web several marcos for typesetting the "C++" symbol, ranging from mild kerning to shrinking, raising and lowering the '+' signs. What is your version of a \cpp{} macro?

-
For reasons of completeness: there is a question about this in the C++ FAQs: parashift.com/c++-faq-lite/… –  Pieter Oct 19 '10 at 13:20
Do you mind adding that as an answer? It would be also very useful if you include some code here to copy&paste. –  Juan A. Navarro Oct 19 '10 at 13:25
I'd typeset it as "Cplusplus" :D –  user8656 Oct 17 '11 at 17:57

By general request (or something of the like at least), I made it an answer.

The C++ FAQ mentions this specific problem: http://www.parashift.com/c++-faq-lite/misc-environmental-issues.html#faq-40.2. The two things they wish to provide are a better typesetting and prevention of line breaks, two possibilities are given:

\newcommand{\CC}{C\nolinebreak\hspace{-.05em}\raisebox{.4ex}{\tiny\bf +}\nolinebreak\hspace{-.10em}\raisebox{.4ex}{\tiny\bf +}}
\def\CC{{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\tiny\bf ++}}}


The first one prevents a linebreak, raises the ++'s a little and puts them closer together.

The second only prevents a linebreak and raises the ++ a bit.

Both options put the ++'s in bold and typeset them a little smaller. Two more are given in the link, but I fail to see why they matter, line break prevention seems the must have for such a macro.

Edit: In case you want to use relative sizes you can use the relsize package, the code becomes

\newcommand\CC{C\nolinebreak\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}{\textbf{+}}}\nolinebreak\hspace{-.10em}\raisebox{.4ex}{\relsize{-3}{\textbf{+}}}}
\newcommand\CC{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}{\textbf{++}}}}


I don't have a favourite though, never needed to typeset C++ :). After experimenting a little, the second one is definitely my favourite, the version with kerning looks too much like the Haskell operator.

-
Hmm. Using \tiny here isn’t a good idea – what if I want to typeset “C++” in a header or in a footnote? –  Konrad Rudolph Oct 19 '10 at 14:59
@Pieter: the Haskell operator is usually set with a lot more overlap (when any is used at all, of course). –  SamB Dec 19 '10 at 6:02
Is there any way to use these commands within a \section{} declaration, eg. \section{\CC Revision}? I keep getting an error message about a brace not matching. –  Steve May 30 '11 at 11:54
You'll have to write \section{\protect\CC Revision}. –  Pieter May 30 '11 at 13:05
Yes, I have included the package. I've just managed to get it to work. My definition is \protected\def\CC{{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}\b‌​f ++}}} and then I can simply use \section{\CC Revision}. I'm not sure why I cannot get it to work following your suggestion, but you gave me a vital hint about protecting the command. By the way, I also use the \xspace macro at the end of the definition to handle spacing (not shown above). Apologies for the formatting - I am not sure how to markup code in the comments. –  Steve May 30 '11 at 15:19

Personally, I like the look of C\texttt{++} the best. It's also very simple!

-

I’ve previously used the following definition (using \scalebox from the graphicx package):

\newcommand*\cpp{C\kern-0.2ex\raisebox{0.4ex}{\scalebox{0.8}{+\kern-0.4ex+}}}


Note that it doesn’t do some things that Pieter’s solution does, in particular it doesn’t prevent a line break (is this even possible here?) – but this could be amended easily by putting it into an \mbox. I’ve also modified the kerning to make the two plusses merge into one. Whether this looks good somewhat depends on the font, though.

In hindsight, the plusses are too high for my taste.

-

I think it depends quite a bit on the font you use. I just tried the recommended macros with Lucida Bright, and in general the +'s were way too small and way too high. I got a reasonable result with the macros recommended in the C++ FAQ, but replacing \tiny by \small and raising the boxes only .2ex instead of .4ex. I suggest you experiment with your font.

-
But having to think about font is very unLatex. (It goes against the whole Content without worry of formatting) –  Oxinabox Feb 12 '12 at 9:36
In my experience there is nothing that can be done about this. It does depend on the font how high the ++ has to go. To the best of my knowledge there is no way to extract this knowledge somehow. Using the x-height is already the best you can do but it still depends on the shape of the 'C'. –  Christian May 16 '12 at 15:52
@Oxinabox -- logos aren't to be considered "traditional typography" -- they're often custom designed (even perhaps more often than not; the latex logo itself is not font-neutral). therefore, assuming that they're "automatic" in latex isn't appropriate. –  barbara beeton Feb 10 at 23:23

Another "correct" way is to use the texlogos package, which defines the C++ logo as:

\DeclareRobustCommand{\cpluspluslogo}{\hbox{C\hspace{-0.5ex}
\protect\raisebox{0.5ex}
{\protect\scalebox{0.67}{++}}}}


That said, I do prefer the appearance shown in other posts, particularly Pieter's second.

-
The question doesn't specifically ask for LaTeX, yet all answers so far are in LaTeX, none in TeX. My solution, which, of course, you can put into a \def or simply straight into the text if you only need it once:
C\raise .8ex \hbox{$_{++}$}
C\raise ??? \hbox{$_{_{++}}$}