19

Is there a convention in TikZ for defining a new style as an extension of an existing style?

As it stands, I can do:

a/.style    = { black, very thin }

Then I could extend this style locally within, e.g., a scope:

\begin{scope}[a/.append style = { thick }]
  …
\end{scope}

or I could define an entire new style:

b/.style    = { black, thick }

but this requires replicating the shared style information across both a and b.

It seems there could be some way of specifying b as an extension of a (everywhere equivalent to "appending" b's definitions to a's definitions), akin to:

b/.style    = { extends = a, thick }

or:

b/.inherits = a,
b/.style    = { thick }

Is there a canonical answer in TikZ beyond just "use a TeX macro to define the shared contents"?

3
  • 7
    You could just do b/.style={a, thick}, or b/.style={a} and then somewhere b/.append style={thick}. .style is .inherits but you will need to use .append style then (or .prefix style if settings from a should overwrite those given in .prefix style). Commented Oct 9, 2013 at 22:07
  • 4
    I agree. There is nothing magic about styles; they are just keys that expand to other keys. To extend them, you include them in your own list of keys.
    – Ryan Reich
    Commented Oct 9, 2013 at 22:27
  • You're both right. I'm glad to accept this as an answer if you make it one. Thanks. (The consequences of a purely macro-based language still take some getting used to!)
    – jrk
    Commented Oct 10, 2013 at 17:55

1 Answer 1

16

Answering my own question for the sake of others: the comments on my question are correct.

In particular, as to the two pseudo-syntaxes for extending or inheriting from an existing style which I sketched, .style is .inherits, and just using the key a is equivalent to extends = a since a expands to all the keys defined in a/.style.

So the easy answers are simply:

a/.style = { black, very thin },
b/.style = { a, thick } % copy styles of "a", then append "thick" which overrides "very thin"

or:

b/.prefix style = a, % equivalent to putting "a" at the start of b's style definition above
% later...
b/.style = { thick }

The reference I was struggling to find when searching for this myself was the pgfkeys package documented in Sec. 63 of the current TikZ/PGF reference manual.

All credit due to @Qrrbrbirlbel and @Ryan Reich.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .