I have some C source code and would like to show the circumflex or caret (^) in the way it is typically shown in source code -- as a full-size character. The source code will be appearing in typewriter font, and I'd like for the character to have the same (i.e. fixed) width as all the other characters.

Things that I tried (all inside \texttt{}) that don't work satisfactorily:

  • \textasciicircum produces a small, high circumflex

  • \char`\^ same

  • \verb|^| same

  • \wedge in math mode produces a symbol that is too big and too wide, upsetting alignment of subsequent columns

I'm also interested in showing a full-size tilde (~), though I see that has already been beaten to death here. I really hope the same hijinks won't be needed for the circumflex, but it would be good to know for sure either way!

As Will Robertson says, the rendering of the caret depends on the font. Here I give three examples:

  1. Computer Modern Typewriter in OT1 encoding
  2. Computer Modern Typewriter in T1 encoding
  3. Inconsolata

In all three the first caret is obtained with \textasciicircum and the second one with \^{}. In the first row the two carets are the same, in the second row the first caret is sligthly bigger and a bit lower; this is the symbol used by \verb if the default encoding is T1.

enter image description here

I just had this problem and did $^\wedge$, that looks pretty ok.

  • Interesting, I didn't realise you could start a math mode segment with a superscript! Will try this at some point. – j_random_hacker May 3 '11 at 15:08
  • 2
    Even better: {\tiny$^\wedge$}. :) – Peter Krumins May 4 '11 at 8:10

I usually use something like this:

\texttt{\char`\^}

Inside an appropriate macro, of course. Whether this looks acceptable will depend a little on what monospace font you're using.

  • Thanks, but that looks identical to \textasciicircum and \verb|^| inside a \texttt{} -- small and high. I'll update my question. Is it possible to use multiple monospace fonts in a single document, and do all monospace fonts of the same point size have the same fixed width? If so that might let me choose a different font for just the ^ character that doesn't mess with the alignment (I need to keep most characters in the standard monospaced font). – j_random_hacker Oct 8 '10 at 8:15
  • 3
    Yes, and no. But you could hack around the problem. I don't see what the problem is, though—the way the caret is displayed in TeX is no different than all other fixed width fonts on my machine. – Will Robertson Oct 8 '10 at 10:59

I just did a small recherche on both characters (after being quite shocked that I need to escape ^ which, while documented, I never needed so far yet), and I found out that switching the font looks like a good solution.

For serif (default/roman) fonts, I only looked at the default cmr and found both tilde and caret ugly but usable. (Most documents I deal with are set in sans-serif for body text… not my decision to make, though.)

For sans-serif fonts, I looked at the default cmss (ugly but usable, too), pxss (looking perfect; this is from the pxfonts package, though I recommend against using it and in favour of just adding \renewcommand{\sfdefault}{pxss}% to your document). I’ve also looked at the nōn-free fonts museo (via .ttf) and pfr (via Type1 glue); both are good but not quite as good as pxss.

For teletype fonts, I looked at cmtt (default, extremely ugly) and inconsolata (perfect; this font is my favourite ever since I discovered it for having a slashed 0 “the right way”; \usepackage[varqu,varl]{inconsolata}% to activate it).

Specifically for this answer, I looked at whether pxfont’s roman flavour is just as good as its sans-serif flavour (yes, it is).

With all fonts, I looked at \textasciitilde, \textasciicircum and \verb|X|, but the “verb” one was always inferior, so I now use the stock \textasciitilde and \textasciicircum macros to quote these characters.

Therefore, I recommend you to add the following to your document preamble…

\renewcommand{\rmdefault}{pxr}%
\renewcommand{\sfdefault}{pxss}%
\usepackage[varqu,varl]{inconsolata}%

… and then use either of these three fonts as it suits you. (However, I fully understand if you don’t want to switch fonts (.oO(except avoiding Inconsolata, which I can’t understand) ☻☺), and refer to the other answers to the question for that then.)

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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