Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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}
share|improve this question
    
@MarcoDaniel: Thanks. Do you want to compose an answer adapting those solutions? –  Peter Grill Mar 13 '12 at 19:59
    
Done ;-). I hope it helps. –  Marco Daniel Mar 13 '12 at 20:40
add comment

1 Answer

up vote 9 down vote accepted

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:

enter image description here

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.