I want to switch my comment to an answer.
First of all the command tikzset
based on the package pgfkeys
. To show the definition of the command you can use the following code:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\expandafter\detokenize\expandafter{\tikzset}
\end{document}
The result of this small example is:
\pgfqkeys{/tikz}
Based on this information you can use a similar construct as shown in the question: Is there a way to set a global key value using pgfkeys
?
The trick based on the command \globaldefs
. In the TeX Book at page 286 you can find the following hint:
Incidentally, if the \globaldefs
parameter is positive at the time of
the assignment, a prefix of \global
is automatically implied; but if
\globaldefs
is negative at the time of the assignment, a prefix of
\global
is ignored. If \globaldefs
is zero (which it usually is), the
appearance or nonappearance of \global
determines whether or not a
global assignment is made.
In short:
default: \globaldefs=0
=0
The assignment is global if the prefix \global
is used.
<0
All assignments are local and \global
will be ignored.
>0
All assignments are global.
Based on the previous information you can create your own command:
\newcommand\gtikzset[1]{%
\begingroup%
\globaldefs=1\relax%
\pgfqkeys{/tikz}{#1}%
\endgroup%
}
The extra grouping is necessary to hold the definition of \globaldefs
local.
Here the complete example:
\documentclass{article}
\usepackage{tikz}
\tikzset{MyStyle/.style={}}%
\newcommand*{\MyTikzPicture}{%
\begin{tikzpicture}
\draw [MyStyle] (0,0) -- (2,0);
\end{tikzpicture}
}%
\newcommand\gtikzset[1]{%
\begingroup%
\globaldefs=1\relax%
\pgfqkeys{/tikz}{#1}%
\endgroup%
}
\begin{document}
\MyTikzPicture
\tikzset{MyStyle/.style={ultra thick, blue}}%
\MyTikzPicture
\begin{tabular}{r l}
a & b \gtikzset{MyStyle/.style={ultra thick, red}}
\end{tabular}
\MyTikzPicture
\end{document}
After this you get a last red line:
