KristofferC / PGFPlotsX.jl

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

Variables in nested options #182

Open BeastyBlacksmith opened 4 years ago

BeastyBlacksmith commented 4 years ago

The following does not use the escaped value of c:

c = "red"
PGFPlotsX.@pgf PGFPlotsX.Plot(
       {
           mark = "*",
           mark_options = { c },
       },
       PGFPlotsX.Coordinates(1:3,2:4))

It reports

! Package pgfkeys Error: I do not know the key '/tikz/c' and I am going to igno
re it. Perhaps you misspelled it.

Is there an alternative way to do this?

tpapp commented 4 years ago

We did not expose that API yet, so currently a workaround would be some variant of

using PGFPlotsX
Plot(PGFPlotsX.dictify(["mark" => "*", c]), PGFPlotsX.Coordinates(1:3,2:4))

I can see two solutions to this:

  1. expose the API for constructing options, eg by defining a direct push! method, and a constructor from an iterable,
  2. make the @pgf macro process interpolation ($c)

We should do (1) in any case IMO, (2) would be a nice sugar in addition.

BeastyBlacksmith commented 4 years ago

that wouldn't allow me to pass colors as a RGB type though, am I right?

tpapp commented 4 years ago

no, use color = ... for that. that needs no extra tricks.

BeastyBlacksmith commented 4 years ago

I mean something like

c = RGB(1.0,0.0,0.0)
PGFPlotsX.@pgf PGFPlotsX.Plot(
       {
           mark = "*",
           "mark options" = "{ color  = $c }",
       },
       PGFPlotsX.Coordinates(1:3,2:4))

( I try to get the markers in a different color than the line )

tpapp commented 4 years ago

You don't need a string, so you don't need to interpolate:

using PGFPlotsX, Colors
c = RGB(1.0,0.0,0.0)
PGFPlotsX.@pgf PGFPlotsX.Plot(
    {
        mark = "*",
        "mark options" = { color = c },
    },
    PGFPlotsX.Coordinates(1:3,2:4))
KristofferC commented 4 years ago

We could do like BenchmarkTools and implement interpolation in the macro.

BeastyBlacksmith commented 4 years ago

I see, that would be useful to construct things like

using PGFPlotsX
opt = :mark
PGFPlotsX.@pgf { $opt = "*" }
tpapp commented 4 years ago

That would be nice.

However, thinking about this, most of the standalone options one would want to interpolate are shortcuts, eg red is color = red, for which we have convenient syntax.