特定のノードを選択し、スタイルを変更する(それらのノードに対して)

特定のノードを選択し、スタイルを変更する(それらのノードに対して)

特定のノードを選択して緑色にすることは可能ですか?

別の for ループを使用することを考えていましたが、複数のスタイルを定義することはできないようです。

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

出力

ここに画像の説明を入力してください

関連情報