3

I want to define a command \myfrac that takes 2 arguments and returns #1 / #2 if it is in text style (e.g. $\myfrac 1 2$), and returns \frac{#1}{#2} if it is in display style (e.g. \[ \myfrac 1 2 \]).

Are there any commands like \ifinline checking the style, or are there any alternatives for \myfrac?

3

Please try with the below tag:

\documentclass{book}
\usepackage{amsmath}
\begin{document}

\newcommand{\myfrac}[2]{#1/#2}
\everydisplay{\let\myfrac\frac}

$\myfrac1 2$

\[
\myfrac1 2
\]
\end{document}
  • notwisthstanding comment of @DavidCarlisle, it should be something like \everydisplay\expandafter{\the\everydisplay\let\myfrac\frac} to not overwrite possible stuff in \everydisplay. Although it is rare that packages use it (mathastext does). – user4686 Sep 2 '17 at 10:06
  • @jfbu yes (although it's somewhat accidental as the actual entries are all set in inline math with \displaystyle) but I'll delete and post a better comment, thanks. – David Carlisle Sep 2 '17 at 10:13
  • 1
    This only works if (after expanding macros) the math is entered by $$ not by $\displaystyle... $ also it forces the display style mode in all nested contests such as array cells (which are set in texstyle and in superscripts etc. – David Carlisle Sep 2 '17 at 10:15
6

You can use \mathchoice for that. It takes four arguments (for all four math modes) and typesets the first one in display mode, the second one in text mode, and the rest in script and scriptscript modes. The following code can illustrate that:

\documentclass{article}
\newcommand{\myfrac}[2]{\mathchoice{\frac{#1}{#2}}{#1/#2}{#1/#2}{#1/#2}}
\newcommand{\myfraca}[2]{\mathchoice{\frac{#1}{#2}}{(#1)/(#2)}{(#1)/(#2)}{(#1)/(#2)}}
\begin{document}
Text mode \(\myfrac12\). Display mode \[\myfrac12.\]

This is fine: \[\myfrac{x+y}{z+1},\] but this isn't: \(\myfrac{x+y}{z+1}\).

Both are fine: \[\myfraca{x+y}{z+1},\] \(\myfraca{x+y}{z+1}\).

But in this case parentheses are awful: \[\myfraca{x}{z},\] \(\myfraca{x}{z}\).
\end{document}

But as you can see, using \mathchoice isn't very suitable for fractions you want, since they sometimes need parentheses and sometimes don't. The result of the above code is the following:

enter image description here

6

Based on @SergeiGolovan's answer. This one tests how many tokens are inside one argument of \myfrac and if there are more than one, encloses them in parentheses. The starred version doesn't use the check.

\documentclass{article}
\makeatletter
\def\my@enparen#1#2;{%
  \ifx\relax#2\relax%
    #1%
  \else%
    (#1#2)%
  \fi}
\newcommand{\myfrac}{\@ifstar{\@myfrac}{\@@myfrac}}
\newcommand{\@myfrac}[2]{\mathchoice{\frac{#1}{#2}}{#1/#2}{#1/#2}{#1/#2}}
\newcommand{\@@myfrac}[2]{\mathchoice{\frac{#1}{#2}}{\my@enparen#1;/\my@enparen#2;}{\my@enparen#1;/\my@enparen#2;}{\my@enparen#1;/\my@enparen#2;}}
\begin{document}
Text mode \(\myfrac{x+y}{z+1}\). Display mode \[\myfrac{x+y}{z+1}.\]

This is fine: \[\myfrac{x_y}{z_1},\] but this isn't: \(\myfrac{x_y}{z_1}\).

Both are fine: \[\myfrac*{x_y}{z_1},\] \(\myfrac*{x_y}{z_1}\).
\end{document}

enter image description here

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.