Grand constructor unification
This addresses #11, #16, #37, and continues #43.
Objectives
-
Consistent constructors of the form Type([options], ...)
where ...
include various convenience forms. Easier to remember, more consistent with the LaTeX code of pgfplots
.
-
Optionally, shorten some struct/constructor names. The expectation is that the user uses pgf.
for the module. This is an optional part of the RFC, and the refactoring can be done without it.
-
Make constructors validate whatever possible (eg dimension consistency etc), to fail early.
Remark: in order to make this writeup more compact, Vector
and Matrix
stand-in for their abstract counterparts below. The functions should always accept everything.
Specifics
Whenever we have options
, it becomes the first field of the struct
.
TikzDocument
Rename to Document
. Constructor is TikzDocument([elements]; preamble)
.
TikzDocument(elements::Vector; preamble)
is deprecated.
TikzDocument(element)
and TikzDocument(; preamble)
survive as special cases.
TikzPicture
Rename to Picture
. The constructor is TikzPicture([options], elements...)
.
TikzPicture(elements::Vector, options)
, TikzPicture(options, elements)
, and TikzPicture(elements::Vector)
are deprecated. TikzPicture(options)
remains as a special case.
Axis
The constructor is Axis([options], plots...)
.
Axis(plots::Vector, options)
, Axis(plot, options...)
are deprecated.
Very similarly: PolarAxis
, also introduce SemiLogXAxis
, SemiLogYAxis
, LogLogAxis
.
Plot, Plot3
Introduce Plot([incremental], [options], elements...)
.
Plot(elements::Vector, options)
, Plot(elements:Vector, options...)
, and forms with label
are removed.
legend
was used for \addlegendentry
. But this can be specified alternatively:
- with a
Legend
type which would emit \legend
(does not exist currently)
- with a
LegendEntry
type which would emit \addlegendentry
(similarly, does not currently exist)
- with the
legend entries
option for Axis
.
Furthermore, Section 4.9.4 of the manual says that
It does not matter where \addlegendentry
commands are placed, only the sequence matters.
so there is no strong reason to have it in Plot
.
Very similarly for Plot3
.
Coordinates
Coordinates(coords::Matrix, [error::Matrix], [meta::Vector])
is the single inner constructor. It checks that coords
and error
have the same size (if applicable), with 2 or 3 columns, and that meta
has matching length (when provided).
Convenience constructors could include:
-
Coordinates(x::Vector, y::Vector; [xerror::Vector, yerror::Vector], [meta::Vector])
. When an error is given but another is not, the first error is filled with zeros.
-
Coordinates(x::Vector, y::Vector, z::Vector; [meta::Vector])
(I don't think 3d allows error bars, does it?)
-
Coordinates(y::Vector; [meta::Vector]
) which makes x
a vector of 1, 2, ...
(for simple time series etc).
Convenience feature: when a row of coords
is missing, an empty line emitted for LaTeX.
Table
Table(options, contents::Matrix, names::Vector)
is the default inner constructor. It verifies consistency of names
and contents
.
Table([options], ::Pair{Union{Symbol, String}, Vector}...)
is the preferred constructor for users. Keys can be defined with symbols and strings (all converted to strings). The constructor verifies that lengths match and that there are no duplicate keys.
Convenience constructors:
-
Table([options], x::Vector, [y::Vector], [z::Vector], [meta::Vector])
: the first 4 vectors are names x
, y
, z
, and meta
automatically when unnamed.
-
Existing converting convenience constructors are kept (eg for StatsBase.Histogram
, Contour
).
Convenience feature: when any element of a row is missing
, an empty line is printed in LaTeX.
Graphics
We keep Graphics([options], filename)
.
GroupPlot
The new constructor is GroupPlot([options], contents::Matrix)
.
The group size
option is inferred from the dimensions of contents
automatically and overwrites any existing option.
contents
can have the following elements:
-
a Plot
, which is emitted as is with a \nextgroupplot
-
an Axis
containing Plots
: its options are appended to \nextgroupplot
, then the plots are emitted,
-
a missing
value: just a \nextgroupplot
.
Expression
Unchanged.
Open questions
-
Should we have a Coordinates3
type separately for 3d coordinates? Does not exist in pgfplots
, but the code would be cleaner (I don't think they take error bars, and Plot3
could refuse to accept 2d Coordinates
, etc).
-
Should "key"
, :key
, "key" => value
, and :key => value
be accepted as a stand-in for options
, converting automatically? Advantage: shorter code when using a single option, though using @pgf
is not much more verbose. Disadvantage: the constructor for Table
would need to disentangle this case, which may be difficult. I would prefer not to do this.
-
Should we have a Legend
and LegendEntry
?
Implementation roadmap
-
Replace type and constructors one by one, to make review easier.
a. each one gets documented,
b. unit tests with 100% coverage for new forms,
c. deprecations are introduced for older forms.
-
Documentation, including examples, are rewritten extensively.
-
Release is tagged.
-
Deprecated forms removed in next release.