# Make macro which checks the type of math mode its in [duplicate]

I frequently find that I want to make a macro, say a derivative macro for example with the following syntax:

\def\dd #1;#2;{ return... }


However, I want it to check if it is inline math such as when math is wrapped between two dollar symbols $...$ or if it is in a block of math such as an $...$ or align or equation environment. If the latter is the case then I want it to typeset

\frac{\mathrm{d} #1 }{\mathrm{d} #2 }


if it is inline math such as $...$ then I want it to typeset

\mathrm{d} #1 / \mathrm{d} #2.


How can I do this?

## marked as duplicate by Werner, egreg macros StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Mar 8 '15 at 17:31

The \mathchoice primitive is designed for this test.

\documentclass{article}
\textwidth4cm

\def\zz#1#2{\mathchoice
{\displaystyle \frac{\mathrm{d} #1 }{\mathrm{d}#2}}%
{\textstyle \mathrm{d} #1 / \mathrm{d} #2}%
{\scriptstyle \mathrm{d} #1 / \mathrm{d} #2}%
{\scriptscriptstyle \mathrm{d} #1 / \mathrm{d} #2}%
}
\begin{document}

$\zz{x}{y}$

$\zz{x}{y}$

\end{document}