8

I want to define a math command which behaves differently in display mode and inline mode. For example, I want to define a \myfrac#1#2,which equals $\frac{#1}{#2}$, but equals $#1/#2$ in inline mode. An MWE is like the following:

\documentclass{article}
\makeatletter
\newcommand\myfrac#1#2{
  \ifdisplay \frac{#1}{#2}
  \else #1/#2
  \fi
}
\makeatother
\begin{document}
\[ \myfrac12 = \frac12 \]

$\myfrac12=1/2$. \end{document}

But I don't know how to jugde the math mode. Thank you for your adive!

Misaya
  • 476
  • Do you really want to depend on display mode or inline mode or do you want to react to the style (e.g. \displaystyle/\textstyle)? E.g. consider \frac{\myfrac{a}{b}}{c} in display math. The numerator is normally set in textstyle, so basically in the "inline math style". Which version of \myfrac should appear there? – Marcel Krüger Feb 17 '21 at 10:47
  • I want the command to detect the math mode itself and to determine which behavior. – Misaya Feb 17 '21 at 12:42

4 Answers4

11

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...

enter image description here

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

David Carlisle
  • 757,742
2

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}

enter image description here

2

I suppose that your \myfrac has to be different only in in-line math mode, i.e. between $...$. On the other hand, you wish to keep classical fractions in display style, i.e. between $$...$$.

It could be done by setting \everymath and \everydisplay registers:

\documentclass{article}
\usepackage{amsmath}

\setbox0=\hbox{$$} \everymath{\let\myfrac=\fracreduced} \everydisplay{\everymath{}} \let\myfrac=\frac \def\fracreduced#1#2{#1/#2}

\begin{document}

Onehalf in text: $\myfrac12$. % prints: 1/2

\begin{align} x&=\myfrac12 \ % prints: {1\over2} y&=\myfrac34 % prints: {3\over4} \end{align}

\end{document}

wipet
  • 74,238
2

I find an easy answer using amsmath package.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand\myfrac[2]{
  \if@display \frac{#1}{#2}
  \else #1/#2 
  \fi
}
\makeatother
\begin{document}
\[
\myfrac12
\]

$\myfrac12$ \end{document}

Misaya
  • 476