[Tex/LaTex] How to use tikzstyle locally?

tikz-pgftikz-styles

You can use \tikzsytle{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?

Best Answer

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}