183

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?

4

11 Answers 11

130

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.

15
  • 17
    Hmm. Using \tiny here isn’t a good idea – what if I want to typeset “C++” in a header or in a footnote? Oct 19, 2010 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, 2010 at 6: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, 2011 at 11:54
  • 2
    You'll have to write \section{\protect\CC Revision}.
    – Pieter
    May 30, 2011 at 13: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, 2011 at 15:19
112

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

C++

1
  • 5
    Follows the KISS principle. +1
    – Shautieh
    Mar 20, 2017 at 7:38
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

3
  • 4
    ... and \smaller is provided by relzise package. Jun 18, 2015 at 21:15
  • 1
    ... and \xspace is provided by the xspace package. That said, \xspace introduces its own set of problems and it may be wise to avoid it. Jun 23, 2015 at 19:46
  • 3
    @PaulGaborit I think that you meant the relsize package (rather than relzise)
    – daneel
    Feb 19, 2020 at 21:03
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.

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.

1
  • +1 for the correctness. Not very good looking though.
    – Bex
    May 2, 2020 at 9:28
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.

3
  • 1
    But having to think about font is very unLatex. (It goes against the whole Content without worry of formatting) Feb 12, 2012 at 9: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, 2012 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. Feb 10, 2014 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++

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

1
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

1
  • 1
    The ++ is much too small for my taste.
    – vonbrand
    May 20, 2021 at 2:25
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++"

3
  • 1
    You forgot to add the use of hyperref.
    – projetmbc
    Nov 20, 2022 at 19:49
  • You should test \texttt{\Cpp} and `\textsf{\Cpp\}.
    – projetmbc
    Nov 20, 2022 at 19:54
  • 1
    @projetmbc - Added hyperref, thanks. I just tested \texttt{\Cpp} and \textsf{\Cpp} and they look reasonable. Just \texttt{\mbox{C++}} is probably better than \texttt{\Cpp}, though. Nov 20, 2022 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .