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

Cannot forward keyword arguments through recipe #320

Open
hersle opened this issue Nov 6, 2024 · 2 comments
Open

Cannot forward keyword arguments through recipe #320

hersle opened this issue Nov 6, 2024 · 2 comments

Comments

@hersle
Copy link
Contributor

hersle commented Nov 6, 2024

Here is a working recipe for letting the user customize the font style of a rendered variable:

using Latexify

struct Variable
    name::String
end

@latexrecipe function f(var::Variable; font=nothing)
    if !isnothing(font)
        return Symbol("\\math", font, "{", var.name, "}")
    else
        return Symbol(var.name)
    end
end

As expected:

var = Variable("radius")
latexify(var) # outputs L"$radius$"
latexify(var; font="rm") # outputs L"$\mathrm{radius}$"

But suppose I want the recipe to call a backend function. This is useful if the recipe is complicated, or I want several recipes to share a common backend function. In anticipation of more keyword arguments, I want the Latexify recipe to forward all keyword arguments kw..., so I don't have to spell them all out in several places:

using Latexify

struct Variable
    name::String
end

function backend(var::Variable; font=nothing)
    if !isnothing(font)
        return Symbol("\\math", font, "{", var.name, "}")
    else
        return Symbol(var.name)
    end
end

@latexrecipe function f(var::Variable; kw...)
    return backend(var; kw...)
end

This code fails with

ERROR: LoadError: BoundsError: attempt to access 1-element Vector{Any} at index [2]
Stacktrace:
 [1] throw_boundserror(A::Vector{Any}, I::Tuple{Int64})
   @ Base ./essentials.jl:14
 [2] getindex
   @ ./essentials.jl:916 [inlined]
 [3] indexed_iterate(a::Vector{Any}, i::Int64, state::Int64)
   @ Base ./tuple.jl:160
 [4] create_kw_body(func_signature::Expr)
   @ Latexify ~/.julia/packages/Latexify/ieukI/src/recipes.jl:36
 [5] var"@latexrecipe"(__source__::LineNumberNode, __module__::Module, funcexpr::Any)
   @ Latexify ~/.julia/packages/Latexify/ieukI/src/recipes.jl:162
in expression starting at /home/hermasl/.julia/dev/SymBoltz/bug.jl:15

The problem seems to be that forwarding kw... is not supported by @latexrecipe. It would be very nice it this worked!

@gustaphe
Copy link
Collaborator

gustaphe commented Nov 6, 2024

It might be possible to make this work, but it might be a bit more work than it's worth. For a workaround, the generated recipe function actually has a hidden keyword variable named kwargs, so

julia> @latexrecipe function f(var::Variable)
       return backend(var; kwargs...)
       end

julia> latexify(Variable("a"); font="rm")
L"$\mathrm{a}$"

A real solution to this issue would be to allow the author of a recipe to rename kwargs, or potentially to add more keyword lists.

@hersle
Copy link
Contributor Author

hersle commented Nov 7, 2024

Thanks! I was not aware of the kwargs... hack; maybe that can do what I want.

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

No branches or pull requests

2 participants