Grid network in TikZ
Of course one can keep your document and truncate the diagram with a simple \ifnum
statement.
\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}
As for your updated question: isn't that just some modulo arithmetic? (I acknowledge a lot of help by KJO!)
\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}
How does that work? By the algorithm you get cycles of length 4 along the diagonals. So if you read it from the top going down right you will get the sequence {"1b","2b","1a","2a"}
, which gets stored in the macro \mylst
. These operations on arrays are described in detail on p. 999 of thee pgfmanual. This corresponds to the negative y
direction, therefore we assign the labels \pgfmathsetmacro{\mylabel}{\mylst[mod(\x-\y+8,4)]}
. Here two amazing features of the TikZ parser were exploited:
- You can access elements of a list with
\mylst[<index>]
(where the index starts at 0, i.e.\mylst[0]
gives you the first element. - The index can be an integer, or, some expression that evaluates to an integer, which if why
\pgfmathsetmacro{\mylabel}{\mylst[mod(\x-\y+8,4)]}
works.
Here, the \x
part is due to the fact that the list keeps repeating in the positive \x
direction with the same pattern, and mod
makes sure that the list really repeats. The offset +8
makes sure that top node is what it is, if you change this, the top node label will change, and the pattern will get shifted. You could have chosen 12, 4, 0, or any number that equals 8 modulo 4. For the edge labels, a similar logic is applied. To condense things to a list of length 2, the coordinates get divided by 2, which is why the pattern, if read along a diagonal is something like r
,r
,b
,b
. If you do not like these divisions by 2, you could make the list longer, \def\mylstsmall{{"r","r","b","b"}}
.
I took over the problem completely and solved it in another way.
I wrote an algorithm that replicates the logic of your network.
For this, there are two variables \etat
(state in english) that is a
or b
and \parite
(parity in english) that is 2 or 1.
\n
represents the depth of the graph. Below n=5
. Of course, you can choose whether the root node is 1a
; 1b
; 2a
or 2b
by initializing the state (\etat
) and parity (\parite
) variables. Here \etat = b
and \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}
Other examples
first example:
%\def\n{3}% #1 depth
%\def\parite{1}% #2 = 1 ou 2
%\def\etat{a}% #3 = a ou b
\reseau{3}{2}{a} % 2a
second example:
%\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
third example:
\reseau{1}{2}{b}% 2b
fourth example:
\reseau{4}{2}{a}% 2a