This 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.)
There'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.
> 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!
The 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)
When 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.
You 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.
I am comparing latex with hand. Both are serial, except hand has more freedom. There is no mouse required to use latex. You can write arbitrary math expressions without the mouse.
I 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.
There 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).
Even mcedit shows matching braces, that's not tree editing :) That would be editor showing a formula in graphic form and allowing you to jump around on it and add/move around nested parts of it with shortcuts.
And on the off chance you want to use the array language J instead, I wrote and used 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.
This 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.
This 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.
Will 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.
If 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
As 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.
Jupyter, and ultimately Python, evaluates the expressions. handcalcs just displays the resulting information with variable values from the Python locals dict.
If 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.
I 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.
Pretty 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...
Sympy 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.
I 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!
This 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.
almost 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
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.)