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?
Asked
Active
Viewed 2,528 times
1
Jasper Habicht
- 48,848
flawr
- 1,123
- 1
- 9
- 19
2 Answers
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
\tikzset(mystyle/.style={}) nowadays. That will also work locally. – TeXnician Dec 31 '17 at 15:57tikzset, is this the preferred way now? – flawr Dec 31 '17 at 16:00\tikzstyle,\tikzsetand[…]all work locally, just use them in a group/environment/scope and not in the preamble. – Qrrbrbirlbel Oct 23 '22 at 23:33\tikzsetor\tikzstyledoesn't matter. In both answersmystyleis 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, likeevery picture,every node,every edge, …) – Qrrbrbirlbel Oct 23 '22 at 23:39