Is there any (open source) solution for converting math expressions from a C-like expression syntax to LaTeX? When I say "solution", I mean either from outside LaTeX (i.e: an external program that makes the conversion and outputs LaTeX syntax), or from within LaTeX (i.e: some package that takes C-like math and displays it in formula format on-the-fly).
As an example, I'm asking for converting from 3*c/(a+2)
to \frac{3 c}{a + 2}
and yes, I know the conversion is not unique, as 3 \frac{c}{a + 2}
would also be a legal conversion.
I know some open source CAS packages, such as Maxima or SymPy can do this, but I was wondering if there's any other simpler solution with a lower overhead, not needing to install a full CAS system.
Best Answer
(I rewrote the code to make it consist of three separate files: A driver tex file that produces the screenshot; a style file (loaded by the driver file) that sets up the main user macro, called
\prettymath
; and a lua file (loaded from the style file via adofile
instruction) that performs the actual work.)The following LuaLaTeX-based solution should not be viewed as a full-blown tool. For now, it's mainly intended to showcase the usefulness and versatility of some of Lua's string functions for the purpose of tackling the problem at hand.
It processes
**
exponential notation, deletes single*
instances, converts the roman-letter representations of selected lowercase Greek letters (e.g.,alpha
andpi
) to their "native" Greek-character glyphs, and pretty-prints integral expressions, selected three-letter mathop functions (sin
,cos
,tan
, anddet
-- it should be straightforward to extend this list) as well as square roots and fractions. It can handle nested fractions and square roots. Naturally, this list is far from complete. Hopefully, the code shown below is reasonably self-explanatory so that (I hope) it's not too difficult to handle other cases.The driver file:
The style file; I suggest you call it
cmath2latex.sty
:The file that contains the Lua code; I suggest you call it "cmath2latex.lua":