5

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 :(

Caramdir
  • 89,023
  • 26
  • 255
  • 291
scallops
  • 305
  • 2
    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
  • 3
    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 01:02
  • @scallops: have a look at the nath package, it does something very close to what you want for the \left...\right part, but treats fractions in a special way. – Philippe Goutet Sep 30 '11 at 04:28
  • @egreg You can easily correct the spacing issues of \left and \right; see http://tex.stackexchange.com/q/2607/1035 .mathtools also implements a similar solution. But what you say about the placement of exponents? – Mateus Araújo Sep 30 '11 at 05:34
  • 1
    @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 08:13
  • @PhilippeGoutet nath looks very promising! Unfortunately, even their example .tex file fails to compile on my system (TeX Live 2011). Is it working on yours'? – scallops Sep 30 '11 at 12:55
  • @egreg Indeed, and my macros do not solve this issue. I'll look into it. – Mateus Araújo Sep 30 '11 at 17:34
  • @scallops: what example.tex file? the nath manual (nathguide.tex) doesn't compile without error, but I never had a problem with a file using nath. – Philippe Goutet Oct 01 '11 at 07:21
  • @PhilippeGoutet Sorry, I was referring to nathguide.tex. I've been trying to figure out weird compatibility errors between nath and my existing documents; there seems to be a new one for each doc! Shame because otherwise, it's almost exactly what I'm looking for. But I'll see what I can make of it. – scallops Oct 04 '11 at 03:54

1 Answers1

9

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

enter image description here 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.

Aditya
  • 62,301
  • 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)). – Andrew Stacey Sep 30 '11 at 06: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 06: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.) – Andrew Stacey Sep 30 '11 at 07:12
  • 2
    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 07: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
  • @Philipp: IIUC, ConTeXt is just parsing the text as \cos\left(...\right) and letting TeX do the spacing. So the incorrect spacing is a "bug" with TeX, or perhaps a misconfiguration of the math parameters in ConTeXt. Is the spacing correct if you use \cos\left(...\right) in plain TeX? – Aditya Sep 30 '11 at 18:48
  • @Aditya: It's a feature of TeX, but maybe some think it's a bug. See also the comments to the question. – Hendrik Vogt Jun 29 '12 at 11:26