在根据给定代码生成的 28 x 28 圆圈数组中,我希望能够突出显示特定的子数组并让相应的箭头指向如下方向:
这是我目前拥有的代码:
\documentclass [12pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\foreach \n in {1, 2, ..., 28} {
\foreach \d in {1, 2, ..., 28} {
\draw (.25 * \d - 2, -0.25 * \n + 2) circle (0.1cm);}}
\draw (8, -0.25 * 14 + 2) circle (0.1cm);
\end{tikzpicture}
\end{document}
答案1
另一种选择是使用蛮力,使用形状节点及其各自的名称,使用 calc 库来定位某些特定点。然后,根据喜好,我改变了箭头的连接方式,你可以to [out = angle, in = angle]
用--
你需要的直线来替换它。
梅威瑟:
\documentclass [tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc,arrows.meta}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\foreach \n in {1, 2, ..., 28} {
\foreach \d in {1, 2, ..., 28} {
\node[draw,circle,black!30,inner sep=0.75mm](X\d-Y\n) at (.25 * \d - 2, -0.25 * \n + 2){};
}
}
\draw
($(X28-Y14)!0.5!(X28-Y15)$)++(2cm,0)
node[draw,circle,black,inner sep=0.75mm](out){};
\foreach \n in {1, 2, ..., 6} {
\foreach \d in {1, 2, ..., 6} {
\draw
(X20-Y11)+(.25 * \d,-0.25 * \n)node[draw,circle,black,inner sep=0.75mm](x\d-y\n){};
\draw[-{Triangle[angle=45:.1cm 1]}]
(x\d-y\n) to [in=180, out=0] (out);
}
}
\draw
($(X14-Y1)!0.5!(X15-Y1)$)++(0,0.5cm)
node[]{input neurons}
(out)++(0,0.5cm)
node[]{hidden neuron};
\end{tikzpicture}
\end{document}
附录:
您可以添加额外的计数器,以便它们可以从 0 开始,以便节点从 0 开始命名。在第一个例子中,没有从 0 开始命名的节点,因为您的计数器从 1 开始。我添加了一个额外的示例和一些命令定义注释。
梅威瑟:
\documentclass [tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,calc,arrows.meta}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[
%Environment cfg
>={Triangle[angle=45:.1cm 1]} % Arrows style by arrows.meta options
]
\foreach \n [count=\y from 0] in {1, 2, ..., 28} {
\foreach \d [count=\x from 0]in {1, 2, ..., 28} {
\node[draw,circle,black!30,inner sep=0.75mm](X\x-Y\y) at (.25 * \d - 2, -0.25 * \n + 2){};
%\node[node style options](node_name[char_variable]) at (node position[number,number]){node_text}; % Description of node definition args.
}
}
\draw
($(X27-Y14)!0.5!(X27-Y14)$)++(2cm,0)
node[draw,circle,black,inner sep=0.75mm](out){};
%\draw (starting coordinate given by calc the middle point between nodes (Xa-Yb) and (Xc-Yd))
% node[node style options](node_name){node_text};
\foreach \n [count=\y from 0] in {1, 2, ..., 6} {
\foreach \d [count=\x from 0] in {1, 2, ..., 6} {
\draw
(X19-Y10)+(.25 * \x,-0.25 * \y)node[draw,circle,black,inner sep=0.75mm](x\x-y\y){};
\draw[->]
(x\x-y\y) to [in=180, out=0] (out);
}
}
\draw
($(X14-Y0)!0.5!(X14-Y0)$)++(0,0.5cm)
node[]{input neurons}
(out)++(0,-0.5cm)
node[]{hidden neuron};
%ADDENDUM
\foreach \n [count=\y from 0] in {1, 2, ..., 8} {
\foreach \d [count=\x from 0] in {1, 2, ..., 6} {
\draw
(X0-Y0)+(.25 * \x,-0.25 * \y)node[draw=red,circle,inner sep=0.75mm](x\x-y\y){};
\draw[->,blue!50!cyan,dashed]%Added some color
(x\x-y\y) to [in=90, out=0] (out);
}
}
\draw[<->] (X6-Y8) -- (X24-Y23);
\end{tikzpicture}
\end{document}
答案2
这是一个可能的方法。选择标准cond
在
\pgfmathtruncatemacro{\itest}{(#1>20 && #1 <27 && #2 >11 && #2 <18 ? 1 :0)}
其中#1
和#2
是 x 和 y 坐标的循环变量。在这个例子中,它告诉 Ti钾Z 突出显示水平循环变量\X
介于 20 和 27 之间,垂直循环变量\Y
介于 11 和 18 之间的节点。您可以根据需要调整突出显示条件。此代码执行两项操作。它突出显示相应的节点,并将其添加到在第二个循环中循环的连接节点列表中\foreach
。
\documentclass [12pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[font=\sffamily,
cond/.code 2 args={%
\pgfmathtruncatemacro{\itest}{(#1>20 && #1 <27 && #2 >11 && #2 <18 ? 1 :0)}
\ifnum\itest=1
\ifcsname lstMyNodes\endcsname
\xdef\lstMyNodes{\lstMyNodes,c-#1-#2}
\else
\xdef\lstMyNodes{c-#1-#2}
\fi
\tikzset{thick}
\fi},circ/.style={circle,draw,inner sep=0pt,minimum size=2mm}]
%
\path[local bounding box=input] foreach \Y in {1, 2, ..., 28} {
foreach \X in {1, 2, ..., 28} {
(.25 * \X - 2, -0.25 * \Y + 2) node[circ,cond={\X}{\Y}](c-\X-\Y){}}};
\path (8, -0.25 * 14 + 2)node[circ,label=above:hidden neuron](c-0-0){}
(input.north) node[above]{input neurons};
\path[latex-] foreach \X in \lstMyNodes
{(c-0-0) edge (\X)};
\end{tikzpicture}
\end{document}
编辑:添加了箭头。
您可以将条件更改为圆形区域。
\documentclass [12pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[font=\sffamily,
cond/.code 2 args={%
\pgfmathtruncatemacro{\itest}{(pow(#1-22.5,2)/25+pow(#2-14.5,2)/25 < 1 ? 1 :0)}
\ifnum\itest=1
\ifcsname lstMyNodes\endcsname
\xdef\lstMyNodes{\lstMyNodes,c-#1-#2}
\else
\xdef\lstMyNodes{c-#1-#2}
\fi
\tikzset{thick}
\fi},circ/.style={circle,draw,inner sep=0pt,minimum size=2mm}]
%
\path[local bounding box=input] foreach \Y in {1, 2, ..., 28} {
foreach \X in {1, 2, ..., 28} {
(.25 * \X - 2, -0.25 * \Y + 2) node[circ,cond={\X}{\Y}](c-\X-\Y){}}};
\path (8, -0.25 * 14 + 2)node[circ,label={[yshift=1ex]above:hidden neuron}](c-0-0){}
(input.north) node[above]{input neurons};
\path[latex-] foreach \X in \lstMyNodes
{(c-0-0) edge (\X)};
\end{tikzpicture}
\end{document}