
Tengo una matriz TikZ. Y quiero resaltar las filas.
1, 3, 6, 10, 15, 21, 28,... =n*(n+1)/2
Para pequeños ejemplos, podría hacerlo de esta manera:
highlight/.style={ row #1/.style={....} },
highlight/.list={1,3,6,10,15}
Pero mi pregunta es: piense en un número bastante mayor de filas (digamos 1000).¿Cómo puedo automatizar eso usando el cálculo " n*(n+1)/2
"?
\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}
Respuesta1
\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}
Respuesta2
Dado que son solo cálculos de números enteros que, con suerte, no superan 2³¹−1, puedes usar \int_eval:n
, \inteval
(que es una versión frontal de int_eval) o \pgfinteval
(que es un clon del anterior) dentro de la especificación clave:
highlight/.style={
row \inteval{#1*(#1+1)/2}/.append style={nodes={fill=pink}}}
Si no desea modificar la highlight
clave, puede utilizar un reenviador, por supuesto:
highlight/.style={row #1/.append style={nodes={fill=pink}}},
highlight'/.style={highlight=\pgfinteval{#1*(#1+1)/2}}
También sería posible crear algo como
highlight/.list wrap={1, ..., 1000}{\inteval{#1*(#1+1)/2}}`
si la sugerencia anterior no es lo suficientemente agradable.
Código
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\tikzset{
create column/.style={
matrix content/.initial=,
/utils/temp/.style={
matrix content/.append={##1\pgfmatrixendrow}},
/utils/temp/.list={#1},
node contents=\pgfkeysvalueof{/tikz/matrix content}}}
\begin{document}
\tikz[
highlight/.style={
row \inteval{#1*(#1+1)/2}/.append style={nodes={fill=pink}}}]
\matrix (m) [
matrix of nodes,
highlight/.list={1, ..., 1000},
create column={1, ..., 1000}];
\tikz[
highlight/.style={row #1/.append style={nodes={fill=pink}}},
highlight'/.style={highlight=\pgfinteval{#1*(#1+1)/2}}]
\matrix (m) [
matrix of nodes,
highlight'/.list={1, ..., 1000},
create column={1, ..., 1000}];
\end{document}