co1emi11er2 / Handcalcs.jl

Julia package for converting Julia calculations into rendered latex.
https://co1emi11er2.github.io/Handcalcs.jl/stable/
MIT License
66 stars 1 forks source link

@handcalc does not expand predefined variables (ERROR: LoadError: type Symbol has no field head) #10

Open TS-CUBED opened 3 months ago

TS-CUBED commented 3 months ago

@handcalc does not expand predefined variables.

julia> b = 2
2

julia> @handcalc b
ERROR: LoadError: type Symbol has no field head
Stacktrace:
 [1] getproperty(x::Symbol, f::Symbol)
   @ Base ./Base.jl:37
 [2] var"@handcalc"(__source__::LineNumberNode, __module__::Module, expr::Any, kwargs::Vararg{Any})
   @ Handcalcs ~/.julia/packages/Handcalcs/4RsVs/src/handcalc_marco.jl:33
in expression starting at REPL[30]:1

but

@handcalcs does:

julia> @handcalcs b
L"$\begin{aligned}
b &= 2
\end{aligned}$"

In most cases I simply use @handcalcs even for single equations, to avoid this issue, but I'd like to use something like:

md"'''
The pressure at the inlet is $(@handcalc p_i), ...
"""

in Quarto, which works if I use:

md"'''
The pressure at the inlet is $(@handcalcs p_i), ...,
"""

but since that wraps the equation in an aligned environment, it does not flow nicely with the text. There is added whitespace around the equation.

co1emi11er2 commented 3 months ago

Good point! I will try to look into it when I get time. I think you may try @latexdefine from latexify for that scenario…Actually that doesn’t seem to inline it nice either.

TS-CUBED commented 3 months ago

@latexdefine works nicely here:

```{julia}
md"""
A helium filled stratospheric balloon is designed for an altitude of $(@latexdefine H) with a maximum volume of $(@latexdefine V_max).
The balloon is filled with $(@latexdefine m_He) of helium.
The molar mass of helium is $(@latexdefine M_He).
The balloon itself has a mass of $(@latexdefine m_b) including all the rigging.
"""


Gives me:

![image](https://github.com/co1emi11er2/Handcalcs.jl/assets/52218856/8335542f-a040-484d-863c-a71942d29fd0)
TS-CUBED commented 3 months ago

Thank you so much for this package, btw.

I am hopeless with putting numbers into equations and my solutions used to be riddled with wrong results (doesn't really matter if we tell the students that it's the way to the result that carries the marks, not the final result).

So I switched to using Julia notebooks to do the calculations. Which led to two problems:

  1. the solutions were now full of code and students couldn't read them anymore (solved by Handcalcs.jl)
  2. I still am hopeless with numbers, so the numbers in the text of the questions and the numbers as defined in the first code block were not necessarily (actually quite rarely) the same, leading to wrong numbers in the now otherwise beautifully typeset (thanks to Handcalcs.jl) solutions.

With this latest change I can define the values in a codeblock and then generate both the question text AND the solution from the same parameters, hopefully leading to correct solutions (finally).

co1emi11er2 commented 3 months ago

Awesome! Glad it worked. Yeah I think @latexdefine is the best thing to use for those scenarios. I can maybe try and clean that up eventually, but on the top of my head it seems like it may add a bit of complexity.

Thanks! I am glad it is helping! That is the main goal for me too, just to make it look like math not code. I will also mainly be integrating with quarto. I am hoping that Pluto soon gets quarto integration.

TS-CUBED commented 3 months ago

Did you manage to find a solution to the squared variables with units problem? I could just take your solution with dev Latexify my own fork of Latexify, but I don't think I can convince my colleagues to do that (some of which are starting to adopt my approach to creating teaching material with Quarto*).

*I started doing it with org-mode, but that would have been a lost battle from the start.

co1emi11er2 commented 3 months ago

I am currently just using the dev Latexify method, but I do 100% plan to get it into Latexify. I am currently working on a big internal tool that will use Handclacs and Unitful, but I am sort of focusing on getting the overall tool done and working on my computer and then will push to get it working on Latexify.

Maybe I will switch focus and get that working on Latexify. I need to register my own units package anyway (just a package that registers units based on my area of profession to Unitful), so maybe I will do that next. The maintainers of Latexify are sort of busy at the moment though, so that's why I sort of pushed it off.

I want to get this maybe working too:

using Handclacs, StructuralUnits #my units package
@handcalcs begin
    b = 5.0ft
    h = 120.0inch
    area = b*h |> ft^2
end

and it will render like so: CleanShot 2024-05-25 at 10 15 44@2x CleanShot 2024-05-25 at 10 15 58@2x

Currently to get this working I have to do something like so and it is not ideal: CleanShot 2024-05-25 at 10 20 00@2x