How to define the symbol ¡ in math mode so that it behaves as expected

OK, I know that the text symbol ¡ can be written as

!


but I need ¡ as a math symbol. If I write

\text{!}


or

\mbox{!}


then the symbol becomes italic in theorems and places where text, but not math, is italicized. How should I define the symbol ¡ so that it behaves as ! in math mode?

• Use \textrm{!}. – Bernard Jun 1 '16 at 9:50

I use

{\scriptstyle \text{\rm !}}


General hint: find someone who did what you want on the arXiv and download the source file :)

Define properly a mathchar for the reverse exclamation mark; for instance

\DeclareMathSymbol{\revexcl}{\mathclose}{operators}{'074}


and use

$n\revexcl$


which will also change size in superscripts and subscripts.

If you want to be able to input !, it's possible with some tricks.

\documentclass{article}

\DeclareMathSymbol{\revexcl}{\mathclose}{operators}{'074}
\DeclareMathSymbol{\excl}{\mathclose}{operators}{'041}
\makeatletter
\begingroup\lccode~=! \lowercase{\endgroup
\def~}{\@ifnextchar{\revexcl\@gobble}{\excl}}
\mathcode!="8000
\makeatother

\begin{document}

$n!$

$n!$

$A_{n!}$

$A_{n!}$

\end{document}