How can I write "C++" in LaTeX so that the output looks nice. For example C$++$
doesn't look good: the plus signs are too big and there is too much space.
-
2See also the suggested answers at tex.se.com: http://tex.stackexchange.com/questions/4302/prettiest-way-to-typeset-c – quazgar Aug 20 '12 at 16:05
-
You could have a look at this other question [LaTeX source code listing like in professional books](http://stackoverflow.com/questions/741985/latex-source-code-listing-like-in-professional-books), it looks awesome and works perfectly. – Dexter Jul 16 '11 at 14:22
7 Answers
The standard solution for cases like this is to use verbatim:
\verb!C++!

- 169,610
- 28
- 168
- 175
-
1i like this solution but the C looks bad...not like the other text, so i want to append the plusplus sign to a normal C i try with this but not work: C \thinspace\verb!++! – mjsr Apr 27 '10 at 21:47
-
21
-
But how do I make a definition, that *doesn't* swallow the following space; as this does: \def\cpp{C{}\texttt{++}} – user2023370 Mar 22 '11 at 15:46
-
2@user643722 You could include a non-breaking space (`~`) in the definition, but normally you escape the space after a command so LaTeX knows it's an actual space: "\cpp\ is fun". Otherwise it thinks the space is just separating the call to `\cpp` from the rest of the test – Michael Mrozek Mar 22 '11 at 17:51
-
1@user643722 The space is swallowed by the parser, so you can't fix that in your definition. The way I've seen and use myself is to enclose the macro inside braces like this "{\cpp} is fun", which frees the parser from depending on the following space to notice that the macro name ended. – Alberto González Palomo Apr 02 '12 at 15:09
-
@AlbertoGonzálezPalomo Long time after you've posted this, but generally in LaTeX to handle the space, a better way is to code things like this is as \cpp{} . There's also xspace, but I think that's overkill. – Joel Feb 01 '15 at 11:32
I've been using the code below to typset a nice looking C++ in my Master-Thesis. The code has been copied verbatim from a german forum. You should be able to just copy-paste all the code in a new .tex-document and pick the relevant stuff for you...
\documentclass{article}
\usepackage{relsize}
\usepackage{lipsum}
%c from texinfo.tex
\def\ifmonospace{\ifdim\fontdimen3\font=0pt }
%c C plus plus
\def\C++{%
\ifmonospace%
C++%
\else%
C\kern-.1667em\raise.30ex\hbox{\smaller{++}}%
\fi%
\spacefactor1000 }
%c C sharp
\def\Csharp{%
\ifmonospace%
C\#%
\else%
C\kern-.1667em\raise.30ex\hbox{\smaller{\#}}%
\fi%
\spacefactor1000 }
\begin{document}
\begin{center}
{\Huge C++ $\rightarrow$ \C++ \& \Huge C\# $\rightarrow$ \Csharp}\\
\bigskip
\ttfamily
{\Huge C++ $\rightarrow$ \C++ \& \Huge C\# $\rightarrow$ \Csharp}\\
\bigskip
\sffamily
{\Huge C++ $\rightarrow$ \C++ \& \Huge C\# $\rightarrow$ \Csharp}
\end{center}
\section{\C++}
\lipsum[1]
\subsection{\Csharp}
\lipsum[1]
\end{document}

- 11,432
- 6
- 35
- 51

- 1,157
- 6
- 18
-
thanks!! the results are nice...one questions that i don't understand in the code...how can you put C++ without math environment or \verb after \Huge? – mjsr Apr 28 '10 at 15:24
-
1sorry, I don't understand your question. The \Huge is just in the code to make it a bit more visible. If you're using the definitions at the beginning of the file, you should be able to just write \C++ anywhere in any environment... – Habi Apr 29 '10 at 21:32
-
ok thanks, i was a little confused with the use of the plus sign. I tough that the math environment was indispensable but now i realized that not. xD – mjsr Apr 30 '10 at 03:03
-
Not bad; looks a bit better to me like this: \def\ifmonospace{\ifdim\fontdimen3\font=0pt } \def\Cpp{% \ifmonospace{C++}\else{C\kern-.13em\raise.65ex\hbox{\textbf{\tiny{++}}}}\fi \spacefactor1000 } – blais Aug 08 '11 at 00:26
-
Great answer, although I'd prefer \textsf{C\kern-.05em\raise.43ex\hbox{\smaller[5]{\textbf{+\kern-.04em+}}}} – Kevin Bader Nov 08 '14 at 12:03
I've found that the following gives good results:
\def\Cplusplus{C\raisebox{0.5ex}{\tiny\textbf{++}}}

- 195
- 1
- 8
-
I like this result, and it doesn't require adding a whole bunch of extra stuff. – stochastic May 20 '19 at 02:59
This is what I used loooong time ago:
\newcommand*{\Cpp}{C\ensuremath{++}\xspace}
to be used like \Cpp
(needs xspace
package). But as you said, it is not really beautiful.

- 16,207
- 15
- 67
- 99
This answer, for the same question on the tex site, gives what I find to be a good looking way to this.
%C++
\newcommand\Cpp{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}{\textbf{++}}}}
%C#
\newcommand\Csh{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\relsize{-3}{\textbf{\#}}}
-
Can't edit because the edit queue is full, but there is a missing closing } on the C# line. – Arsenal Apr 07 '21 at 20:51
There is a \cpluspluslogo
command "with the ‘+’ signs properly positioned" in the package texlogos.
An other discussion on TeX - LaTeX Stack Exchange: Prettiest way to typeset “C++” (cplusplus)?.

- 737
- 1
- 5
- 24