How to define a math command depending on its mode (display or inline)?
Due to an unfortunate design choice in TeX it is not possible to directly determine the current math style (as a later \over
may change it) However the \mathchoice
primitive allows you to set the text in all four styles (display, text, script and scriptscript) with the appropriate one being chosen when tex assembles the final math list.
the inline 1/2 may benefit from some kerning and adjustment, but...
\documentclass{article}
\newcommand\zzfrac[2]{\mathchoice
{\frac{#1}{#2}}%
{#1/#2}%
{#1/#2}%
{#1/#2}%
}
\begin{document}
\[
\zzfrac{1}{2}
+
\frac{\zzfrac{1}{2}}{\zzfrac{1}{2}}
+
x^{\zzfrac{1}{2}}
\]
\end{document}
While David's answer is the canonical approach, the scalerel
package allows the same thing by way of a different syntax. The \ThisStyle{}
invocation essentially places the argument in a \mathchoice
and makes the current mathstyle available to you, generally, be by way of an invocation to \SavedStyle
(this is useful if you are in a place where the style is lost, for example, inside of an \hbox
). In your case, however, you don't need to invoke the current math style, you just need to know what it is, so as to decide what path to take. Inside a \ThisStyle
, the command \m@switch
contains a D
, T
, S
, or s
for the four (display, text, script, and scriptscript) math styles. So here, I check if it is a D
and decide accordingly.
\documentclass{article}
\usepackage{scalerel}
\makeatletter
\newcommand\zzfrac[2]{\ThisStyle{\if D\m@switch
\frac{#1}{#2}\else#1/#2\fi}}
\makeatother
\begin{document}
\[
\zzfrac{1}{2}
+
\frac{\zzfrac{1}{2}}{\zzfrac{1}{2}}
+
x^{\zzfrac{1}{2}}
\]
\end{document}