
Handcalcs: Automatically render Latex equations from Python math code - amrrs
https://github.com/connorferster/handcalcs
======
mcqueenjordan
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.)

~~~
Terr_
> share a single place

I particularly like the idea of feeling reassured that whatever I publish for
humans also passed a test-suite.

------
abnry
This 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
> 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
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)

~~~
abdullahkhalids
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.

~~~
ajuc
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.

~~~
enriquto
this is exactly the purpose of vim, dude

~~~
ajuc
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.

~~~
enriquto
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).

~~~
ajuc
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.

~~~
enriquto
The show matching braces thing is not essential. But, barring the visual
formula, %-navigation through braces _is_ tree navigation.

~~~
ajuc
But if you have serialized code tree navigation isn't needed, the whole point
was to make it graphical.

------
montalbano
In 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)

~~~
mlochbaum
And 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
This is lovely

------
CGamesPlay
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.

------
jlukecarlson
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.

~~~
mhh__
Maybe, but if you can do Python I cannot imagine you not being able to do
LaTeX. The equations are the easy part.

~~~
macintux
It’s still one more thing to learn. There are lots of things I _could_ do that
aren’t on my priority list.

------
bronzeage
Sagemath with %display latex does practically the same thing, and it's much
more powerful symbolic execution engine. Works with matrices too.

------
jakeogh
going the other way LaTeX -> code:
[https://github.com/sympy/sympy/issues/5418](https://github.com/sympy/sympy/issues/5418)

------
smabie
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.

~~~
fiddlerwoaroof
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

------
etaioinshrdlu
Is this a kind of open source version of Mathematica's traditional form?
[https://reference.wolfram.com/language/ref/TraditionalForm.h...](https://reference.wolfram.com/language/ref/TraditionalForm.html)

------
justmyname
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.

~~~
cmferster
Jupyter, and ultimately Python, evaluates the expressions. handcalcs just
displays the resulting information with variable values from the Python locals
dict.

------
caterama
There'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?

------
maksimsum
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.

------
jimbob45
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.

~~~
xrisk
An easier way to achieve the same thing is to use Pandoc :)

------
tikej
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...

~~~
cmferster
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.

------
technicalbard
That it can do units too makes this a possible replacement for MathCAD!

~~~
cmferster
That is my hope! At least for more typical arithmetic.

------
TallGuyShort
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!

~~~
klysm
I could see that working with a direct `sum` invocation.

~~~
cmferster
That's the current plan.

------
abalaji
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.

------
RossBencina
The %%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
This can be done by 'unparsing' the python AST. We do this in the DaCe
project:
[https://github.com/spcl/dace/blob/900cac070c4b083653c672e225...](https://github.com/spcl/dace/blob/900cac070c4b083653c672e2250c07fd32d1e0dd/dace/codegen/cppunparse.py#L156)

------
modmans2nd
I feel like a latex language editor is easier.

~~~
cmferster
But it doesn't do the calculation for you. handcalcs is about performing
calculations and having it automatically show your work.

------
kareninoverseas
This is what I needed when I was writing my undergrad thesis. Would have saved
me hours of time. Alas.

------
lifeisstillgood
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

------
ggcdn
weird, 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)

------
hatmatrix
All the more reason to move to a declarative style of programming.

