8 ms·
Handcalcs: Automatically render Latex equations from Python math code
- abnry 6y agoThis is fantastic! The best use case I can think of is to verify your python formulas are correct. It is much easier to read math notation than code.
- enriquto 6y ago> It is much easier to read math notation than code. I agree. And for writing, the difference is even bigger than for reading. Thus, what we need is the reverse tool, that translates our beautiful formulas into the damned code!
- ajuc 6y agoThe problem isn't translation, it's input. Keyboard is designed for serial 1-dimensional input, formulas are nested trees. The approaches I've seen are: - OCR a handwritten formula (unreliable but the most convenient) - make the user write serialized code and show it as formula (basically this submission, but also latex etc. - misses the point) - force the user to design the formula tree with some graphical editor and fill it with values and identifiers later (office suites do this and it's such a pain to constantly switch between keyboard and mouse)
- abdullahkhalids 6y agoWhen you write formulas by hand, don't you write them in serial format i.e. symbol by symbol? The advantage of hand is that you can often write the symbols in whatever order you want, while latex has pretty much a canonical order. Eg. by hand, you can write down the denominator of a fraction before the numerator. While latex require numerator before denominator.
- ajuc 6y agoYou filled the numerator and want to switch to denominator - how do you specify that with keyboard? You could use TAB or something, but that denominator could be nested in 10 levels of other stuff so you probably use mouse or jump for 5 seconds randomly until you get to the right place. The problem is that you have to jump which "by hand" is natural (start writing slightly lower) while with keyboard requires switching to mouse and back or remembering shortcuts and modeling the tree structure of your formula in your head to know where the shortcut will jump. Same with introducing new levels of nesting. That's why I prefer just writing code for formulas over graphical editors.
- enriquto 6y agothis is exactly the purpose of vim, dude
- ajuc 6y agoI haven't seen tree-edit mode in vim, I've seen it in emacs for clojure (it was called paredit or sth like that) and I've tried to learn it but couldn't get used to it in a few days. Maybe it would work better for formulas.
- enriquto 6y agoThere are a lot of "fancy" packages tailored to latex and formulas like vim-latexsuite, that set up place holders that you can navigate around and whatnot. But even without all that, a very basic thing that you can do is move around the matching pairs of braces (and brackets, and other paired things) using % If you do not want to move, but only see where the matching brace is, you can "set showmatch" and it will flash the matching brace when your cursor passes through the other one. This is really helpful for writing nested formulas (although a better strategy is to add a lot of newlines and whitespace so that the structure of your formulas is clear).
- ajuc 6y ago
- abalaji 6y agoThis is really neat. I'm sure high school teachers with a little programming experience might enjoy setting this up with Google colab in Algebra and Calculus classes.
- hatmatrix 6y agoAll the more reason to move to a declarative style of programming.
- jlukecarlson 6y agoThis seems like a pretty helpful educational tool — you could sprinkle it in a project's documentation to help clarify the exact formulas being used. I could also imagine people using this if they aren't that comfortable with LaTeX and want an easier way to write it. They could just write out their formulas with Python, call @handcalc() to get the string output, then post it in their tex documents.
- mcqueenjordan 6y agoThis is very useful. My use case is that I often write python simulations of designs and/or as cost analysis simulations, but I also often want to put these conclusions into white papers. This is an excellent way to de-duplicate the work of getting the python simulation code and the latex code both correct, and as a bonus, they can share a single place to change. I'd love to be able to inline this code into my org documents, and have it display the latex output, but that might be asking for too much magic. (As in, not sure if the magic is worth the incantation.)
- Terr_ 6y ago> share a single place I particularly like the idea of feeling reassured that whatever I publish for humans also passed a test-suite.
- narwally 6y agoThere's already an emacs-jupyter package that provides an emacs api for interacting with the jupyter kernel through either a repl or org-mode source blocks. It provides output from the jupyter kernel in latex, svg, and png among others. What your asking probably wouldn't be to difficult and will probably find its way into one of the ipython notebook specific packages if it isn't already in one of them already.
- tikej 6y agoPretty cool but I think SymPy is more practical w.r.t. to formula transformations and rendering is as good in Jupyter notebooks. If it only had as good pattern matching and substitution rules as Mathematica...
- cmferster 6y agoSympy is for solving an original solution. handcalcs is for plugging numbers into formulas. Next major milestone is integrating sympy with handcalcs so it is easy to transition from the sympy steps to the calculation steps.
- montalbano 6y agoIn case people would like similar functionality in the Julia programming language, there is also a package for that: https://github.com/korsbo/Latexify.jl https://github.com/korsbo/Latexify.jl
- leephillips 6y agoThis and handcalcs are impressive. Maxima, the symbolic math program, also does TeX output.
- mlochbaum 6y agoAnd on the off chance you want to use the array language J instead, I wrote and used JtoLaTeX (https://github.com/mlochbaum/JtoLaTeX https://github.com/mlochbaum/JtoLaTeX) throughout college for physics and math papers and even a master's thesis. It can display or calculate formulas, and embed plots.
- andrewnc 6y agoThis is lovely
- etaioinshrdlu 6y agoIs this a kind of open source version of Mathematica's traditional form? https://reference.wolfram.com/language/ref/TraditionalForm.html https://reference.wolfram.com/language/ref/TraditionalForm.h...
- lifeisstillgood 6y agoalmost related but is there a project allowing editing of a .rst doc to be real time transformed into Html / latex via sphinx - I guess it's not too hard but the faffing about with servers seems like it would be nice if someone had already solved it
- ggcdn 6y agoweird, I was looking for this exact concept (except for Javascript code) literally right before checking whats new on HN. This looks great. Its super useful for when you need to perform calculations and also keep a saved record of it (like structural engineering calcs)
- deleted 6y ago[deleted]
- technicalbard 6y agoThat it can do units too makes this a possible replacement for MathCAD!
- cmferster 6y agoThat is my hope! At least for more typical arithmetic.
- smabie 6y agoWill it render my matrices/vectors? I suppose since Python doesn't have static typing that is unlikely. I've been using sage math a lot and the latex generation is really solid. It only renders greek letters correctly if you type them out though, it won't transform unicode greek letters to the correct latex equivalent. Though maybe XeTeX would solve this problem? I'm not sure.
- fiddlerwoaroof 6y agoIf it puts the Unicode character into the Tex source, verbatim, XeTeX is the way to go (or Luatex or Context, I believe): at this point I almost never use vanilla LaTeX
- modmans2nd 6y agoI feel like a latex language editor is easier.
- cmferster 6y agoBut it doesn't do the calculation for you. handcalcs is about performing calculations and having it automatically show your work.
- kareninoverseas 6y agoThis is what I needed when I was writing my undergrad thesis. Would have saved me hours of time. Alas.
- maksimsum 6y agoIf only you had created this sooner, I could have saved so much time writing up lab reports for my physics courses. I attempted something similar with Excel sheets and some bash scripts, but the effort involved took more time than just writing the lab report by hand.
- jakeogh 6y agogoing the other way LaTeX -> code: https://github.com/sympy/sympy/issues/5418 https://github.com/sympy/sympy/issues/5418
- TallGuyShort 6y agoI was hoping to see it detect loops that sum something and translate it into sigma notation but that's ambitious... Very cool, regardless - I'll be using this a lot!
- jimbob45 6y agoI know it’s off topic but there is no bigger cheat code in high school than knowing Latex. In no other way can you get an additional free 5% on every paper with virtually no additional time input past the initial investment.
- xrisk 6y agoAn easier way to achieve the same thing is to use Pandoc :)
- CGamesPlay 6y agoThis seems super-useful! Really interesting library that I earmarked for use next time I'm doing something math-heavy. Quick note about the site: the animated GIFs are not helpful. A static screenshot would let me see the result and give me time to understand what's going on. As it is, they jump back to an unrendered Jupiter cell almost immediately, and the animation is just typing; not really showing anything useful.
- justmyname 6y agoAs I see on the demo, it evaluates the expression itself. There is a ton of possibilities to misinterpret the domain of operations involved. Moreover, how should we control the details to which it should unwind the undergoing calculations? That way of work seems a bit too optimistic to me.
- aargh_aargh 6y agoFair point. It also provides the %%tex magic that gets you the source LaTeX snippet. May save you 80% of the effort, the rest you can tweak manually.
- cmferster 6y agoJupyter, and ultimately Python, evaluates the expressions. handcalcs just displays the resulting information with variable values from the Python locals dict.
- caterama 6y agoThere's also https://github.com/google/latexify_py https://github.com/google/latexify_py out there. Handcalcs seems to have a more extensive readme at least. Does anyone have a comparison?
- bronzeage 6y agoSagemath with %display latex does practically the same thing, and it's much more powerful symbolic execution engine. Works with matrices too.
- RossBencina 6y agoThe %%tex directive to convert Python to LaTex looks useful. Does anyone know whether there is a similar way to output the Python code in C++ syntax?
- crashocaster 6y agoThis can be done by 'unparsing' the python AST. We do this in the DaCe project: https://github.com/spcl/dace/blob/900cac070c4b083653c672e2250c07fd32d1e0dd/dace/codegen/cppunparse.py#L156 https://github.com/spcl/dace/blob/900cac070c4b083653c672e225...