Seleccionar nodos específicos y cambiar el estilo (para esos nodos)

Seleccionar nodos específicos y cambiar el estilo (para esos nodos)

¿Es posible seleccionar nodos específicos y colorearlos de verde?

Estaba pensando en usar otro bucle for, pero no parece que pueda definir más de un 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}

Respuesta1

Puedes crear .tryun estilo que tenga un nombre combinado delXy elyvalor del nodo actual. (Sin el .trycontrolador necesitarías definir todas las combinaciones de estilos posibles).

Lamentablemente, \tikzsetno se puede usar dentro de un \foreachbucle ya que ejecuta su cuerpo solo localmente. Pensé en definir claves PGF globales pero creo que la etoolboxsolución es más fácil de adaptar.

Los \foreachbucles “recopilan” todo \tikzsetlo que luego ejecutamos con \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

Con el ejemplo que has dado, esto también se puede hacer con 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}

Producción

ingrese la descripción de la imagen aquí

información relacionada