KristofferC / PGFPlotsX.jl

Plots in Julia using the PGFPlots LaTeX package
Other
301 stars 40 forks source link

Vector syntax not working within options? #284

Open kimauth opened 2 years ago

kimauth commented 2 years ago

Does the vector syntax not work within a set of options or am I just missing how to do this right?

This is what I would like to obtain:

using PGFPlotsX

node_options = @pgf {circle, # and some other options I want to set
                    pin=raw"[pin edge = {out=-70,in=110,semithick,}, color={rgb,1:red,0.0052;green,0.0982;blue,0.3498}] below right:{my_text}"}

# prints as
[circle, pin={[pin edge = {out=-70,in=110,semithick,}, color={rgb,1:red,0.0052;green,0.0982;blue,0.3498}] below right:{my_text}}]

But, within the options of pin I don't want to hardcode the color. So I can do:

using ColorSchemes
blue = ColorSchemes.batlow[0.0]
pin_options = @pgf {pin_edge = {out=-70,in=110, semithick,}, color = blue}
pin = [pin_options, raw"below right:{my_text}"]

and so far, print_tex(pin) does what I want to get:

[pin edge={out={-70}, in={110}, semithick}, color={rgb,1:red,0.0052;green,0.0982;blue,0.3498}] below right:{my_text}

However, using pin within another set of options prints Julia types instead

node_options = @pgf {circle, pin = pin}

# prints as
[circle, pin={PGFPlotsX.Options(OrderedCollections.OrderedDict{Any, Any}("pin_edge" => PGFPlotsX.Options(OrderedCollections.OrderedDict{Any, Any}("out" => -70, "in" => 110, "semithick" => nothing), true), "color" => RGB{Float64}(0.005193,0.098238,0.349842)), true),below right:{my_text}}]
KristofferC commented 2 years ago

It is possible https://github.com/KristofferC/PGFPlotsX.jl/blob/7b41de7160f3a1516e3cefb7eee419a9a55845d1/src/options.jl#L229 should be changed to also call print_tex on the vs.

I just tried something like

@eval PGFPlotsX begin
    print_opt(io::IO, v::AbstractVector) = print(io, join((print_tex(String, x) for x in v), ","))
end

and I get

julia> node_options
[circle, pin={[pin edge={out={-70}, in={110}, semithick}, color={rgb,1:red,0.0052;green,0.0982;blue,0.3498}] ,below right:{my_text}
}]

I haven't thought anything deep about this though.

kimauth commented 2 years ago

It's almost good, but it prints a comma behind the square brackets which tikz doesn't understand. I guess this is sort of special syntax though, as it's options for tikz and not for pgf.

For the sake of completeness, here is something that actually shows a picture (and does work with the first node_options, but not if there is the additional comma):

axis_opts = @pgf {width = "8cm", "axis equal", xmin= -2., xmax=10, ymin = -2., ymax=9., axis_lines="none",}
Axis(axis_opts, [raw"\node", node_options, " at ", Coordinate(0.0, 0.0), "{};"],)
tpapp commented 2 years ago

Note that this is TikZ code, not pgfplots. In the manual we suggest that \node commands are just constructed as strings and passed as is.

I sympathize with

don't want to hardcode the color

but I would recommend constructing the string from something like

col = colorant"firebrick3"
sprint(PGFPlotsX.print_opt, col)

which is currently not officially part of the API but should remain supported.

Generally, quite a few building blocks of the package are useful outside pgfplots, and maybe we should make an effort to clean up and export them to emit LaTeX code for general purposes.

tpapp commented 2 years ago

Maybe we should just expose (but not necessarily export) print_opt and print_options as API, and mention them in the manual. They are probably the best solution for composing raw LaTeX directly with the translations this package implements.