14

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"?

  • 6
    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). – Qrrbrbirlbel Oct 9 '13 at 22:07
  • 2
    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 Oct 9 '13 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 Oct 10 '13 at 17:55
10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.