# Substitute \frac for a slash (/) only on inline math mode

I was wondering if there is a way to have LaTeX interpret $\frac{a}{b}$ as $a/b$, but $\frac{a}{b}$ as $\frac{a}{b}$.

It is a little tedious to change a inline equation $a/b$ to a display equation $\frac{a}{b}$ when doing changes to my text.

• The best thing to do might be \newcommand\sfrac{#1/#2}, then you keep the argument separation, and it is just one extra letter. Actually detecting inline math might be difficult. – daleif Jun 29 '16 at 22:40
• IMHO, the problem is not how to detect in-line math mode (there exists \ifinner for that), but rather the fact that the semantics might change when switching from one notation to the other: compare \frac{x+y}{x-y} with (x+y)/(x-y) and with x-y/x-y. Generally speaking, I wouldn’t recommend trusting an automatic substitution like this except in very simple cases. – GuM Jun 29 '16 at 23:17
• @GustavoMezzetti \ifinner is not a test for inline math, there are many items in display math that also end up with \ifinner being true. – daleif Jun 30 '16 at 7:07
• @daleif: Good point, I spoke without reflecting enough. A selection based on \mathchoice, that is, on the current style, is much more reliable, at least for this kind of problem. – GuM Jun 30 '16 at 22:42

Loading amsmath naturally switches \frac to \tfrac when in "inline math" (or text style) and \dfrac under "display math" (or display style). This provides a better layout: \documentclass{article}

\usepackage{amsmath}

\begin{document}

See $\frac{1}{2}$, or
$\frac{1}{2} \quad e^{\frac{1}{2}}.$

\end{document}


However, you can update \frac to condition based on the style using \mathchoice: \documentclass{article}

\let\oldfrac\frac% Store \frac
\renewcommand{\frac}{%
\mathchoice
{\oldfrac{#1}{#2}}% display style
{#1/#2}% text style
{#1/#2}% script style
{#1/#2}% script-script style
}

\begin{document}

See $\frac{1}{2}$, or
$\frac{1}{2} \quad e^{\frac{1}{2}}.$

\end{document}


Given that (say) \frac{x+y}{x-y} will be set as x+y/x-y when in text style, and technically have an incorrect mathematical representation, one could venture one step further. That is, measure the numerator and denominator width, and set it inside brackets if necessary: \documentclass{article}

\let\oldfrac\frac% Store \frac
\makeatletter
\newcommand{\groupit}{(#1)}% To group...
\newcommand{\nogroupit}{#1}% ...or not to group
\renewcommand{\frac}{%
\setbox\z@\hbox{$#1$}% Store numerator
\setbox\tw@\hbox{$#2$}% Store denominator
\ifdim\wd\z@>1em \let\groupornot@i\groupit\else\let\groupornot@i\nogroupit\fi% Measure numerator
\ifdim\wd\tw@>1em \let\groupornot@ii\groupit\else\let\groupornot@ii\nogroupit\fi% Measure denominator
\mathchoice
{\oldfrac{#1}{#2}}% display style
{\groupornot@i{#1}/\groupornot@ii{#2}}% text style
{\groupornot@i{#1}/\groupornot@ii{#2}}% script style
{\groupornot@i{#1}/\groupornot@ii{#2}}% script-script style
}
\makeatother

\begin{document}

See $\frac{1}{2}$ or $\frac{m}{x^2}$, or
$\frac{1}{2} \quad e^{\frac{1}{2}} \quad e^{\frac{x+y}{x-y}}.$

\end{document}

• nice! Can you just clarify what is the difference between text style, script style and script-script stile? – josinalvo Jun 29 '16 at 22:54
• @josinalvo: x is in display style under $x$, in text style under $x$, in script style under $a^x$ and in script-script style under $a^{b^x}$ or higher super-/sub-scripts. – Werner Jun 29 '16 at 23:07
• Mmmh… What about \frac{x+y}{x-y}? – GuM Jun 29 '16 at 23:10
• @GustavoMezzetti is right --- the problem is that semantically \frac{x}{y} is equivalent to (x)/(y) (grouping included), not to x/y. Still, TeXnically the answer is correct. A warning about semantic would be useful, though. – Rmano Jun 30 '16 at 6:27
• @GustavoMezzetti: I've added some flair to the choice, grouping as is necessary based on an elementary width check. – Werner Jun 30 '16 at 7:01