.png)
É possível selecionar nós específicos e colori-los de verde?
Eu estava pensando em usar outro loop for, mas não parece que consigo definir mais de um estilo.
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,fill=MidnightBlue!25,minimum size=2.0em}][lightstyle/.style={circle,draw,fill=Green!25,minimum size=2.0em}
\foreach \x in {0,...,4}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{5 + 5*\x - \y}
\node [darkstyle] (\x\y) at (1.5*\x,-1.5*\y) {\label};}
\foreach \x in {0,...,4}
\foreach \y [count=\yi] in {0,...,3}
\draw (\x\y)--(\x\yi) (\y\x)--(\yi\x) ;
\foreach \x in {0,...,4}
\for each \y in {0, 4}
\node [lightstyle]
\end{tikzpicture}
\end{document}
Responder1
Você pode criar .try
um estilo que tenha um nome combinado doxe asimvalor do nó atual. (Sem o .try
manipulador, você precisaria definir todas as combinações de estilos possíveis.)
Infelizmente, \tikzset
não pode ser usado dentro de um \foreach
loop, pois executa seu corpo apenas localmente. Pensei em definir chaves globais de PGF mas acho que a etoolbox
solução é mais fácil de adaptar.
Os \foreach
loops “coletam” tudo \tikzset
o que executamos posteriormente \myTikZsets
.
Código 1
\RequirePackage[dvipsnames]{xcolor}
\documentclass[tikz]{standalone}
\usepackage{etoolbox}
\begin{document}
\begin{tikzpicture}[
every node/.style={circle,draw,minimum size=2.0em},
darkstyle/.style={fill=MidnightBlue!25},
lightstyle/.style={fill=Green!25},
redstyle/.style={fill=red!25},
style for 1-3/.style={redstyle},
style for 3-3/.style={redstyle},
]
\foreach \x in {0,...,4} {
\foreach \y in {0,4} {
\xappto\myTikZsets{\noexpand\tikzset{style for \x-\y/.style=lightstyle}}
}
}
\myTikZsets % use the stored \tikzset calls
\renewcommand*{\myTikZsets}{}% and empty it again (for later use)
\foreach \x in {0,...,4}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{5 + 5*\x - \y}
\node [darkstyle, style for \x-\y/.try] (n-\x-\y) at (1.5*\x,-1.5*\y) {\label};}
\foreach \x in {0,...,4}
\foreach \y [count=\yi] in {0,...,3}
\draw (n-\x-\y)--(n-\x-\yi) (n-\y-\x)--(n-\yi-\x) ;
\end{tikzpicture}
\end{document}
Código 2
Com o exemplo que você deu, isso também pode ser feito com TeX \ifnum
s simples.
\RequirePackage[dvipsnames]{xcolor}
\documentclass[tikz,convert=false]{standalone}
\begin{document}
\begin{tikzpicture}[
every node/.style={circle,draw,minimum size=2.0em},
darkstyle/.style={fill=MidnightBlue!25},
lightstyle/.style={fill=Green!25},
]
\foreach \x in {0,...,4}
\foreach \y in {0,...,4}
{\pgfmathtruncatemacro{\label}{5 + 5*\x - \y}
\ifnum\y=0
\tikzset{darkstyle/.style={lightstyle}}
\fi
\ifnum\y=4
\tikzset{darkstyle/.style={lightstyle}}
\fi
\node [darkstyle] (n-\x-\y) at (1.5*\x,-1.5*\y) {\label};}
\foreach \x in {0,...,4}
\foreach \y [count=\yi] in {0,...,3}
\draw (n-\x-\y)--(n-\x-\yi) (n-\y-\x)--(n-\yi-\x) ;
\end{tikzpicture}
\end{document}