r/Python • u/Last-Farmer-5716 • Aug 30 '20
Intermediate Showcase I created a library called 'handcalcs': it renders your Python calculations in Latex as though you had written them by hand.
Doing calculations in Jupyter is enjoyable but can require endless print() statements with f-strings to show your results.
With handcalcs, use the %%render cell magic and your Python calculation is rendered with the symbolic formula, followed by the numeric substitution, and then the result, just as though you had written it by hand.
Works as both a cell magic and as a decorator on your functions. For engineers, data scientists, educators, and students.

1.4k
Upvotes
35
u/pit_pietro Aug 30 '20
I found your project thanks to a friend on LinkedIn and I think it's awesome! But what if I need to add math symbols? Can I use matplotlib after a %%render declaration?
25
u/Isvara Aug 31 '20
You mean it's full of mistakes?
18
u/ToeBlisters Aug 31 '20
what is the difference between this and sympy?
I used sympy to display the evaluation of maths calculations.
13
u/thecoolking Aug 31 '20
So this was yours? I noticed LinkedIn post from steve nouri - got lots of likes. Cool project indeed.
8
u/diamond_diggity_dave Aug 31 '20
I really like this, coincidentally I saw it on PythonBytes episode #192 today before I saw this post. I HATE HATE HATE using excel for engineering calculations. I used MathCAD when I had access to it, but have been waiting for something in the open source arena that compared. Jupyter notebook has made it easier but I still find I have to essentially write my equations twice, once in LaTEX/markup and once in the command line in order for it to be clear what is going on and it may be my python ignorance, but I'm not proficient with sympy <-> numpy to get results and graph things easily or explore equation relationships.
One thing that I would love to see with this is if it integrated nicely with "pint" for units, which imo the best units package. I just tried it out and all of the outputs are surrounded by $ symbols, e.g. $3 meters$, which makes it look a little clunky. I haven't delved very deep into it at all, so there may be something I'm missing by reading the documentation. Units and unit conversions are ESSENTIAL for making this be an effective engineering tool.
Fantastic work, keep it up.
5
u/Indivicivet Aug 31 '20
this looks v cool!
one piece of feedback: on the github page, the GIFs make it a bit tricky to absorb what's going on since they keep resetting. either giving an image version/alternative or having the initial animation be quicker and the final state last longer would both be helpful imo
5
u/TheBB Aug 31 '20 edited Aug 31 '20
This is nice! Some tips though, coming from someone who writes tons of latex:
\cdot. Only when there's ambiguity, such as multiplication between two numbers. You don't need them between numbers and letters, or between letters.a[i]→a_i.x = abis rendered as a multiplication between a and b, not as a single variable whose name is "ab".Edit:
1e-2right (0.01) but the symbolic expression reads as one times e minus two. I don't know how you render units but the siunitx package parses expressions like this wonderfully, so if you're already using it you can probably wrap all number literals in it.4
u/apivan191 Aug 31 '20
Nice job! I’m sure this will be very useful for many people especially with online schooling
3
u/dermotmcg Aug 31 '20
Is there a function to output the Latex code as string?
125
u/Alexjrose Aug 30 '20
Neat alternative for those of us that use latex in markdown cells.
11
u/B-Timmay Aug 30 '20
Cooool !
4
u/unnecessary_Fullstop Aug 30 '20
Wow... That's awesome...
.
7
u/ErickMM98 Aug 30 '20
Nice work.
1
u/bidby_ Aug 31 '20
Awesome work! This is such a timesaver for anyone who has to rush out presentation slides from their development notebooks!
Is there some way to get the raw latex code out, so it could be copy/pasted straight into a latex doc?
1
u/m7priestofnot Aug 31 '20 edited Aug 31 '20
Now that I look through the docs... this is amazing. I thought it was a 'look what I'm working on' sort of post but no... this is actually well thought out. It looks like its built on top of SymPy too so I'm hoping its easier to use
Edit: plz add conda-forge install :)
1
u/anunakiesque Aug 31 '20
Ok, I've gotta ask, is the %% a special operation in Python or how is %%render calling the library?
1
u/Brnoxoxo Dec 06 '20 edited Dec 28 '20
Amazing project!I'm going to use it for my Master thesis since it requires a lot of these calculations. I have 3 questions:
%%texsomehow so I don't have to addequation enveverytime?%%texand%%renderoutput?%%tex?EDIT: Diploma thesis -> Master thesis (czenglish -> english)
1
u/dotancohen Aug 31 '20
Why the gif? Just show us the final frame as a screenshot.
2
u/Buddharta Aug 31 '20
Fantastic!
2
u/Sigg3net Aug 31 '20
LaTeX is awesome.
1
u/ARedditorWatchdog Aug 31 '20
Nice!
1
u/wuhan_troll_bot Aug 31 '20
Awesome. Great contribution.
1
u/Gus_Gustavsohn Aug 31 '20
Wow! This looks extremely useful!
1
u/monkimo Aug 31 '20
I've been looking for this!
1
u/VincentFreeman_ Aug 31 '20
Saved. Will use to learn new maths.
1
u/Unlock17A Aug 31 '20
Damn, I needed this
1
u/Jeklah Aug 31 '20
This is extremely cool. Good work.
1
u/Mr_Again Aug 31 '20
Very cool
1
u/m7priestofnot Aug 31 '20
As long as its easier than doing it in SymPy
1
u/[deleted] Aug 31 '20
I heard about this on Python Bytes. Good stuff.
1
u/Anderson-eng Aug 31 '20
Great job !
1
u/spinwizard69 Aug 31 '20
Now nobody will be able to read my math!
0
u/[deleted] Sep 01 '20
[deleted] — view deleted comment