2

I want to write a macro for a math map f:A\to B when it is in inline math mode and f:A\longrightarrow B for displayed math. But I don't know how to check that it is in inline mode or displayed style.

Here is my try:

\documentclass{article}

\newcommand{\map}[3]{#1\mathpunct{:}#2\longrightarrow #3}

\begin{document}

$\map{f}{A}{B}$ [\map{f}{A}{B}] \end{document}

I have tried the following but it produces Undefined control sequence. $\map

\makeatletter
\newcommand[3]{\map}{
    #1\mathpunct{:}#2
    \if@display
    \longrightarrow #3
    \else
    \rightarrow #3
    \fi}
\makeatother
C.F.G
  • 552

1 Answers1

7

You can't differentiate displaystyle and text style using a if test since use of the plain TeX \over can change the mathstyle after the fact. But you can use \mathchoice to provide four expressions (one for each math size (display/text/script/scriptscript)) and only the one for the right size will actually appear:

\documentclass{article}

\newcommand{\map}[3]{#1\colon #2\mathchoice{\longrightarrow}{\to}{\to}{\to}#3}

\begin{document}

$\map{f}{A}{B}$ [\map{f}{A}{B}] \end{document}