4

I have a TikZ-matrix. And I want to highlight the rows
1, 3, 6, 10, 15, 21, 28,... =n*(n+1)/2

For small examples, I could do it this way:

highlight/.style={  row #1/.style={....}   },
highlight/.list={1,3,6,10,15}

But my question is: Think about a quite bigger number of rows (say 1000) - how can I automate that by using the calculation "n*(n+1)/2"?

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
highlight/.style={  row #1/.style={nodes={fill=pink}}   },
]
\matrix[matrix of nodes,
highlight/.list={1,3,6,10,15}
](m){
1 \\
2 \\
3 \\
4 \\
5 \\
6 \\
7 \\
8 \\
9 \\
10  \\
11 \\
};
\end{tikzpicture}
\end{document}
4
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\ExplSyntaxOn
\cs_new:Npn\my_calculate:n #1
  { , \int_eval:n{#1*(#1+1)/2} }

\newcommand\createlist[1]{
  \int_step_function:nnN{1}{#1}\my_calculate:n
}
\ExplSyntaxOff

\begin{document}
\begin{tikzpicture}[
  highlight/.style={row #1/.style={nodes={fill=pink}}},
]
  \matrix[matrix of nodes,
    highlight/.list/.expanded={\createlist{5}}
  ] (m) {
    1 \\
    2 \\
    3 \\
    4 \\
    5 \\
    6 \\
    7 \\
    8 \\
    9 \\
    10 \\
    11 \\
  };
\end{tikzpicture}
\end{document}
| improve this answer | |
  • Maybe a pgffor loop could be added for the matrix. – projetmbc Sep 20 at 18:19
  • @projetmbc That would be nice, but this is not trivial - as far I know. You can address it in a question "How to create a TikZ matrix with a loop", if you want. – cis Sep 20 at 19:37
  • @cis Have you try to play with this https://tex.stackexchange.com/a/22276/6880 ? – projetmbc Sep 20 at 20:05
  • @projetmbc Ah, interesting. In the case here, I needed something that I can use a listwise applied style, so the matrix was coincidence. – cis Sep 20 at 20:42

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.