4

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!

2
  • 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 at 10:47
  • I want the command to detect the math mode itself and to determine which behavior. – Misaya Feb 17 at 12:42
8

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}
4
  • Thank you! Maybe this is the best method! – Misaya Feb 17 at 12:43
  • what's the problem with \ifinner...? – touhami Feb 17 at 15:58
  • @touhami \ifinner does absolutely nothing useful in math mode. (have a look at commath.sty to see examples of that..... – David Carlisle Feb 17 at 16:05
  • 1
    @DavidCarlisle Could you please elaborate on or provide a reference for the four latex styles? I was previously only familiar with two: display and inline. – user257566 Feb 22 at 6:12
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

1
  • Thanks very much! – Misaya Feb 18 at 1:45
1

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}
1
  • Well done! Thank you! – Misaya Feb 18 at 1:45
1

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} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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