1

You can use \tikzstyle{mystyle}=[some options]; to define a custom mystyle globally, that means it affects all tikz graphics in the document, but is there also something similar if you want to define a custom mystyle just within one tikzpicture?

flawr
  • 1,123
  • 1
  • 9
  • 19
  • You should use \tikzset (mystyle/.style={}) nowadays. That will also work locally. – TeXnician Dec 31 '17 at 15:57
  • @TeXnician I didn't know about tikzset, is this the preferred way now? – flawr Dec 31 '17 at 16:00
  • I don't think the linked question is really a duplicate (and maybe this questions get answered somewhere in that lengthy post). The truth is: \tikzstyle, \tikzset and […] all work locally, just use them in a group/environment/scope and not in the preamble. – Qrrbrbirlbel Oct 23 '22 at 23:33
  • And I also don't think that the answers here really help. \tikzset or \tikzstyle doesn't matter. In both answers mystyle is still defined globally (and overwritten locally). But just because it is defined, doesn't mean it has an effect on the pictures until it used there somewhere. (Some styles have obviously, like every picture, every node, every edge, …) – Qrrbrbirlbel Oct 23 '22 at 23:39

2 Answers2

6

Instead of groups and \tikzset, add the style definition to the optional argument of the tikzpicture environment.

\documentclass{article}
\usepackage{tikz}
\tikzset{mystyle/.style={fill=blue}}

\begin{document}
    \begin{tikzpicture}
        \node[mystyle] {Test};
    \end{tikzpicture}

    \begin{tikzpicture}[mystyle/.style={fill=yellow}]
        \node[mystyle] {Test};
    \end{tikzpicture}

    \begin{tikzpicture}
        \node[mystyle] {Test};
    \end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688
2

Use \tikzset instead of the \tikzstyle approach. This will allow local key-value settings.

\documentclass{article}
\usepackage{tikz}
\tikzset{mystyle/.style={fill=blue}}

\begin{document}
    \begin{tikzpicture}
        \node[mystyle] {Test};
    \end{tikzpicture}
    \begingroup
    \tikzset{mystyle/.style={fill=yellow}}
    \begin{tikzpicture}
        \node[mystyle] {Test};
    \end{tikzpicture}
    \endgroup
    \begin{tikzpicture}
        \node[mystyle] {Test};
    \end{tikzpicture}
\end{document}
TeXnician
  • 33,589