TransWikia.com

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

TeX - LaTeX Asked by simplegamer on March 21, 2021

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

2 Answers

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]

unexpandeddefstopasciimath{stopasciimath}
unexpandeddefstartasciimath#1stopasciimath{%
  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
letlparent(
letrparent)

protecteddefdoasciimath#1{%
    %enableautofences % No straightforward LaTeX equivalent
    mathdelimitersmode="16 % Some magic number from ConTeXt
    directlua{asciimath("luaescapestring{detokenizeexpandafter{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}

Answered by Henri Menke on March 21, 2021

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

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

Writing out $x+1over(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 globallet /=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.

Answered by wipet on March 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP