使用 tikzpicture 生成框图,矩形内有矩形

使用 tikzpicture 生成框图,矩形内有矩形

我是 tikzpicture 的新手,但我能够使用以下 MWE 生成图表,但我无法生成如下图所示的图形。请帮助我生成单个块。在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{verbatim}

\begin{document}
\pagestyle{empty}

\tikzstyle{int}=[draw, fill=darkgray!20, minimum width=4cm, minimum height=12cm]
\tikzstyle{init} = [pin edge={to-,thin,black}]

 \begin{tikzpicture}[node distance=7.5cm,auto,>=latex']

\node [int] (a) {Version};
\node (b) [left of=a,node distance=5cm, coordinate] {a};
\node [int] (c) [right of=a] {Version};
\node [coordinate] (end) [right of=c, node distance=5cm]{};


\path[->] (a) edge node {Previous Header} (b);
\path[->] (c) edge node {Previous Header} (a);
\draw[->] (end) edge node {Previous Header} (c) ;
\end{tikzpicture}

\end{document}

答案1

因为它是 tikz 的一个有用示例,它允许您绘制重复的事物,并进行一些序数变化,也因为它并不像看起来那么简单,因此,如果您想创建类似对象的东西,我使用了一些有用的结构,使用定义并使用范围移动它们。对于节点的定位,我更喜欢使用定位库,您可以在第 7.11 章中看到它视觉Tikz

结果: 在此处输入图片描述

梅威瑟:

\documentclass[tikz,border=0.5cm]{standalone}
\usepackage[scaled]{helvet}
\renewcommand*\familydefault{\sfdefault} 
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning,calc,fit,backgrounds}

\begin{document}
    \begin{tikzpicture}[
        %Environment Cfg.
        font=\sffamily\bfseries,
        >={Triangle[angle=50:3pt 2]},%from arrows.meta
        %Environment Styles.
        iBox/.style={
            rectangle,
            xscale= 0.75,
            inner sep=0pt,
            minimum height=7mm,
            minimum width=#1,
            rounded corners=3pt,
            draw=black!50,  
            fill=black!5        
        },
        eBox/.style={
            rectangle,
            rounded corners=1pt,
            draw=black!60,
            fill=black!30       
        },
    ]
    %Create repetitive objet with some variables like order name N, N+1 ...
    %\Block[hash block order][block order]{block-coordinate}
    \def\Block[#1][#2]#3{
        \begin{scope}[shift={(#3)}]
            \draw
            (0,0)
                node[iBox=3.5cm](HASH-#2){Hash of block #1} %node[options](can be omitted){text_content}
                node[iBox,inner xsep=7pt, above right = 1.5pt and 0pt of HASH-#2.north west](VER){\scriptsize Version}
                node[iBox,inner xsep=10pt, above left = 1.5pt and 0pt of HASH-#2.north east]{\scriptsize Merkle Root}
                node[iBox,inner xsep=6pt, below right = 1.5pt and 0pt of HASH-#2.south west]{\scriptsize nonce}
                node[iBox,inner xsep=7pt, below left = 1.5pt and 0pt of HASH-#2.south east]{\scriptsize Difficulty Target}
                node[xscale=0.75, below = 1cm of HASH-#2](STE){\small State}
                node[iBox,inner xsep=4pt,below right = 1.5pt and 1.5pt of HASH-#2.south west|-STE.south](BA){\scriptsize Balance}
                node[iBox,inner xsep=4pt,below left = 1.5pt and 1.5pt of HASH-#2.south east|-STE.south](ST){\scriptsize Storage}
            ($(BA)!.5!(ST)$) % use tikzlibrary calc to obtain the middle point between BA and ST
                node[iBox,inner xsep=4pt]{\scriptsize Code}
                node[iBox=3.5cm,below = 1cm of STE](LoT){\scriptsize List of Transactions};
                \begin{scope}[on background layer] % from tikzlibrary backgrounds
                    \node[eBox,fit=(VER)(LoT),outer sep =5pt, label={[xscale=0.75]90:\scriptsize BLOCK #2}](BK-#2){};
                    \node[iBox=3.5cm,inner sep =2pt,fit=(BA)(ST)(STE)]{};
                \end{scope}
        \end{scope}
    }

    %Start drawing the thing.
    \Block[N-1][N]{0,0};
    \Block[N][N+1]{3.5,0};
    \Block[N+1][N+2]{7,0};
    %Draw final details.
    \node[left=-5pt of BK-N,scale=2]{...};
    \node[right=-5pt of BK-N+2,scale=2]{...};
    \draw[->,very thick](HASH-N+1)--(HASH-N);
    \draw[->,very thick](HASH-N+2)--(HASH-N+1);
    \draw[<-,very thick](HASH-N+2.east)--++(1cm,0);

    \end{tikzpicture}   
\end{document}

相关内容