korsbo / Latexify.jl

Convert julia objects to LaTeX equations, arrays or other environments.
MIT License
544 stars 55 forks source link

Support expressions like `latexify(Expr(:call, sin, 3))`? #251

Closed hyrodium closed 1 year ago

hyrodium commented 1 year ago

Here's an example:

julia> using Latexify

julia> ex1 = Expr(:call, :sin, 3)  # same as :(sin(3))
:(sin(3))

julia> ex2 = Expr(:call, sin, 3)  # not same as :((sin)(3))
:((sin)(3))

julia> eval(ex1)  # usual evaluation
0.1411200080598672

julia> eval(ex2)  # but this also works
0.1411200080598672

julia> latexify(ex1)
L"$\sin\left( 3 \right)$"

julia> latexify(ex2)  # error
ERROR: AssertionError: latexify does not support objects of type typeof(sin).
Stacktrace:
  [1] _latexraw(args::Function; kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:convert_unicode, :env), Tuple{Bool, Symbol}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexraw.jl:109
  [2] process_latexify(args::Function; kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:convert_unicode, :env), Tuple{Bool, Symbol}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexify_function.jl:49
  [3] #latexraw#40
    @ ~/.julia/packages/Latexify/wTEwj/src/latexraw.jl:58 [inlined]
  [4] (::Latexify.var"#63#66"{Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:convert_unicode, :env), Tuple{Bool, Symbol}}}})(i::Function)
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexoperation.jl:19
  [5] iterate
    @ ./generator.jl:47 [inlined]
  [6] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, Latexify.var"#63#66"{Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:convert_unicode, :env), Tuple{Bool, Symbol}}}}}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
    @ Base ./array.jl:807
  [7] collect_similar
    @ ./array.jl:716 [inlined]
  [8] map
    @ ./abstractarray.jl:2933 [inlined]
  [9] latexoperation(ex::Expr, prevOp::Vector{Symbol}; kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:convert_unicode, :env), Tuple{Bool, Symbol}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexoperation.jl:19
 [10] (::Latexify.var"#recurseexp!#44"{Bool, Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:env,), Tuple{Symbol}}}})(ex::Expr)
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexraw.jl:98
 [11] _latexraw(inputex::Expr; convert_unicode::Bool, kwargs::Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:env,), Tuple{Symbol}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexraw.jl:102
 [12] process_latexify(args::Expr; kwargs::Base.Pairs{Symbol, Symbol, Tuple{Symbol}, NamedTuple{(:env,), Tuple{Symbol}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexify_function.jl:49
 [13] _latexinline(x::Expr; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexinline.jl:4
 [14] _latexinline(x::Expr)
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexinline.jl:3
 [15] process_latexify(args::Expr; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexify_function.jl:49
 [16] process_latexify
    @ ~/.julia/packages/Latexify/wTEwj/src/latexify_function.jl:40 [inlined]
 [17] latexify(args::Expr; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexify_function.jl:27
 [18] latexify(args::Expr)
    @ Latexify ~/.julia/packages/Latexify/wTEwj/src/latexify_function.jl:25
 [19] top-level scope
    @ REPL[22]:1

I noticed this behavior when trying an interpolation for expression.

julia> f = sin
sin (generic function with 14 methods)

julia> ex = :($f(3))  # same as ex2 = Expr(:call, sin, 3)
:((sin)(3))