# Converting text math to TeX math (e.g. automatically filling in \left(, \right), \frac, etc)

At some point, it occurred to me that another level of abstraction may be possible on top of LaTeX. For example, why should I type

\left( \frac{\frac{a}{b + c} \frac{w + \lambda}{z}}{x\left[ \frac{c_n}{d}\right]} \right)\left \{\frac{x_0}{y_0} : x_0 > 0 \right \}


when I could say the same thing, more compactly, with

({{a}/{b + c} {w + \lambda}/{z}}/{x[{c_n}/{d}]} ){{x_0}/{y_0} : x_0 > 0}


or even

({a/{b + c} {w + \lambda}/z}/{x[c_n/d]} ){x_0/y_0 : x_0 > 0}


Sure, conversion from the latter to the former may not be general enough for all use cases, but it should be okay for many of them. It's also somewhat easier to read/debug, what with the fewer words running around.

I think I am going to try to implement such a command, \textmath{...} in LuaTeX (the {} should not be a problem since the parentheses are supposed to be balanced) that does precisely this conversion. But before I attempt this, my question is: is there already a package that does something similar?

After all, I'd hate to reinvent the wheel, and I suspect this question may be also on some other peoples' minds.

Thank you!

Note: this question isn't about the macros that many TeX editors have, convenient as they may be. While they may allow you to perform the topmost expansion automatically, that does not help readability in the long-run :(

-
Think to \frac{\Li n}{n/\log n} (\Li is defined suitably) which should not be rendered as \frac{\Li n}{\frac{n}{\log n}}. Also adding \left and \right everywhere is not a good idea. –  egreg Sep 29 '11 at 23:20
Sorry, I don't understand what you mean by \Li. But thinking about it, you're probably right in that I'd need to allow the user to distinguish between frac and nonfracs. Is there a particular reason \left and \right's are bad, since they would be invisible to the user when lua tex.print(...)s them? –  scallops Sep 29 '11 at 23:27
That's just an example: a student had to typeset a table comparing the "integral logarithm" (Li) and the function "n/log n" when studying the distribution of prime numbers. The point is that not all fractions should be rendered in the "numerator-above-denominator" form. About \left and \right: they influence the spacing and also the placement of exponents, often in nonoptimal way. –  egreg Sep 29 '11 at 23:37
Maybe you could use the formatting tools of some CAS (e.g. Sage) to do the conversion. (I do however agree with egreg that in general this won't produce optimal output.) –  Caramdir Sep 30 '11 at 1:02
@Mateus Look at $(a+b)^2$ and $\left(a+b\right)^2$; moreover in the second formula the spaces around the + are frozen, because \left-\right makes a subformula. –  egreg Sep 30 '11 at 8:13

ConTeXt provides a calcmath module (short for calculator math) that partially implements what you want.

\usemodule[calcmath]

% Needed because there is a typo in the code what prevents auto scaling of
% delimiters
\startluacode
function moduledata.calcmath.tex(str,mode)
context(moduledata.calcmath.totex(str,mode))
end
\stopluacode

\starttext
\displaycalcmath{cos(a/(b + c) log(w+λ)/z)}
\stoptext


gives

This module was meant to implement features present in calculator math, so it only supports a single style of parenthesis and does not support subscripts or superscripts, although it should be easy to add them.

The lua code is in x-calcmath.lua file in the context distribution. This code is from the early days of luatex (circa 2006), and if it were to be rewritten now, it will be written using lpeg rather than regexs. The x-calmath.lua file has a partial lpeg implementation that is commented out.

-
Wow! I see that ConTeXt can not only typeset, it can do symbolic manipulation as well. Mind you, I think it needs to take a maths class again. I'm not sure how it got sum(a/(b+c) log(w + l)/z) to be (cos(a/(b+c)) log((w + l)/z)). –  Loop Space Sep 30 '11 at 6:46
@Andrew: Oops, I added the cos term after I had written an answer without the cos term, and made a typo while updating the answer. I have corrected the post. –  Aditya Sep 30 '11 at 6:55
He he! That still doesn't look quite right, though. I will accept that log (w + l)/z is ambiguous so while I disagree with ConTeXt's choice then I'll merely grumble quietly to myself. But the first is not. The entire expression should be inside the cos. (It occurs to me that you merely updated the code without regenerating the picture, so it may not be ConTeXt's error.) –  Loop Space Sep 30 '11 at 7:12
Also, there should be no space between cos and the opening parenthesis. I think this is one of the examples where replacing all delimiters by auto-scaled ones doesn't work reliably. –  Philipp Sep 30 '11 at 7:35
@Andrew: You are right, it is completely my mistake. I should not be fiddling with the details late at night. I have attached the output that I get by running the posted code. From what I understand, x-calcmath module is meant for school kids typesetting math and as such follows the convention of TI calculator. Not having used a TI calculator, I cannot comment on whether the handling of log is consistent with the TI rules or not. I think that the Microsoft linear math format is a good input format for such expressions. It will be nice to support that within the TeX world. –  Aditya Sep 30 '11 at 18:42