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)}.
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)}.
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}
xml = xml or {},
lxml = lxml or {} and moduledata = moduledata or {} are needed?
– Weißer Kater
Aug 08 '20 at 08:43
x-asciimath module references some internal ConTeXt modules. Since the example doesn't use any of them I have put some dummy initialization. The statement a = a or {} assign a to itself if it is not nil, otherwise it initializes it to the empty table. Probably a = {} would do in this case but you don't want to accidentally erase something.
– Henri Menke
Aug 08 '20 at 09:23
vimtexthere 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. – Rmano Aug 07 '20 at 21:29/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} = cbut it might make you a little happier. – Don Hosek Aug 07 '20 at 22:38a/b,2/7, and(a+b)/(c+d)in mind, or are you also contemplating inputs such as\sin(\theta_1+\theta_2)/(c+d)? – Mico Aug 08 '20 at 06:26\let/=\over. – Donald Arseneau Aug 08 '20 at 09:34