특정 노드 선택 및 스타일 변경(해당 노드에 대해)

특정 노드 선택 및 스타일 변경(해당 노드에 대해)

특정 노드를 선택하고 녹색으로 칠할 수 있습니까?

다른 for 루프를 사용하려고 했는데 스타일을 2개 이상 정의할 수 없는 것 같습니다.

\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}

답변1

.try다음과 같은 이름이 결합된 스타일을 만들 수 있습니다.엑스그리고와이현재 노드의 값. (핸들러가 없으면 .try가능한 모든 스타일 조합을 정의해야 합니다.)

안타깝게도 본문이 로컬에서만 실행되므로 루프 \tikzset내부에서 사용할 수 없습니다 . \foreach글로벌 PGF 키를 정의하려고 생각했지만 etoolbox솔루션이 적응하기 더 쉽다고 생각합니다.

루프 \foreach\tikzset나중에 \myTikZsets.

코드 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}

코드 2

여러분이 제시한 예를 사용하면 일반 TeX로도 만들 수 있습니다 \ifnum.

\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}

산출

여기에 이미지 설명을 입력하세요

관련 정보