使用 TikZ 生成模块化图表

使用 TikZ 生成模块化图表

我正在寻找一种更好的方法来创建如下图像:

在此处输入图片描述

我想制作带有块和端口(带有希腊字母的东西)的图表,以便我可以执行以下操作:

  • 不必手动划分块(在划分块时会自动考虑端口及其宽度)。
  • 自动改变块的宽度和高度,以使它们足够宽以容纳所有端口。
  • 以了解其位置的方式更改端口的形状/颜色。(例如,使用样式将块对面的端口颜色设为红色,或者将箭头放入的 NOT 圆圈设为红色。)
  • 不要tikzpicture不断地嵌套环境(这很烦人,而且一定有更好的方法。当我接触到端口有自己的端口的东西时,这一点会变得尤为突出。(端口可以有修饰符等来改变其含义))。

目前,我创建上述图表的代码非常粗糙且手动,随着我需要的图表越来越大,它变得越来越难。我主要在寻找关于如何利用 Ti 制作更模块化和可组合的结构的示例或建议Z,以及有关代码风格的建议。

\documentclass[tikz,crop]{standalone}
\usepackage{amssymb,amsmath}

\usepackage{tikz}
\usetikzlibrary{
  calc,
  positioning,
  shapes.arrows%
}

\begin{document}
    \begin{tikzpicture}[remember picture]
      \tikzstyle{component}=[rectangle
                            ,draw
                            ,node distance =1.5cm
                            ,minimum height = 0.8cm]
      \tikzstyle{port side}=[node distance=0 cm
                            ,outer sep=-0.2 pt
                            ,inner sep=0pt]
      \tikzstyle{port}=[rectangle
                       ,draw
                       ,node distance=0.1 cm,
                       ,outer sep=0 pt
                       ,minimum height = 0.6cm
                       ,minimum width = 0.6cm
                       ,inner sep = 3pt]

      \node[component] (blk1) {Block 1};
      \node[port side,right=of blk1] (blk1.right) {
          \begin{tikzpicture}
            \node[port] (alp) {$\alpha$};
          \end{tikzpicture}
        };
      \node[port side,above=of blk1] (blk1.above) {
          \begin{tikzpicture}
            \node[port] (bet1) {$\beta$};
          \end{tikzpicture}
        };


      \node[component,right=of blk1] (blk2) {Block 2};
      \node[port side,left=of blk2] (blk2.left) {
        \begin{tikzpicture}
          \node[port] (del) {$\delta$};
        \end{tikzpicture}
      };
      \node[port side,above=of blk2] (blk2.above) {
        \begin{tikzpicture}
          \node[port] (bet2) {$\beta$};
        \end{tikzpicture}
      };
      \node[port side,right=of blk2] (blk2.right) {
        \begin{tikzpicture}
          \node[port] (gam) {$\gamma$};
        \end{tikzpicture}
      };

      \node[component,right=of blk2] (blk3) {Block 3};
      \node[port side,left=of blk3] (blk3.left) {
        \begin{tikzpicture}
          \node[port] (eps) {$\epsilon$};
        \end{tikzpicture}
      };
      \node[port side,above=of blk3] (blk3.above) {
        \begin{tikzpicture}
          \node[port] (bet3) {$\beta$};
          \node[port,right=of bet3] (eta) {$\eta$};
        \end{tikzpicture}
      };
      \node[port side,right=of blk3] (blk3.right) {
        \begin{tikzpicture}
          \node[port] (thet) {$\theta$};
        \end{tikzpicture}
      };

      \node[component,right=of blk3] (blk4) {Block 4};
      \node[port side,left=of blk4] (blk4.left) {
        \begin{tikzpicture}
          \node[port] (iota) {$\iota$};
        \end{tikzpicture}
      };
      \node[port side,above=of blk4] (blk4.above) {
        \begin{tikzpicture}
          \node[port] (kap) {$\kappa$};
        \end{tikzpicture}
    };

    \draw[<-] (alp.east) -- (del.west);
    \draw[<-] (gam.east) -- (eps.west);
    \draw[<-] (thet.east) -- (iota.west);

    \draw[rounded corners,solid,<-] (bet1.north) |- +(-1cm,0.2cm);
    \draw[rounded corners,solid,<-] (bet2.north) |- +(-4cm,0.2cm);
    \draw[rounded corners,solid,<-] (bet3.north) |- +(-4.5cm,0.2cm);
    \draw[dashed] ($(bet1.north)+(-1,0.2cm)$) -- +(-.5cm,0cm);

    \draw[rounded corners,solid,<-] (eta.north) |- +(-8.55cm,0.4cm);
    \draw[dashed] ($(eta.north)+(-8.55cm,0.4cm)$) -- +(-.5cm,0cm);

    \draw[rounded corners,solid,<-] (kap.north) |- +(1cm,0.2cm);
    \draw[dashed] ($(kap.north)+(1cm,0.2cm)$) -- +(.5cm,0cm);

  \end{tikzpicture}
\end{document}

答案1

您的问题不太清楚。请像下面我的 MWE 一样整理一下您的代码。然后针对您遇到的单个问题提出具体问题。例如:“如果端口是红色的,我该如何将其涂成红色多于一块砖头?

你可以从下面开始。我也尝试将其转换为更好的编码风格。

% arara: pdflatex

\documentclass[tikz]{standalone}
\usetikzlibrary{%
    ,calc
    ,positioning
    }

\begin{document}
\begin{tikzpicture}[%
    ,component/.style={%
        ,rectangle
        ,draw
        ,node distance =1.5cm
        ,minimum height = 0.8cm
        }
    ,port/.style={%
        ,rectangle
        ,draw
        ,node distance=-.4pt,
        ,minimum height = 0.6cm
        ,minimum width = 0.6cm
        }
    ,connector/.style={%
        ,rounded corners
        ,<-
        }
    ]

    \node[component] (blk1) {Block 1};
        \node[port,right=of blk1] (alp) {$\alpha$};
        \node[port,above=of blk1] (bet1) {$\beta$};

    \node[component,right=of blk1] (blk2) {Block 2};
        \node[port,left=of blk2] (del) {$\delta$};
        \node[port,above=of blk2] (bet2) {$\beta$};
        \node[port,right=of blk2] (gam) {$\gamma$};

    \node[component,right=of blk2] (blk3) {Block 3};
        \node[port,left=of blk3] (eps) {$\epsilon$};
        \node[port,above=of blk3.130] (bet3) {$\beta$};
        \node[port,above=of blk3.50] (eta) {$\eta$};
        \node[port,right=of blk3] (thet) {$\theta$};

    \node[component,right=of blk3] (blk4) {Block 4};
        \node[port,left=of blk4] (iota) {$\iota$};
        \node[port,above=of blk4] (kap) {$\kappa$};

    \path[->] (iota) edge (thet) (eps) edge (gam) (del) edge (alp);

    \draw[connector] (bet1.north) |- +(-0.1,0.2);
    \draw[connector] (bet2.north) |- +(-0.1,0.2);
    \draw[connector] (bet3.north) |- +(-6.5,0.2);
    \draw[dashed] ($(bet1.north)+(-1,0.2)$) -- +(-.5,0);

    \draw[connector] (eta.north) |- +(-8.55,0.4);
    \draw[dashed] ($(eta.north)+(-8.55,0.4)$) -- +(-.5,0);

    \draw[connector] (kap.north) |- +(1,0.2);
    \draw[dashed] ($(kap.north)+(1,0.2)$) -- +(.5,0);   
\end{tikzpicture}
\end{document}

相关内容