Is it possible to display a fraction over two lines ?
(In actual code rather than latex documentation)
Is it possible to display a fraction over two lines ?
(In actual code rather than latex documentation)
Sure:
julia> x = (123 + 567 + 789) /
(987 + 654 + 321)
0.753822629969419
I mean, similar to how \alpha gives α (similar to Latex)
Is there a package that would show an equation, say (a+b)/c, is it would display in Latex
How would you imagine such a thing to work in code?
Here you go
macro div(ex)
div_sign = findfirst(ex.args .== :-)
lhs = ex.args[1:div_sign-1]
rhs = ex.args[div_sign+1:end]
filter!(x -> !(x isa LineNumberNode), lhs)
filter!(x -> !(x isa LineNumberNode), rhs)
esc(quote( $(lhs...) / $(rhs...) ) end)
end
julia> @div begin
45*123
-
sin(3)
end
39221.93653540533
More seriously this could maybe be done in a notebook (like in Mathematica) but I don’t think it’s possible normal text files.
The .jl file would have a Latex type expression written on one line say
x = \frac{ a + b }{ c }
VS Code would display it in nice mathematical notation over a couple of lines.
Similar to a Latex editor except that a, b, c, x are Julia variables.
More generally - with its Unicode support Julia equations look much closer to actual math than any other language (that I know). I was wondering if this could be taken a step further.
Thanks
Suppose we added parser support for such \LaTeX expressions. Upon encountering code like this, what Julia functions should be called?
\lim_{n\rightarrow\infty}\frac{1}{s_n^{2+\delta}}\sum_{i=1}^n\mathbb E\left[|X_i-\mu_i|^{2+\delta}\right]
which displays like so:
Julia variables and functions could be included in the expression e.g. frac{ my_func(x) }{ 2 }
Absolute values and exponents, all have a clear interpretation at least.
An expression like Lim(n->inf)
(sorry I can’t get it to render like that) would probably be excluded. Or could be an additional way of calling an existing function. The meaning of it within the expression might depend on which math package the user has installed. Or the user could attach it to a selected function.
macro div(ex)
filter!(x -> !(x isa LineNumberNode), ex.args)
div_sign = findfirst(a-> in(:-, a.args), ex.args)
lhs = ex.args[1:div_sign-1]
rhs = ex.args[div_sign+1:end]
esc(quote( $(lhs...) / $(rhs...) ) end)
end
@div begin
45*123
- - - -
sin(3)
end
Latexify has macros that can help with this. They will run your code like normal but also simultaneously turn the expression into a LaTeXString which can be rendered in the right environment. I have had success using these in combination with either Pluto or Weave.
You can hide the code cells to show only the pretty math, and you can still use all the variables and functions as normal.
pretty cool - thanks
What you are asking for would be a feature of VSCode more than a feature of Julia. Pluto and Weave render to a webpage or a PDF so they have more freedom to display equations on multiple lines.
The closest thing I know of in VSCode are called Font Ligatures. These render some common operators as specific symbols instead of the raw text objects (-> vs →).
Doesn’t sound like Font Ligatures is the way to go.
That Pluto Weave stuff looks great.
I’d like to write code that doesn’t need an accompanying math document. If people ask how it works I could point them to the code and the equations would look similar to a PDF.
Putting models in Pluto or Weave (as opposed to .jl files) sounds cumbersome though.
I’m with you. I was searching for a solution to that very problem not too long ago. Pluto is the best for code and visualizations in the same document as long as what you want to show can be done in a single script using registered packages. When I wanted to have math visualizations in my own unregistered package, I had to turn to Weave and separate PDFs. I made this package so ] test
will build the documentation PDFs from the source code with Weave. (Weave has an option to use regular .jl files with specially-formatted comments.) Let me know if you find another way!