Selecionando nós específicos e alterando o estilo (para esses nós)

Selecionando nós específicos e alterando o estilo (para esses nós)

É 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 .tryum estilo que tenha um nome combinado doxe asimvalor do nó atual. (Sem o .trymanipulador, você precisaria definir todas as combinações de estilos possíveis.)

Infelizmente, \tikzsetnão pode ser usado dentro de um \foreachloop, pois executa seu corpo apenas localmente. Pensei em definir chaves globais de PGF mas acho que a etoolboxsolução é mais fácil de adaptar.

Os \foreachloops “coletam” tudo \tikzseto 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 \ifnums 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}

Saída

insira a descrição da imagem aqui

informação relacionada