184

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?

yo'
  • 51,322
  • 4
    For reasons of completeness: there is a question about this in the C++ FAQs: http://www.parashift.com/c++-faq-lite/misc-environmental-issues.html#faq-40.2 – 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 –  Oct 17 '11 at 17:57
  • Asked also here: http://stackoverflow.com/questions/2724760/how-to-write-c-in-latex – 0 _ Jan 29 '16 at 03:05

11 Answers11

132

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

The C++ FAQ mentions this specific problem: https://isocpp.org/wiki/faq/misc-environmental-issues#latex-macros. 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}{\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.

Pait
  • 103
Pieter
  • 5,411
  • 17
    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 06:02
  • 1
    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
  • 2
    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
  • I've tested both variants that use \relsize and they both work. You have the relsize package included? – Pieter May 30 '11 at 15:05
  • 2
    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}\bf ++}}} 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
  • @Steve Markup in comments is like for inline code in answers, i.e. with backticks (``` ). – Xavier Apr 30 '13 at 15:59
  • Using the xspace package and tossing in an \xspace at the end might also be helpful. – Rhys Ulerich Jun 18 '13 at 10:16
  • As a beginner: How to implement the second approach? Copy-and-paste into body or preamble? Then just type C++ in body text? Tried this and that, didn't see a difference... MWE, sil vous plait :-) – nutty about natty Jul 06 '13 at 16:43
  • This worked in the body text: C\hspace{-.05em}\raisebox{.4ex}{\tiny\bf ++}. How to make it work as a "macro" ? – nutty about natty Jul 06 '13 at 16:49
  • Fails with KOMA class, because of undefined old font command '\bf'. There is no difference between \bf and \bfseries, correct? So replacing the bold command solves this – user1 Mar 27 '19 at 17:46
  • @Ben: I think the second pair of commands is meant to solve the undefined old font command '\bf' (at least it did for me). So this is the usecase the author of the answer was wondering about. – Icarus May 19 '19 at 10:13
  • These definitions seem to fail when used in captions. – Felix Crazzolara Dec 31 '20 at 10:40
  • First one looks great. – gsandhu Feb 13 '21 at 04:57
113

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

C++

Werner
  • 603,163
Nick Matteo
  • 2,427
23

Here's how C++ is typeset in the C++ standard:

\newcommand{\Rplus}{\protect\hspace{-.1em}\protect\raisebox{.35ex}{\smaller{\smaller\textbf{+}}}}
\newcommand{\Cpp}{\mbox{C\Rplus\Rplus}\xspace}

That looks like this:

enter image description here

Richard Smith
  • 351
  • 2
  • 4
11

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.

Caramdir
  • 89,023
  • 26
  • 255
  • 291
Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
9

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

 enter image description here

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

6

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.

Jan Hlavacek
  • 19,242
  • 1
    But having to think about font is very unLatex. (It goes against the whole Content without worry of formatting) – Frames Catherine White Feb 12 '12 at 09:36
  • 1
    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
  • 2
    @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 '14 at 23:23
4

I liked @kundors solution, but modified it so that C and + uses the same font and has the same weight. Here you can see the difference and decide your self:

C\texttt{++} \texttt{C++} 

C++

Jonas Stein
  • 8,909
3

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{$_{++}$}

Assuming 10pt size of the font; the '++' is in script style, which is size 7. Would it be better to use scriptscript style, having something like the following?

C\raise ??? \hbox{$_{_{++}}$}

Heimdall
  • 236
2

I found that the suggestion from the C++ FAQ looks bad in many contexts. So I abused inline math to build something that also looks good in titles etc.:

\usepackage{ifthen}
\newcommand{\CC}[1][]{$\text{C\hspace{-.25ex}}^{_{_{_{++}}}}
                      \ifthenelse{\equal{#1}{}}{}{\text{\hspace{-.625ex}#1}}$}

This takes the standard version as an optional argument: \CC typesets C++, \CC[17] typesets C++17.

The following shot demonstrates \CC[14] and \CC in context:

enter image description here

kamikaze
  • 121
2

Another in the string of correct ways is to use the Middlescript macros by V. Eijkhout, which adjusts to the font centerline and covers size changes pretty well (about as well as standard subscript and superscript). I won't copypaste the macro code since it's just a SE link, but with it you can use:

C\textmidscript{++}         % text-mode
$\mathrm{C\midscript{++}}$  % math-mode
1

After much effort, I finally landed on the following macro:

\usepackage{hyperref}
\usepackage{relsize}
\usepackage{xspace}
% ...
\newcommand{\Cpp}{\texorpdfstring{\mbox{C\raisebox{.35ex}{\textsmaller[2]{++}}}\xspace}{C++}}

Unlike the other answers here, it uses \texorpdfstring to avoid issues with hyperlinks and tables of contents. I also found that \relsize{-3} was broken somehow on TeXLive 2022 (at least on Overleaf), so I use \textsmaller[2] instead. Shrinking by 2 instead of 3 looked better in my document, but YMMV.


Demonstration:

demonstration of typesetting macro for "C++"