1

I'm programmatically generating a tikz matrix, and sometimes, when I've generated one cell, I learn something from that which is going to affect how I style a later cell. So, I want to be able to call a tikzset command that fixes the particular cell's style. Something like:

\begin{tikzpicture}
\matrix [matrix of nodes] {
    hello \tikzset{row 1 column 2/.style={red}}& world \\
};
\end{tikzpicture}

would render the word "world" in red. It doesn't and I assume that this is due to some sort of grouping level. I can get around it by issuing a global command

\begin{tikzpicture}
\matrix [matrix of nodes] {
    hello {\globaldefs=1%
    \tikzset{row 1 column 2/.style={red}}}& world \\
};
\end{tikzpicture}

This sets the style for all subsequent tikzpictures as well (see full MWE below). Of course, I can unset the style later, but this feels inelegant. Is there a better way to set the style (from within one cell to affect a different, later cell) so that it only applies to the current matrix or tikzpicture?

MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
%direct setting of the style doesn't work
\begin{tikzpicture}
\matrix [matrix of nodes] {
    hello \tikzset{row 1 column 2/.style={red}}& world \\
};
\end{tikzpicture}
%setting the global style works, but...
\begin{tikzpicture}
\matrix [matrix of nodes] {
    hello {\globaldefs=1%
    \tikzset{row 1 column 2/.style={red}}}& world \\
};
\end{tikzpicture}
%...style is not local (obviously) and remains in effect on later pictures
\begin{tikzpicture}
\matrix [matrix of nodes] {
    hello & world \\
};
\end{tikzpicture}
\end{document} 

output:

enter image description here

1

You can insert |[options list]| in any cell to decide particular options:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
%direct setting of the style doesn't work
\begin{tikzpicture}
\matrix [matrix of nodes] {
    hello &|[red]| world \\
    hello &|[green, draw, font=\bfseries]| hello\\
};
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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