4

I am trying to display a Hilbert-matrix and to color the antidiagonals by varying colors choosen from an array, so that cell (i,j) gets color \Colors[i+j-1 mod 3] (so in order that nearby cells with equal value get the same color). The coloring on the Hilbert-matrix I got does not work (yet).

I tried to follow (Changing color in foreach using arrayjob) but I can't figure out, how to compute the color in fashion that I can use it later in the draw-command.

I tried to define it by use of execute at begin cell:

\documentclass[tikz,border=2mm]{standalone}

\usepackage{tikz}\usetikzlibrary{matrix}

\begin{document}
\newcommand{\Colors}{{%
"FF0000","00FF00","0000FF",%
%"FF0000","00FF00","0000FF",
}}

%hilbert matrix
\begin{tikzpicture}[
  mygridmatrix/.style={matrix of nodes,
    nodes={rectangle,
      draw,
      execute at begin cell=\pgfmathsetmacro{\thecurrentcolor}{\Colors[\the\pgfmatrixcurrentcolumn]}\definecolor{currentcolor}{HTML}{\thecurrentcolor},
      fill=currentcolor,
      minimum width=8mm,
      minimum height=8mm,
      node contents={$\frac{1}{\the\pgfmatrixcurrentrow+\the\pgfmatrixcurrentcolumn-1}$},
    },
    nodes in empty cells,
},
]

\matrix (m) [mygridmatrix]
{
&&&\\
&&&\\
&&&\\
&&&\\
};
\end{tikzpicture}

\end{document}

but this yields only ! Package xcolor Error: Undefined color 'currentcolor'. :( Replacing currentcolor by any real color like red makes it working, but then all antidiagonals get the color (and not the ones according to \Colors-array).

Any help appreciated.

2

I don't know what the color code supposed to be but you can modify the following accordingly.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
  mycolorstyle/.code={%
    \pgfmathparse{\Colors[\the\pgfmatrixcurrentcolumn-1]}%
    \definecolor{temp}{HTML}{\pgfmathresult}%
    \tikzset{fill=temp}%
  }
}
\begin{document}
\newcommand{\Colors}{{"FF0000","00FF00","0000FF","FF0000","00FF00","0000FF"}}

%hilbert matrix
\begin{tikzpicture}[
  mygridmatrix/.style={matrix of nodes,
    nodes={rectangle,
      draw,
      mycolorstyle,
      minimum width=8mm,
      minimum height=8mm,
      node contents={$\frac{1}{\pgfmathparse{int(\the\pgfmatrixcurrentrow+\the\pgfmatrixcurrentcolumn-1)}\pgfmathresult}$},
    },
    nodes in empty cells,
},
]
\matrix (m) [mygridmatrix]
{
&&&\\
&&&\\
&&&\\
&&&\\
};
\end{tikzpicture}
\end{document}

enter image description here

Your version didn't work because at begin cell is invoked inside the node's hbox not in the creation of the node shape itself hence it's too late to change the fill spec.

  • Thanks! with your help i was able to achieve the antidiagonals by: \tikzset{ mycolorstyle/.code={% \pgfmathparse{Mod((\the\pgfmatrixcurrentrow)+(\the\pgfmatrixcurrentcolumn)-1,3)}% \pgfmathparse{\Colors[\pgfmathresult]}% \definecolor{temp}{HTML}{\pgfmathresult}% \tikzset{fill=temp}% } } – user122608 Jan 8 '17 at 13:05

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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