TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 Jan 17 '13 at 20:04
For starters you cannot use \fi since it declares the end of an if statement. – azetina Jan 17 '13 at 20:04
Good to know. Clearly I'm a neophyte @ TeX. :) – merlin2011 Jan 17 '13 at 20:06
I think \newcommand would be preferred over \def ... this applies to all the answers below! – Ari Brodsky Jan 18 '13 at 8:53
up vote 12 down vote accepted

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?

share|improve this answer
In my answer I've added the conditional "unless you are a wizard". Surely, David, you are allowed to redefine \fi at will! – Boris Jan 17 '13 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 – David Carlisle Jan 17 '13 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.

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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