我正在尝试在 TikZ 中绘制网格网络。但即使经过多次尝试,我也无法获得所需的图。这是我的 MWE:
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage{scalerel}
\usepackage{ifthen}
\usetikzlibrary{decorations.markings}
\tikzset{
mynode/.style = {circle, draw, thick, fill = white, inner sep=0pt},
type1/.style = {mynode},
type2/.style = {mynode}
}
\begin{document}
\begin{tikzpicture}[>=stealth]
\draw[step=3cm,color=white] grid (2,2);
\begin{scope}[rotate=45]
\foreach \x in {-3,...,1}
\foreach \y in {-3,...,1} {
\pgfmathsetmacro{\type}{int(mod(abs(\x+\y),2)+1)};
\pgfmathsetmacro{\pos}{int(mod(abs(\y),2)+1)};
\pgfmathsetmacro{\post}{int(mod(abs(\x),2)+1)};
\draw[blue, -] (\x-0.6, \y) -- (\x+0.6, \y);
\ifthenelse{\type=2 and \pos=2 and \post=1}{\node[black,below] at (\x-0.3, \y+0.3) {{\tiny$b$}};}{\node[black,below] at (\x-0.3, \y+0.3) {{\tiny $r$}};}
%
\draw[red, -] (\x, \y-0.6) -- (\x, \y+0.6);
\ifthenelse{\type=1 \and \pos=1}{\node[type\type] at (\x,\y) {\color{orange}$K_{\type a}$};}{}
\ifthenelse{\type=1 \and \pos=2}{\node[type\type] at (\x,\y) {\color{orange}$K_{\type b}$};}{}
\ifthenelse{\type=2 \and \pos=1}{\node[type\type] at (\x,\y) {\color{black}$K_{\type a}$};}{}
\ifthenelse{\type=2 \and \pos=2}{\node[type\type] at (\x,\y) {\color{black}$K_{\type b}$};}{}
}
\end{scope}
\end{tikzpicture}
\end{document}
网络背后的逻辑:
- 您可以认为沿着左侧
$ K_{1a} $
分裂为左分支,右侧分裂为 。$K_{2b}$
$r$
$K_{2a}$
- 类似地,沿左侧
$ K_{1b} $
分成 ,左分支为,右侧分为 。$K_{2a}$
$b$
$K_{2b}$
仅左箭头带有标签$r$
和$b$
。
- 所有带标签的圆形框
$a$
ie$ K_{1a} $
和$K_{2a}$
始终有左分支$r$
, - 同样,标签
$b$
ie$ K_{1b} $
和$K_{2b}$
have left branch always$b$
。
此外,垂直方向上所有的圆形框都相同。就像一个单元格重复。
答案1
当然,您可以保留您的文档并用一个简单的语句截断图表\ifnum
。
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage{scalerel}
\usepackage{ifthen}
\usetikzlibrary{decorations.markings}
\tikzset{
mynode/.style = {circle, draw, thick, fill = white, inner sep=0pt},
type1/.style = {mynode},
type2/.style = {mynode}
}
\begin{document}
\begin{tikzpicture}[>=stealth]
\draw[step=3cm,color=white] grid (2,2);
\begin{scope}[rotate=45]
\foreach \x in {-3,...,1}
{\foreach \y [evaluate=\y as \z using {int(-\x-\y)}] in {-3,...,1} {
\pgfmathsetmacro{\type}{int(mod(abs(\x+\y),2)+1)};
\pgfmathsetmacro{\pos}{int(mod(abs(\y),2)+1)};
\pgfmathsetmacro{\post}{int(mod(abs(\x),2)+1)};
\ifnum\z<3
\draw[blue, -] (\x-0.6, \y) -- (\x+0.6, \y);
\ifthenelse{\type=2 and \pos=2 and \post=1}{\node[black,below] at (\x-0.3, \y+0.3) {{\tiny$b$}};}{\node[black,below] at (\x-0.3, \y+0.3) {{\tiny $r$}};}
%
\draw[red, -] (\x, \y-0.6) -- (\x, \y+0.6);
\ifthenelse{\type=1 \and \pos=1}{\node[type\type] at (\x,\y) {\color{orange}$K_{\type a}$};}{}
\ifthenelse{\type=1 \and \pos=2}{\node[type\type] at (\x,\y) {\color{orange}$K_{\type b}$};}{}
\ifthenelse{\type=2 \and \pos=1}{\node[type\type] at (\x,\y) {\color{black}$K_{\type a}$};}{}
\ifthenelse{\type=2 \and \pos=2}{\node[type\type] at (\x,\y) {\color{black}$K_{\type b}$};}{}
\fi
}}
\end{scope}
\end{tikzpicture}
\end{document}
至于你更新的问题:这不就是一些模数运算吗?(我承认很多的帮助韩国航空!
\documentclass[tikz,border=1mm]{standalone}
\tikzset{
mynode/.style = {circle, draw, thick, fill = white, inner sep=0pt},
type1/.style = {mynode,text=orange},
type2/.style = {mynode}
}
\begin{document}
\begin{tikzpicture}[>=stealth]
\def\mylst{{"1b","2b","1a","2a"}}
\def\mylstsmall{{"r","b"}}
\draw[step=3cm,color=white] grid (2,2);
\begin{scope}[rotate=45]
\foreach \x in {-3,...,1}
{\foreach \y [evaluate=\y as \z using {int(-\x-\y)}] in {-3,...,1} {
\pgfmathsetmacro{\mylabel}{\mylst[mod(\x-\y+8,4)]}
\pgfmathsetmacro{\myrb}{\mylstsmall[mod(int(\x/2-\y/2+5),2)]}
\pgfmathsetmacro{\type}{int(mod(abs(\x+\y),2)+1)};
\pgfmathsetmacro{\pos}{int(mod(abs(\y),2)+1)};
\pgfmathsetmacro{\post}{int(mod(abs(\x),2)+1)};
\ifnum\z<3
\draw[blue, -] (\x-0.6, \y) -- (\x+0.6, \y);
\draw[red, -] (\x, \y-0.6) -- (\x, \y+0.6);
\node[type\type] at (\x, \y){$K_{\mylabel}$};
\node[black,below,font=\tiny] at (\x-0.3, \y+0.3) {$\myrb$};
%
\fi
}}
\end{scope}
\end{tikzpicture}
\end{document}
这是如何工作的?通过该算法,您可以获得沿对角线的长度为 4 的循环。因此,如果您从顶部向下阅读,您将获得序列{"1b","2b","1a","2a"}
,该序列存储在宏中\mylst
。这些数组操作在 pgfmanual 第 999 页中有详细描述。这对应于负y
方向,因此我们分配标签\pgfmathsetmacro{\mylabel}{\mylst[mod(\x-\y+8,4)]}
。这里是 Ti 的两个惊人功能钾Z 解析器被利用:
\mylst[<index>]
您可以使用(索引从 0 开始,即\mylst[0]
给出第一个元素)访问列表的元素。- 索引可以是整数,或者是某个计算结果为整数的表达式(如果可以的话)
\pgfmathsetmacro{\mylabel}{\mylst[mod(\x-\y+8,4)]}
。
这里,\x
部分原因是列表\x
以相同的模式不断沿正方向重复,并mod
确保列表确实重复。偏移量+8
确保顶部节点是原来的节点,如果更改偏移量,顶部节点标签将更改,模式将发生偏移。您可以选择 12、4、0 或任何等于 8 模 4 的数字。对于边标签,应用了类似的逻辑。要将事物压缩为长度为 2 的列表,坐标将除以 2,这就是为什么如果沿对角线读取,模式会是这样的r
, r
, b
, b
。如果您不喜欢这些除以 2 的值,您可以将列表加长\def\mylstsmall{{"r","r","b","b"}}
。
答案2
我彻底接手了这个问题,并用另一种方法解决了它。
我编写了一个算法来复制您的网络逻辑。为此,有两个变量\etat
(英文状态)即a
或b
和\parite
(英文奇偶校验)即 2 或 1。
\n
表示图的深度。下面。当然,n=5
您可以选择根节点是否为1a
;;或者通过初始化状态()和奇偶校验()变量。这里和1b
2a
2b
\etat
\parite
\etat = b
\parite = 1
\documentclass[border=1mm,tikz]{standalone}
%\usepackage{tikz}
\usepackage{scalerel}
\usepackage{ifthen}
\usetikzlibrary{decorations.markings}
\tikzset{
mynode/.style = {circle, draw, thick, fill = white, inner sep=0pt},
type1/.style = {mynode},
type2/.style = {mynode}
}
%\def\n{3}% #1 depth
%\def\parite{1}% #2 = 1 ou 2
%\def\etat{a}% #3 = a ou b
\newcommand\reseau[3]{
\begin{tikzpicture}[>=stealth]
\draw[step=3cm,color=white] grid (2,2);
\begin{scope}[rotate=-45,every label/.style={outer sep=1pt,inner sep=0pt,font=\scriptsize}]
\gdef\paritex{#2};
\gdef\etatx{#3};
\gdef\paritey{#2};
\gdef\etaty{#3};
% début de graphique
\foreach \x in {0,...,#1}{
\pgfmathsetmacro{\xx}{-#1+\x}
\foreach \y in {0,...,\xx}{
\ifthenelse {\equal{\paritey}{2}} %rang pair
{\ifthenelse {\equal{\etaty}{a}} %A
{
\gdef\paritey{1};
\gdef\etaty{a};
\pgfmathparse {int(subtract(\x,\y))}
\ifthenelse {\equal{\pgfmathresult}{#1}}
{\node[type2] at (\x,\y)(\x\y){\color{black}$K_{2 a}$};}
{\node[type2,label=200:r] at (\x,\y)(\x\y){\color{black}$K_{2 a}$};}
}
{
\gdef\paritey{1};
\gdef\etaty{b};
\pgfmathparse {int(subtract(\x,\y))}
\ifthenelse {\equal{\pgfmathresult}{#1}}
{\node[type2] at (\x,\y)(\x\y){\color{black}$K_{2 b}$};}
{\node[type2,label=200:b] at (\x,\y)(\x\y){\color{black}$K_{2 b}$};}
}
}
{% rang impair
\ifthenelse {\equal{\etaty}{a}}
{
\gdef\paritey{2};
\gdef\etaty{b};
\pgfmathparse {int(subtract(\x,\y))}
\ifthenelse {\equal{\pgfmathresult}{#1}}
{\node[type1] at (\x,\y)(\x\y){\color{orange}$K_{1 a}$};}
{\node[type1,label=200:r] at (\x,\y)(\x\y){\color{orange}$K_{1 a}$};}
}
{
\gdef\paritey{2};
\gdef\etaty{a};
\pgfmathparse {int(subtract(\x,\y))}
\ifthenelse {\equal{\pgfmathresult}{#1}}
{\node[type1] at (\x,\y)(\x\y){\color{orange}$K_{1 b}$};}
{\node[type1,label=200:b] at (\x,\y)(\x\y){\color{orange}$K_{1 b}$};}
}
}
}
\ifthenelse {\equal{\paritex}{2}} %rang pair
{\ifthenelse {\equal{\etatx}{a}} %A
{
\gdef\paritex{1};
\gdef\etatx{b};
}
{
\gdef\paritex{1};
\gdef\etatx{a};
}
}
{% rang impair
\ifthenelse {\equal{\etatx}{a}}
{
\gdef\paritex{2};
\gdef\etatx{a};
}
{
\gdef\paritex{2};
\gdef\etatx{b};
}
}
\gdef\paritey{\paritex};
\gdef\etaty{\etatx};
}
% construction des traits bleu et rouges
\pgfmathsetmacro{\limite}{#1-1}
\foreach \x [evaluate=\x as \xx using int(\x+1)] in {0,...,\limite}{
\pgfmathsetmacro{\limite}{-#1+\x+1}
\foreach \y [evaluate=\y as \yy using int(\y-1)]
in {0,...,\limite}{
\draw[blue] (\x\yy) edge (\x\y);
\draw[red] (\x\y)edge(\xx\y);
}
}
\end{scope}
\end{tikzpicture}
}
\begin{document}
%\def\n{3}% #1 depth
%\def\parite{1}% #2 = 1 ou 2
%\def\etat{a}% #3 = a ou b
\reseau{5}{1}{b}
\end{document}
其他示例
第一个例子:
%\def\n{3}% #1 depth
%\def\parite{1}% #2 = 1 ou 2
%\def\etat{a}% #3 = a ou b
\reseau{3}{2}{a} % 2a
第二个例子:
%\def\n{3}% #1 depth
%\def\parite{1}% #2 0=even et 1 = odd
%\def\etat{1}% #3 0=A et 1=B
\reseau{2}{1}{b}% 1b
第三个例子:
\reseau{1}{2}{b}% 2b
第四个例子:
\reseau{4}{2}{a}% 2a