
Eu tenho uma matriz TikZ. E quero destacar as linhas
1, 3, 6, 10, 15, 21, 28,... =n*(n+1)/2
Para pequenos exemplos, eu poderia fazer assim:
highlight/.style={ row #1/.style={....} },
highlight/.list={1,3,6,10,15}
Mas minha pergunta é: pense em um número bem maior de linhas (digamos 1000) -como posso automatizar isso usando o 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}
Responder1
\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}
Responder2
Como são apenas cálculos inteiros que esperamos não ultrapassar 2³¹−1 você pode usar \int_eval:n
, \inteval
(que é uma versão front-end do int_eval) ou \pgfinteval
(que é um clone do anterior) dentro da especificação da chave:
highlight/.style={
row \inteval{#1*(#1+1)/2}/.append style={nodes={fill=pink}}}
Se você não quiser alterar a highlight
chave, você pode usar um encaminhador, é claro:
highlight/.style={row #1/.append style={nodes={fill=pink}}},
highlight'/.style={highlight=\pgfinteval{#1*(#1+1)/2}}
Também seria possível criar algo como
highlight/.list wrap={1, ..., 1000}{\inteval{#1*(#1+1)/2}}`
se a sugestão anterior não for boa o suficiente.
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}