tikz - 在foreach循环中发出填充节点的问题

tikz - 在foreach循环中发出填充节点的问题

我有以下 tikz 图片:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]

\foreach \m/\l [count=\y] in {1,2,missing,3}
  \node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};

\foreach \m [count=\y] in {1,missing,2}
  \node [every neuron/.try, neuron \m/.try] (hidden-\m) at (2,2.2-\y*1.1) {};

\foreach \m [count=\y] in {1}
  \node [every neuron/.try, neuron \m/.try, fill=black] (output-\m) at (4,1.1-\y) {};

\end{tikzpicture}

\end{document}

图片 2

但是,我想用颜色(例如黑色)填充第二列节点,同时仍保留结构 foreach。我尝试按如下方式操作:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]

\foreach \m/\l [count=\y] in {1,2,missing,3}
  \node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};

\foreach \m [count=\y] in {1,missing,2}
  \node [every neuron/.try, neuron \m/.try, fill=black] (hidden-\m) at (2,2.2-\y*1.1) {};

\foreach \m [count=\y] in {1}
  \node [every neuron/.try, neuron \m/.try, fill=black] (output-\m) at (4,1.1-\y) {};

\end{tikzpicture}

\end{document}

但是,结果如下。看来我也错误地填充了缺失的点(我不想这样做,我只想填充节点)。任何帮助都值得感激。

图片 2

答案1

如果你从如何更改用 Tikz 绘制的节点之间的间距?(至少neuron styleneuron missing也在那里声明)您可以强制neuron missing包含一个fill=none选项并在第二个 foreach 上应用fill=black,但在neuron missing应用样式之前。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{%
  every neuron/.style={
    circle,
    draw,
    minimum size=1mm
  },
  neuron missing/.style={
    draw=none, 
    scale=5,
    text height=0.333cm,
    execute at begin node=\color{black}$\vdots$,
    fill=none
  },
}

\begin{document}

\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]

\foreach \m/\l [count=\y] in {1,2,missing,3}
  \node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};

\foreach \m [count=\y] in {1,missing,2}
  \node [fill=black, every neuron/.try, neuron \m/.try] (hidden-\m) at (2,2.2-\y*1.1) {};

\foreach \m [count=\y] in {1}
  \node [every neuron/.try, neuron \m/.try, fill=black] (output-\m) at (4,1.1-\y) {};

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容