How do I show a caret (^) in math mode in LaTeX? [closed]
Asked Answered
C

7

21

I'm trying to display a caret (^) in math mode in LaTeX to represent the exclusive or operation implemented in the "C languages". By default, ^ is for creating a superscript in math mode. The closest I can seem to get is by using \wedge, which isn't the same.

Clergy answered 1/12, 2008 at 3:59 Comment(4)
For what it's worth, \wedge is the common symbol for AND (conjunction), so it's not a good idea to use a similar-looking character for exclusive OR. Unless you are typesetting code, in which case you should not be using math mode anyway.Codel
I highly recommend the short math guide which includes a list of symbols.Agger
This should go in latex overflow pageArielle
@Arielle Do you mean http://tex.stackexchange.com/? I think old questions like this will not be migrated.Right
R
20

You might want to use the common symbol for exclusive or instead, \oplus (but give it a proper name with something like \newcommand\XOR{\oplus}).

The caret is a bit too small to be noticeable as a binary operator. However, if you do insist using a caret instead, use this:

\newcommand\XOR{\mathbin{\char`\^}}
$x \XOR y$

The \mathbin puts the right spacing around the symbol for a binary operator, and the \char ensures that the glyph is obtained from the roman font.

Requiem answered 1/12, 2008 at 4:53 Comment(2)
There are at least four "common symbols" for exclusive or, depending on context. I wanted the one used in the "C languages" which is the carat. The real question that hasn't been mentioned is "why am I using math mode for code?" I probably should have just used a verbatim environment.Clergy
Good point with which symbol to use. Take a look at the listings package, if you haven't already. It can do code printing very nicely.Requiem
P
11

How about

\^{}

or

\verb|^|

?

Portsalut answered 1/12, 2008 at 4:5 Comment(1)
\verb is never allowed inside the argument to other commands, so is of limited use for this sort of thing.Requiem
F
11

Use the wedge symbol as a superscript. It has the perfect size.

Something like this:

$ ^\wedge $
Freemason answered 20/9, 2011 at 7:45 Comment(0)
S
5

Use \textasciicircum in text mode. If you are in math mode, you need to use something like $\mbox{\textasciicircum}$.

Strikebound answered 20/10, 2009 at 14:9 Comment(0)
A
4

This is what I did:

$2\hat{\text{ }}3$
Atiana answered 17/2, 2012 at 16:41 Comment(0)
A
3

Within math mode, you can use:

$7 \^{ } 3 = 4$

to do this, as shown from the online LaTeX renderer here.

enter image description here

Amsterdam answered 1/12, 2008 at 4:16 Comment(1)
Actually I found this editor seems to double backslashes before passing to LaTeX, so e.g. \alpha\beta produces the output alpha and then beta on the next line, rather than the actual Greek symbols for alpha and beta on a single line. OTOH the "original" equation editor page worked: codecogs.com/latex/eqneditor.phpStonework
F
2

\^ is an accent character that applies to other characters, \^{4} gives a 4 with a carat on its head. It takes up no horizontal space. If you write $7 \^{} 3 = 4$ you get a 73 with a mark smashed onto both the 7 and the 3. What you need then is to fill out the space a little bit. Through trial and error and a nice application called LatexIt, I found this sequence to work beautifully:

\hspace{1.5} \^{} \hspace{1.5}  

This gives a 3 unit width with the carat centered in it. It looks nice.

It is a pointy carat though, and \verb|^| gives a more flattened one that looks more like a monotype font frequently used in programming languages.

Foucquet answered 1/12, 2008 at 4:22 Comment(3)
I'm not sure this should be the accepted answer. As noted below, \^ isn't valid in math mode. Furthermore, rather than inserting manual spaces, use \mathbin{} instead to get the spacing right (see fntguide.pdf).Requiem
I know it's not the cleanest answer, but it works on my machine, you know how that goes...Foucquet
If by "works" you mean "gives an error" :) I think it's a bad design decision that some LaTeX IDEs are set up to scroll past all the errors and warning.Requiem

© 2022 - 2024 — McMap. All rights reserved.