Interpret a/(b+c) as \frac{a}{b+c}

Is it possible to make LaTeX, or a similar software, interpret a/b as \frac{a}{b}?

Writing out (x+1)/((x)(x+2)) seems much easier than writing \frac{x+1}{(x)(x+2)}.

• Maybe if you promise to always use the parentheses. But you didn't use them in the previous line. Aug 7, 2020 at 20:48
• In vimtex there is a command that switches between the two forms, which I personally don't use because they are graphically different and I want to use both, depending on the context. Aug 7, 2020 at 21:29
• My first thought is to make / active and \let/=\pver. The semantics will be a bit different: you'll need to drop the parentheses yourself and if it occurs in the middle of another expression you'd need to write, e.g., {a/b} = c but it might make you a little happier. Aug 7, 2020 at 22:38
• See my answer here: tex.stackexchange.com/questions/332012/… Aug 7, 2020 at 23:53
• @DonHosek Typo. \let/=\over. Aug 8, 2020 at 9:34

LaTeX is one of formats of TeX. And TeX has \over primitive. The usage is $numerator\over denominator$ or $...{numerator\over denominator}...$. The LaTeX macro \frac is defined (roughly speaking) by \over primitive: \def\frac#1#2{{#1\over#2}}.

LaTeX does not hide TeX primitives, so you can write:

Writing out $x+1\over(x)(x+2)$ seems much easier than writing $\frac{x+1}{(x)(x+2)}$.


You can set the / character as "math active character" and write:

{\catcode/=13 \global\let /=\over}
\mathcode/="8000

Writing out $x+1/(x)(x+2)$ seems much easier than writing $\frac{x+1}{(x)(x+2)}$.


Of course, if the scope of the fraction is less than whole formula, you must use {} in order to give this scope, for example ${a/b}=c$, as mentioned in the Don Hosek's comment.

It is really hard to crack down on all the corner cases that come with an approach using textual replacements. In general parsing such constructs requires a finite state machine to correctly render, e.g.

f(x)/g(x)


The syntax you are describing reminds me a lot of AsciiMath, a language to write mathematical formulae with only very few special symbols. I don't know of an implementation of AsciiMath in LaTeX, but there exists one in ConTeXt. This might be closer to what you are looking for. However, I'd like to quote Hans Hagen, the implementer of this code, from his talk at TUG 2015 (from memory)

“Writing AsciiMath is fun. You never know what output you will get.”

\usemodule[asciimath]

\unexpanded\def\stopasciimath{\stopasciimath}
\unexpanded\def\startasciimath#1\stopasciimath{%
\startformula
\asciimath{#1}%
\stopformula
}

\starttext

\startasciimath
a/b * alpha/omega * f(x)/g(x) * (x+1)/(a*(b+c))
\stopasciimath

\stoptext


You can actually use ConTeXt's AsciiMath in LaTeX with surprisingly little effort (but probably with limitations).

\documentclass{article}
\usepackage{amsmath}
\usepackage{unicode-math} % you have to use this for AsciiMath
\usepackage{environ}
\usepackage{luacode}
\begin{luacode*}

xml = xml or {}
lxml = lxml or {}
moduledata = moduledata or {}
statistics = statistics or { register = function() end }
require("x-asciimath")

function asciimath(str)
local texmath = moduledata.asciimath.convert(str)
assert(texmath) -- sledgehammer error handling
tex.sprint(texmath)
end

\end{luacode*}

% Some aliases from ConTeXt
\let\lparent(
\let\rparent)

\protected\def\doasciimath#1{%
%\enableautofences % No straightforward LaTeX equivalent
\mathdelimitersmode="16 % Some magic number from ConTeXt
\directlua{asciimath("\luaescapestring{\detokenize\expandafter{\expanded{#1}}}")}%
}

\NewEnviron{asciimath}{%
\begin{math}%
\doasciimath{\BODY}%
\end{math}%
}

\NewEnviron{asciidisplaymath}{%
\begin{displaymath}%
\doasciimath{\BODY}%
\end{displaymath}%
}

\begin{document}

\begin{asciimath}
a/b * alpha/omega * f(x)/g(x) * (x+1)/(a*(b+c))
\end{asciimath}

\begin{asciidisplaymath}
a/b * alpha/omega * f(x)/g(x) * (x+1)/(a*(b+c))
\end{asciidisplaymath}

\end{document}

• Oh, a silent revenge downvote :) At least I gave a technical reason for mine... Aug 8, 2020 at 5:15
• I wrote a program called "naturalmath" which sounds similar to asciimath. faculty.missouri.edu/~stephen/naturalmath Aug 8, 2020 at 5:50
• @StephenMontgomery-Smith That looks interesting, thanks for bringing this up. I wanted to have a look at the Perl code but clicking faculty.missouri.edu/~stephen/naturalmath/naturalmath gives me a “403 Forbidden”. Aug 8, 2020 at 6:30
• Unfortunately you will have to download the zip or tar file. Sorry. Aug 8, 2020 at 6:38
• @ Henri Menke: Could you explain why the expressions xml = xml or {}, lxml = lxml or {} and moduledata = moduledata or {} are needed? Aug 8, 2020 at 8:43