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'm trying to programmatically set the style of the columns of a TikZ matrix. My attempt is the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}

\tikzstyle{mymatrix}=[draw]  
\foreach \c [count=\i] in {red, green, blue} {
    \globaldefs=1        % make tikz style global
    \tikzset{mymatrix/.append style={column \i/.style={nodes={fill=\c}}}}
}

\matrix (m) [matrix of nodes, mymatrix] {
 1 & 2 & 3 & 4 \\
 5 & 6 & 8 & 8 \\ };

\end{tikzpicture}

\end{document}

Unfortunately, this code does not compile because the commands \i and \c are undefined. My guess is that the style inside the for-loop has to be expanded inside the for-loop, but I don't know how to do this. Any ideas?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

You can expand the variables by using an \edef to define a new command:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}

\tikzstyle{mymatrix}=[draw]  
\foreach \c [count=\i] in {red, green, blue} {
    \globaldefs=1 
    \edef\dotikzset{
        \noexpand\tikzset{
            mymatrix/.append style={
                column \i/.style={
                    nodes={fill=\c}
                }
            }
        }
    }
    \dotikzset
}

\matrix (m) [matrix of nodes, mymatrix] {
 1 & 2 & 3 & 4 \\
 5 & 6 & 8 & 8 \\ };

\end{tikzpicture}

\end{document}
share|improve this answer
    
Yes, works great! Thanks! –  Yori May 12 '12 at 17:50

Jake was faster but here is something I learned from egreg in this question :

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{matrix}

\let\mystyle\empty
\newcommand{\populatestyle}{%
  \foreach \c [count=\i] in {red, green, blue} {
    \begingroup\edef\x{\endgroup
       \noexpand\gappto\noexpand\mystyle{column \i/.style={nodes={fill=\c}},}}\x
    }%
}
\populatestyle

\tikzset{mymatrix/.estyle={\mystyle}}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,mymatrix] {
 1 & 2 & 3 & 4 \\
 5 & 6 & 7 & 8 \\ };
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Interesting solution, I will keep this approach in mind, it might come in handy. Thanks! –  Yori May 12 '12 at 17:52
    
Why does the \endgroup command have to be inside the definition of \x? –  Yori May 12 '12 at 18:41
    
@Yori It's better to remove it anyway :) \edef\x{\noexpand\gappto\noexpand\mystyle{column \i/.style={nodes={fill=\c}},}}\x should work too. –  percusse May 12 '12 at 19:31
    
Yes, I noticed; I just used your idea in a different part of my code. Works like a charm. –  Yori May 12 '12 at 19:33

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.