Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SymPy plugin #186

Closed
wants to merge 2 commits into from
Closed

add SymPy plugin #186

wants to merge 2 commits into from

Conversation

ranocha
Copy link
Contributor

@ranocha ranocha commented Oct 7, 2021

I hope this follows the recommended style. When it works, it would be really great to get a new release of Latexify.jl.

@gustaphe
Copy link
Collaborator

gustaphe commented Oct 8, 2021

Latexify is not intended as a collection of plugins for different types, the ones that are here are holdovers from before the recipe system was introduced.

Depending on if the SymPy devs want to support this or not, I would suggest either making a PR pulling in Latexify and creating a recipe in SymPy, or creating a SymPyLatexify glue package.

(I'm not a maintainer, just someone who has done this before)

@ranocha
Copy link
Contributor Author

ranocha commented Oct 8, 2021

Hmm, I see. Could you please comment on #185 in that case? The problem is that people might want to avoid pulling in all dependencies of Latexify.jl and the official recipe system using a macro doesn't work with Requires.jl.

@ranocha
Copy link
Contributor Author

ranocha commented Oct 8, 2021

And I don't think glue packages are a good solution. If I want to use two packages together, I don't want to depend on a third one.

@ChrisRackauckas
Copy link
Contributor

SymPy just needs to add the recipe to their package and then everything is 👍

@gustaphe
Copy link
Collaborator

gustaphe commented Oct 8, 2021

I was just working on a comment, but it took a while because I can't figure out Requires.
As the author of UnitfulLatexify I know your pain. If/when JuliaLang/Pkg.jl#1285 is solved I'm celebrating. OTOH, pulling in a package isn't that hard or expensive in julia, I've learned to not worry too much about it.

@ranocha
Copy link
Contributor Author

ranocha commented Oct 10, 2021

Okay, let's say I prepare a PR to SymPy.jl using @latexrecipe. How should this look like?

Trying to mimic the SymEngine plugin by converting a SymPy.Sym to a String or an Expr doesn't work:

julia> using SymPy, Latexify

julia> α = SymPy.symbols("α")
α

julia> @latexrecipe function _(x::Sym)
           string(x)
       end

julia> latexify(- 3/(8*α) + 1/(8*α^2))
ERROR: MethodError: no method matching get(::Char, ::Symbol, ::Symbol)
Closest candidates are:
  get(::PyCall.PyDict{K, V}, ::Any, ::Any) where {K, V} at /home/hendrik/.julia/packages/PyCall/BD546/src/conversions.jl:496
  get(::IOContext, ::Any, ::Any) at show.jl:343
  get(::NamedTuple, ::Union{Integer, Symbol}, ::Any) at namedtuple.jl:309
  ...
Stacktrace:
 [1] process_latexify(args::Sym; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Latexify ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:21
 [2] process_latexify(args::Sym)
   @ Latexify ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:18
 [3] latexify(args::Sym; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Latexify ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:3
 [4] latexify(args::Sym)
   @ Latexify ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:2
 [5] top-level scope
   @ REPL[4]:1

julia> @latexrecipe function _(x::Sym)
           Meta.parse(string(x))
       end

julia> latexify(- 3/(8*α) + 1/(8*α^2))
ERROR: MethodError: no method matching iterate(::Expr)
Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen}) at range.jl:821
  iterate(::Union{LinRange, StepRangeLen}, ::Integer) at range.jl:821
  iterate(::T) where T<:Union{Base.KeySet{<:Any, <:Dict}, Base.ValueIterator{<:Dict}} at dict.jl:695
  ...
Stacktrace:
 [1] indexed_iterate(I::Expr, i::Int64)
   @ Base ./tuple.jl:92
 [2] process_latexify(args::Sym; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Latexify ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:18
 [3] process_latexify
   @ ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:18 [inlined]
 [4] latexify(args::Sym; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Latexify ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:3
 [5] latexify(args::Sym)
   @ Latexify ~/.julia/packages/Latexify/afimT/src/latexify_function.jl:2
 [6] top-level scope
   @ REPL[6]:1

Do I really have to use some internal knowledge about undocumented implementation details of @latexrecipe to fix it using something like

julia> @latexrecipe function _(x::Sym)
           (string(x), ), kwargs
       end

julia> latexify(- 3/(8*α) + 1/(8*α^2))
L"$\frac{-3}{8 \cdot \alpha} + \frac{1}{8 \cdot \alpha^{2}}$"

Or are there other suggestions?

@ranocha
Copy link
Contributor Author

ranocha commented Oct 11, 2021

Nevermind, I fixed this specific problem in #188.

@ranocha
Copy link
Contributor Author

ranocha commented Oct 11, 2021

I converted this PR into a draft. As requested above, I converted the compatibility code for Latexify and SymPy into a PR to SymPy.jl. Additionally, I created a new PR #187 with a bug fix I developed while working on this PR. When these two PRs get merged, this PR can be closed.

@ranocha
Copy link
Contributor Author

ranocha commented Oct 14, 2021

Closed in favor of #187 and JuliaPy/SymPy.jl#439

@ranocha ranocha closed this Oct 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants