Tikz 之字形图,节点尺寸不断增加

Tikz 之字形图,节点尺寸不断增加

我正在尝试实现这个目标:

节点较大的锯齿图

由此tikz 示例我能够很容易地得到一条直线。我尝试使用另一个 tikz 示例,但不幸的是,效果并不好——我无法让节点很好地对齐。我尝试设置anchor=center等,但它们纹丝不动。

最小(ish)损坏示例:

最小破碎示例

\documentclass[12pt,a4paper]{minimal}

\usepackage[margin=0cm]{geometry}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{tikzmark,positioning,decorations.pathreplacing,%
    calc,trees,positioning,arrows,chains,shapes.geometric,shapes,%
    matrix,shapes.symbols,scopes}
\makeatletter % http://www.texample.net/tikz/examples/labeled-chain/
\tikzset{joinl/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every joinl]#1(\tikzchaincurrent)\fi}}}
\makeatother

\begin{document}

\tikzset{
>=stealth',
  punktchainoff/.style={
    rectangle,
    rounded corners,
    draw=black, very thick,
    text width=6em,
    minimum height=10em,
    text centered},
  every join/.style={->},
}

\begin{center}
\begin{tikzpicture}
    \matrix (m) [matrix of nodes, row sep=2em, column sep=2em, minimum
    height=30em, anchor=center] {
        \node [punktchainoff] (1) {\vspace{-1.6em}\begin{equation*}
            a
        \end{equation*}};
        &
        \node [punktchainoff] (2)
        {\vspace{-1.6em}\begin{equation*}\begin{array}{ccc}
            a & a \\
            a & a \\
            a & a \\
            a & a \\
            a & a \\
            a & a
        \end{array}\end{equation*}};
        &
        \node [punktchainoff] (3)
        {\vspace{-1.6em}\begin{equation*}\begin{array}{cccccccccccccc}
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a
        \end{array}\end{equation*}};
        \\
        \node [punktchainoff] (5) {\vspace{-1.6em}\begin{equation*}
            b
        \end{equation*}};
        &
        \node [punktchainoff] (6)
        {\vspace{-1.6em}\begin{equation*}\begin{array}{ccc}
            b & b \\
            b & b \\
            b & b \\
            b & b \\
            b & b \\
            b & b
        \end{array}\end{equation*}};
        &
        \node [punktchainoff] (7)
        {\vspace{-1.6em}\begin{equation*}\begin{array}{cccccccccccccc}
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b
        \end{array}\end{equation*}};
        %{$\ldots$}
    \\
    };
    { [start chain]
        \chainin(1)[join];
        \chainin(5)[join];
        \chainin(2)[join];
        \chainin(6)[join];
        \chainin(3)[join];
        \chainin(7)[join];
    }
\end{tikzpicture}
\end{center}

\end{document}

答案1

我会使用锚点并在循环中绘制箭头,因为在\ifodd那里实现起来更容易。

\documentclass[12pt,a4paper]{minimal}

\usepackage[margin=0cm]{geometry}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{tikzmark,positioning,decorations.pathreplacing,%
    calc,trees,positioning,arrows,chains,shapes.geometric,shapes,%
    matrix,shapes.symbols,scopes}
\makeatletter % http://www.texample.net/tikz/examples/labeled-chain/
\tikzset{joinl/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every joinl]#1(\tikzchaincurrent)\fi}}}
\makeatother

\begin{document}

\tikzset{
>=stealth',
  punktchainoff/.style={
    rectangle,
    rounded corners,
    draw=black, very thick,
%     text width=6em,
%     minimum height=10em,
    text centered},
  every join/.style={->},
}

\begin{center}
\begin{tikzpicture}
\node[punktchainoff,anchor=south] (1) at (0,0)  {$a$};
\node[punktchainoff,anchor=north] (2) at (0,-1)  {$b$};
\node[punktchainoff,anchor=south] (3) at (1.5,0) {$\begin{matrix}
            a & a \\
            a & a \\
            a & a \\
            a & a \\
            a & a \\
            a & a \\
\end{matrix}$};
\node[punktchainoff,anchor=north] (4) at (1.5,-1) {$\begin{matrix}
            b & b \\
            b & b \\
            b & b \\
            b & b \\
            b & b \\
            b & b \\
\end{matrix}$};
\node[punktchainoff,anchor=south] (5) at (3,0) {$\begin{matrix}
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
\end{matrix}$};
\node[punktchainoff,anchor=north] (6) at (3,-1) {$\begin{matrix}
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
\end{matrix}$};
\foreach \X [count=\Y] in {2,...,6}
{\ifodd\Y
\draw[->] (\Y.south) -- (\X.north);
\else
\draw[->] (\Y.north) -- (\X.south);
\fi
}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

编辑:我在这个版本中剔除了所有未使用的库并使用独立版本而不是最小版本。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{amsmath}
\usetikzlibrary{arrows}
\begin{document}

\tikzset{
>=stealth',
  punktchainoff/.style={
    rectangle,
    rounded corners,
    draw=black, very thick,
    text centered},
%  every join/.style={->},
}

\begin{tikzpicture}
\node[punktchainoff,anchor=south] (1) at (0,0)  {$a$};
\node[punktchainoff,anchor=north] (2) at (0,-1)  {$b$};
\node[punktchainoff,anchor=south] (3) at (1.5,0) {$\begin{matrix}
            a & a \\
            a & a \\
            a & a \\
            a & a \\
            a & a \\
            a & a \\
\end{matrix}$};
\node[punktchainoff,anchor=north] (4) at (1.5,-1) {$\begin{matrix}
            b & b \\
            b & b \\
            b & b \\
            b & b \\
            b & b \\
            b & b \\
\end{matrix}$};
\node[punktchainoff,anchor=south] (5) at (4,0) {$\begin{matrix}
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
            a & a & a & a \\
\end{matrix}$};
\node[punktchainoff,anchor=north] (6) at (4,-1) {$\begin{matrix}
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
            b & b & b & b \\
\end{matrix}$};
\foreach \X [count=\Y] in {2,...,6}
{\ifodd\Y
\draw[->] (\Y.south) -- (\X.north);
\else
\draw[->] (\Y.north) -- (\X.south);
\fi
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

可以使用chains库来解决这个问题,但我认为 TikZ 矩阵更适合。通常,我会直接使用 TikZ-CD 来实现这一点,但使用简单的 TikZ 数学节点矩阵和一些边也不算太过分。

代码

\documentclass[12pt, tikz]{standalone}
\usepackage{mathtools}
\usetikzlibrary{arrows.meta, matrix, quotes}
\begin{document}
\tikz[
  >=Stealth,
  punktchainoff/.style={
    rectangle,
    rounded corners,
    draw=black, very thick,
    text centered},
  C/.tip={Circle[length=+0pt +4]},
]
\matrix(m)[
  every outer matrix/.append style={inner sep=+0pt},
  matrix of math nodes, ampersand replacement=\&,
  row sep=3em, column sep=2em,
  nodes=punktchainoff,
  row 1/.append style={anchor=south},
  row 2/.append style={anchor=north}
  ]{
   \begin{array}{c} a \end{array}
\& \begin{array}{cc} a & a \\ a & a \end{array}
\& \begin{array}{ccc} a & a & a \\ a & a & a\\ a & a & a\end{array}
\& |[coordinate]| % fake target
\\
   \begin{array}{c} b \end{array}
\& \begin{array}{cc} b & b \\ b & b \end{array}
\& \begin{array}{ccc} b & b & b \\ b & b & a\\ b & b & b\end{array}
\\
}
[->, every edge quotes/.append style={inner sep=+.15em}]
  foreach[count=\d from 2] \c in {1, 2, 3}{
    (m-1-\c) edge["$f$"] (m-2-\c)
    ([xshift=1ex]m-2-\c.north)
    \ifnum\c<3
      edge["$g$"'] ([xshift=-1ex]m-1-\d.south)
    \else
      edge[-{>[sep=+0pt +6] . C[sep=+0pt +4] C[sep=+0pt +4] C}] ([xshift=-1ex]m-1-\d.south)
    \fi
  };
\end{document}

输出

在此处输入图片描述

相关内容