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?


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.

  • 2
    Redefining \fi, even in a local context, is likely to be a bad idea as it's a TeX primitive used in conditionals.
    – Joseph Wright
    Commented Jan 17, 2013 at 20:04
  • For starters you cannot use \fi since it declares the end of an if statement.
    – azetina
    Commented Jan 17, 2013 at 20:04
  • Good to know. Clearly I'm a neophyte @ TeX. :)
    – merlin2011
    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

3 Answers 3


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.


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!
    – Boris
    Commented Jan 17, 2013 at 20:08
  • 3
    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:


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>}:


You must log in to answer this question.

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