如何根据 foreach 循环中的值获得不同类型的边?

如何根据 foreach 循环中的值获得不同类型的边?

我正在尝试使用 TikZ 绘制 Knodel 图。我试图重现的图表对于每个“维度”都有不同的样式边缘。我希望有不同的 Delta 值对应不同的边在我的 foreach 循环中。

(我遇到的另一个问题是旋转图形使其看起来像下面这样)

以下是来自我正在尝试复制: 论文中的 Knodel 图(图 1)

以下是我目前所掌握的信息: 在此处输入图片描述

这是我的代码:

\documentclass[tikz,border=5mm]{standalone}
\usepackage{xparse}

\begin{document}

%from https://tex.stackexchange.com/a/175520
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\floor}{m}
 {
  \fp_eval:n { floor ( #1 ) }
 }
\ExplSyntaxOff

\tikzstyle{vertex}=[draw, circle, fill=black, inner sep=0.55mm]

\newcommand \knodel[2]{
    \def\N{#1}
    \def\Nhalf{\floor{\N}}
    \def\Delta{#2}

    %make the vertices
    \foreach \j in {0, ..., \numexpr \Nhalf - 1}{
            \node[label=left:{\small (1, \j)}] at (1, \j) [vertex] {};
        }
    \foreach \j in {0, ..., \numexpr \Nhalf - 1}{
            \node[label=right:{\small (2, \j})] at (2, \j) [vertex] {};
        }

    %draw the edges
    \foreach \j in {0, ..., \numexpr \Nhalf - 1}{
        \foreach \k in {0, ..., \numexpr \Delta - 1}{
            %\draw (1,j) to (2, (j + 2^k - 1) mod floor(n/2));
            \pgfmathtruncatemacro \target{mod(\j + (2^\k) - 1,\Nhalf)}
            \draw (1, \j) to (2, \target);
        }
    }
}

\begin{tikzpicture}

\knodel{8}{3}

\end{tikzpicture}
\end{document}

答案1

这些不就是简单的模运算吗?

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\newcommand{\kaspressknodel}[2]{\pgfmathtruncatemacro{\mymod}{#1+1}
  \foreach \X in {0,...,#1}
   {\node[bullet,label={right:$\boldsymbol{(2,\X)}$}] (v2\X) at 
   (4,{1.2*(\X-(#1+0)/2)}){};
   \node[bullet,label={left:$\boldsymbol{(1,\X)}$}] (v1\X) at 
   (1.5,{1.2*(\X-(#1+0)/2)}){};}
  \foreach \X [evaluate=\X as \NextX using {int(mod(\X+3,\mymod))},
  evaluate=\X as \AnotherX using {int(mod(\X+1,\mymod))},] in {0,...,#1}
  {\draw[thick] (v1\X) -- (v2\NextX);
  \draw[dashed] (v1\X) -- (v2\AnotherX);
  \draw (v1\X) -- (v2\X);} 
}
\begin{document}
\begin{tikzpicture}[bullet/.style={circle,inner sep=2pt,fill}]
 \begin{scope}[local bounding box=left]
    \kaspressknodel{6}{3} 
 \end{scope}
 %
 \begin{scope}[local bounding box=legend,shift={(left.east)}]
  \foreach \X [count=\Y starting from 0] in {solid,dashed,thick}
  {\draw[\X] (0.5,1-\Y) --++ (1,0) node[right]{dim~\Y};}
 \end{scope}
 \begin{scope}[local bounding box=right,shift={(legend.east)}]
    \kaspressknodel{3}{3} 
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

更多 Ti获得相同结果的方法是

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\tikzset{pics/.cd,
kaspressknodel/.style n args={2}{code={\pgfmathtruncatemacro{\mymod}{#1+1}
  \foreach \X in {0,...,#1}
   {\node[bullet,label={right:$\boldsymbol{(2,\X)}$}] (v2\X) at 
   (4,{1.2*(\X-(#1+0)/2)}){};
   \node[bullet,label={left:$\boldsymbol{(1,\X)}$}] (v1\X) at 
   (1.5,{1.2*(\X-(#1+0)/2)}){};}
  \foreach \X [evaluate=\X as \NextX using {int(mod(\X+3,\mymod))},
  evaluate=\X as \AnotherX using {int(mod(\X+1,\mymod))},] in {0,...,#1}
  {\draw[thick] (v1\X) -- (v2\NextX);
  \draw[dashed] (v1\X) -- (v2\AnotherX);
  \draw (v1\X) -- (v2\X);} 
}}}
\begin{document}
\begin{tikzpicture}[bullet/.style={circle,inner sep=2pt,fill}]
 \pic[local bounding box=left] {kaspressknodel={6}{3}}; 
 %
 \begin{scope}[local bounding box=legend,shift={(left.east)}]
  \foreach \X [count=\Y starting from 0] in {solid,dashed,thick}
  {\draw[\X] (0.5,1-\Y) --++ (1,0) node[right]{dim~\Y};}
 \end{scope}
 %
 \pic[local bounding box=right,shift={(legend.east)}] {kaspressknodel={3}{3}}; 
\end{tikzpicture}
\end{document}

然后压缩图片就很简单了,例如\pic[local bounding box=left,yscale=0.8] {kaspressknodel={6}{3}};yscale=0.8添加以减少高度。然后您还可以旋转图表或更改其颜色。

相关内容