2

I'm trying to set the style of each cell of a tikz matrix. However, I can't find information on how to access the content of the cell to pass it as an option to the style.

What I would like to do is this:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[]
\matrix[matrix of nodes]{
  |[fill=white!10!black]| 10 & |[fill=white!20!black]| 20 & |[fill=white!100!black]| 100 \\
};
\end{tikzpicture}
\end{document}

enter image description here

Ideally, I want to grab the content and use it as the condition for the fill option.

I was trying to set the node myself using execute at begin cell and execute at end cell but the content is not available as a parameter yet (#1). I also tried to naively store the contents on a macro at the execute at begin cell with a \def\macro\bgroup and then close it with execute at end cell=\egroup\node[fill=white!\macro!black]{\macro}; but that didn't work either. I guess I need some way grab all the contents without using the braces.

Is there a macro or key that stores the content of the cell to access it and manipulate it?

Is there a way to achieve what I'm trying to do without having access to the content in a macro?

4
  • 1
    Consider using pgfplotstable to read in the table. That way you do have access to the content.
    – Symbol 1
    Commented Nov 9, 2021 at 0:55
  • tex.stackexchange.com/search?q=collectcell
    – js bibra
    Commented Nov 9, 2021 at 1:12
  • You can also use a matrix plot from pgfplots.
    – user255043
    Commented Nov 9, 2021 at 1:24
  • I thought about the pgfplotstable, but I'm not sure if I will have the fine grain access to the elements later on. This is just an element on the final drawing that will require some back linking to it. Do you know if I can do the same with the table?
    – adn
    Commented Nov 9, 2021 at 9:33

2 Answers 2

3

Instead of grabbing the node/cell content (which would be too late to set node options), here I use execute at begin node to insert code to node contents.

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

\begin{document}
\begin{tikzpicture}[
  my style/.style={
    fill=white!#1!black,
    execute at begin node={#1}
  }
]
  \matrix[matrix of nodes] {
    |[my style=10]|  & 
    |[my style=20]|  &
    |[my style=100]| \\
  };
\end{tikzpicture}
\end{document}

enter image description here

2

This is a tricky solution. I tried defining a style with node contents but it didn't worked. As alternative solution, a centered label can be used. But you need to fix a minimum size for the node.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
    mystyle/.style={fill=red!#1!blue, label=center:#1, minimum size=8mm}}
    
\begin{document}
\begin{tikzpicture}[]
\matrix[matrix of nodes]{
  |[mystyle=10]| & |[mystyle=20]| & |[mystyle=50]| \\
};
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .