10

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)}.

  • 4
    Maybe if you promise to always use the parentheses. But you didn't use them in the previous line. – Teepeemm Aug 07 '20 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. – Rmano Aug 07 '20 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. – Don Hosek Aug 07 '20 at 22:38
  • See my answer here: https://tex.stackexchange.com/questions/332012/translate-in-line-equations-to-tex-code-any-package/332061#332061 – Steven B. Segletes Aug 07 '20 at 23:53
  • Please clarify what some like use cases might be. E.g., do you mostly have simple expressions such as a/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
  • 1
    @DonHosek Typo. \let/=\over. – Donald Arseneau Aug 08 '20 at 09:34

2 Answers2

18

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.

wipet
  • 74,238
9

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

enter image description here

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}
Mico
  • 506,678
Henri Menke
  • 109,596
  • 2
    Oh, a silent revenge downvote :) At least I gave a technical reason for mine... – Henri Menke Aug 08 '20 at 05:15
  • 2
    I wrote a program called "naturalmath" which sounds similar to asciimath. http://faculty.missouri.edu/~stephen/naturalmath/ – Stephen Montgomery-Smith Aug 08 '20 at 05:50
  • @StephenMontgomery-Smith That looks interesting, thanks for bringing this up. I wanted to have a look at the Perl code but clicking http://faculty.missouri.edu/~stephen/naturalmath/naturalmath gives me a “403 Forbidden”. – Henri Menke Aug 08 '20 at 06:30
  • Unfortunately you will have to download the zip or tar file. Sorry. – Stephen Montgomery-Smith Aug 08 '20 at 06:38
  • @ Henri Menke: Could you explain why the expressions xml = xml or {}, lxml = lxml or {} and moduledata = moduledata or {} are needed? – Weißer Kater Aug 08 '20 at 08:43
  • @user125730 The 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
  • Some background info I just found by searching: Hans Hagen gave a talk named What if ..., on TUG2005 and published an article in TUGboat Volume 36 (2015), titled When to stop . . . and containing some discussion about support for asciimath format. – muzimuzhi Z Aug 08 '20 at 19:52
  • @muzimuzhiZ Yes, that's the talk I was referring to. – Henri Menke Aug 08 '20 at 22:10