0

This question already has an answer here:

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 Mar 8 '15 at 17:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

The \mathchoice primitive is designed for this test.

enter image description here

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

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