9

I have standalone figures that I include in my document. These figures use statements like \providecommand{\HypotenuseLabel}{$h$} which allows the figure to have a default label, and which can be over written via \newcommand{\HypotenuseLabel}{$x^2+y^2$} in the main file that includes this figure to customise this diagram for that particular application.

This figure also has statements like \tikzstyle{HypotenuseLineStyle}=[red, very thick, opacity=0.5] which are used to draw this particular line. I would like to change this to something of the nature \providetikzstyle so that this style only gets defined in the figure if it is not already defined. Is there an easy way to do this?

Here is a related question but for environments.

8

AFAIK \tikzstyle was an unofficial, never documented macro. It is used in some code shown in the pdfmanual but never described. The correct official syntax is \tikzset{HypotenuseLineStyle/.style={red, very thick, opacity=0.5}}.

See section 55.4.4 Defining Styles, on page 493. I can't find any .provide style or so there, so I don't think there is one. However, it would be possible to define one:

\documentclass{article}

\usepackage{tikz}

% .style is defined as \pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}
\tikzset{/handlers/.provide style/.code={%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}%
        {\pgfkeys {\pgfkeyscurrentpath /.code=\pgfkeysalso {#1}}}%
}}

\begin{document}

\tikzset{test/.provide style={green}}% Sets the style

\tikzset{test/.provide style={red}}% Doesn't overwrites the style!

% Do we get the green light?
\tikz \fill [test] (0,0) circle (5pt);


\tikzset{test2/.style={green}}% Sets the style

\tikzset{test2/.provide style={red}}% Doesn't overwrites the style!

% Do we get the green light?
\tikz \fill [test2] (0,0) circle (5pt);

\end{document}
  • Just for the reference. \tikzstyle is used 8 times in v2.10 manual ({every node}=[...] on pages 177, 178, 178, 422, 430, 432, 441 and {every picture}+=[...] on page 199). I agree that using undocumented macro is a bit dangerous, but in this case its intent is clearly readable. I may also add that I like it for its conciseness. – przemoc Jul 8 '11 at 20:57
  • @przemoc: The issue with \tikzstyle is that it is not easily parseable and adding new handlers like code (/.code), style 2 args, etc. etc. would be difficult. Using the key=value system itself is much more flexible. Personally I find \tikzset{foo/.append style={bar}} much nicer looking than \tikzstyle{foo}+=[bar] – Martin Scharrer Jul 8 '11 at 21:02
  • re issue - it's meant (from what I can tell) only for pure styles, or with 1 argument. If you have some other stuff, then sure, \tikzset is more robust. – przemoc Jul 8 '11 at 21:12
  • 1
    @przemoc: I heard that \tikzstyle is an early macro which is deprecated in favor of the official \tikzset. – Martin Scharrer Jul 8 '11 at 21:17
  • According to the source, \tikzoption is deprecated but there's no such notice for \tikzstyle and, indeed, \tikzstyle is used extensively in the main code (59 times). – Loop Space Sep 2 '11 at 19:12

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.