TeX/ifmmode

From Wikibooks, open books for an open world
< TeX
Jump to: navigation, search

Synopsis[edit]

\ifmmode <TeX code 1> [\else <TeX code 2>] \fi

Description[edit]

The \ifmmode command denotes the start of an if-then-else control structure that tests whether TeX is currently in either math mode or display math mode. As indicated in the Synopsis, the \else part is optional. <TeX code 1> is processed if TeX is in one of the math modes, otherwise it is ignored. If the \else section is included and TeX is not in one of the math modes then <TeX code 2> is processed; otherwise it is ignored.

Examples[edit]

Here are some simple examples:

$\ifmmode\mathcal{A}\fi$

will reduce to

$\mathcal{A}$

While

\def\A{\ifmmode \mathcal{A} \else $\mathcal{A}$ \fi}

defines a macro \A that can be used both in and out of math mode to typeset a calligraphy script A.