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

"C++" in plain text

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?

link|improve this question

1  
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
feedback

3 Answers

up vote 43 down vote accepted

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.

version 1

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

version 2

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}{\bf +}}\nolinebreak\hspace{-.10em}\raisebox{.4ex}{\relsize{-3}{\bf +}}}
\newcommand\CC{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}{\bf ++}}}

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.

link|improve this answer
7  
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
Pieter, thanks for your tip. It works if I omit the use of the \relsize command. Using \relsize gives the same error as before. Do you know how to get around this? – Steve May 30 '11 at 14:46
show 2 more comments
feedback

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

C++

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.

link|improve this answer
feedback

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.

link|improve this answer
But having to think about font is very unLatex. (It goes against the whole Content without worry of formatting) – Oxinabox Feb 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 5 hours ago
feedback

Your Answer

 
or
required, but never shown

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