# How to globally tikzset styles

I would like to set a tikzstyle from within a tabular environment, and use it after the table. Adding the usual \global seems to have no effect. The MWE below produces:

but I want the last line to be red not blue.

\documentclass{article}

\usepackage{tikz}

\tikzset{MyStyle/.style={}}%

\newcommand*{\MyTikzPicture}{%
\begin{tikzpicture}
\draw [MyStyle] (0,0) -- (2,0);
\end{tikzpicture}
}%

\begin{document}
\MyTikzPicture

\tikzset{MyStyle/.style={ultra thick, blue}}%
\MyTikzPicture

\begin{tabular}{r l}
a & b  \global\tikzset{MyStyle/.style={ultra thick, red}}
\end{tabular}

\MyTikzPicture
\end{document}


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:

• Yep, works. But don't think the \begingroup, \endgroup is necessary as I believe that there is already an implied grouping. – Peter Grill Mar 13 '12 at 21:36
• @PeterGrill: Have you seen the example that demostrate the necessary group. – Marco Daniel Mar 14 '12 at 9:02
• @MarcoDaniel Why does \gtikzset have to use \pgfqkeys and not directly \tikzset? Is it important to check what \tikzset is doing while not checking what \pgfqkeys is?? – Robert Siemer Sep 3 at 12:37