10

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? Commented Feb 17, 2021 at 10:47
  • I want the command to detect the math mode itself and to determine which behavior. Commented Feb 17, 2021 at 12:42

4 Answers 4

13

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}
5
  • Thank you! Maybe this is the best method! Commented Feb 17, 2021 at 12:43
  • what's the problem with \ifinner...? Commented Feb 17, 2021 at 15:58
  • @touhami \ifinner does absolutely nothing useful in math mode. (have a look at commath.sty to see examples of that..... Commented Feb 17, 2021 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. Commented Feb 22, 2021 at 6:12
  • @user257566 (and future readers) math mode - What is \mathchoice? - TeX - LaTeX Stack Exchange Commented Jan 7, 2022 at 4:24
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! Commented Feb 18, 2021 at 1:45
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}
1
  • Well done! Thank you! Commented Feb 18, 2021 at 1:45
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} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.