# How do I define a math mode formula with a caret ^ in LaTeX?

This question has two parts. First, is there a way to define a macro only for math mode?

Second, how do I specifically get the following definition to work correctly?

\def\fi{f^{-1}}


One additional point is that I'm trying to create this definition within a local scope rather than global scope, but I'm not sure that's relevant.

• Redefining \fi, even in a local context, is likely to be a bad idea as it's a TeX primitive used in conditionals. Commented Jan 17, 2013 at 20:04
• For starters you cannot use \fi since it declares the end of an if statement. Commented Jan 17, 2013 at 20:04
• Good to know. Clearly I'm a neophyte @ TeX. :) Commented Jan 17, 2013 at 20:06
• I think \newcommand would be preferred over \def ... this applies to all the answers below! Commented Jan 18, 2013 at 8:53

  \def\fi{f^{-1}}


Don't even think of redefining \fi \fi is the TeX primitive (if backwards) that terminates every conditional construct. If you redefine this then more or less every construct in LaTeX will break.

  \def\finv{f^{-1}}


would define \finv to produce f-1 which is what I assume you want rather than a literal ^ in the printed output?

• In my answer I've added the conditional "unless you are a wizard". Surely, David, you are allowed to redefine \fi at will! Commented Jan 17, 2013 at 20:08
• Not \fi I'd redefine most things but not that unless you're in a latex3 mood and redefine (undefine) \if and \ifx and \ifmath and friends at the same time Commented Jan 17, 2013 at 20:14
1. You can use \ifmmode, e.g. \def\fim{\ifmmode f^{-1}\fi}

2. Do not redefine \fi unless you are a wizard. It is used in all \if.. constructions. Define \fim instead.

For starters \fi is a TeX primitive and it declares the end of an if statement. To answer your question you can use the amsmath package with:

\def\finv{\ensuremath{f^{-1}}}


This ensures you are in math mode every time. Yet a variant of this using the fixltx2e and xspace packages you can create the same command by using \TextOrMath{<text mode>}{<math mode>}:

\newcommand{\finv}{\TextOrMath{$f^{-1}$\xspace}{f^{-1}}}