创建内存节点

创建内存节点

我怎样才能创建具有这种形式的节点:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

\begin{tikzpicture}
    \tikzset{BRAM/.style=
        {
            draw=black,inner sep=0,fill=green!5,matrix of nodes, nodes in empty cells,
            nodes={minimum width=2cm,minimum height=0.15cm,draw,outer sep=0,inner sep=0},
        }   
    }

    \matrix (dbtable) at (0,0) [BRAM]
    {
        \\
        \\
        |[minimum height=1cm]|{R0}
        \\
        \\
        \\
    };

\end{tikzpicture}
\end{document}

我想创建一个内存块节点

答案1

对于您的问题,有几种解决方案,例如使用\newcommandpics,下一个使用样式。

此解决方案的唯一缺点是,当需要时,name必须at将和作为选项提供,node而不是使用更常见的语法。我不确定为什么会发生这种情况,但我认为这与使用node contents而不是有关{}

无论如何,它缩短了打字时间。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}

\begin{tikzpicture}
    \tikzset{BRAM/.style=
        {
               matrix, matrix of nodes, nodes in empty cells,
            row sep=-\pgflinewidth, inner sep=0pt,
            nodes={minimum width=2cm,minimum height=0.15cm,draw,outer sep=0,inner sep=0},
            row 3/.style={nodes={minimum height=1cm}},
            node contents={\\\\ #1\\\\\\}
        }   
    }

    \node[BRAM=R0, name=dbtable, at={(0,0)}];
    \node[BRAM=R1, name=db, at={(2,2)}];
    \node[BRAM=R2, name=db2, below right= 1 and 2 of db, nodes={fill=green!30}];

    %Double arrow between centers stopping at borders
    \draw[<->] (dbtable)--(db2);

    %Arrow between to particular anchor points
    \draw[->,red, thick] (dbtable.center)--(db.south east);

    %Line between to nodes with perpendicular coordinate system
    \draw[->,blue, line width=2pt] (dbtable)|-(db);
    \draw[->,orange] (db-2-1)-|(db2);

    %Curved lines 
    \draw[-,dashed,brown, very thick] (dbtable.south west) to[bend right] (db2.south);
    \draw[<-,green, line width=1mm] (dbtable.north west) to[out=150, in=120] (db-1-1);

\end{tikzpicture}
\end{document}

在此处输入图片描述

更新:BRAM带端口的样式。这不是一个完美的解决方案,因为使用fill覆盖了内部端口。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}

\begin{tikzpicture}
    \tikzset{BRAM/.style=
        {
               matrix, matrix of nodes, nodes in empty cells,
            row sep=-\pgflinewidth, inner sep=0pt,
            nodes={minimum width=2cm,minimum height=0.15cm,draw,outer sep=0,inner sep=0},
            row 3/.style={nodes={minimum height=1cm}},
            node contents={\\\\ #1\\\\\\},
            append after command={\pgfextra%
                \node[anchor=north west, font=\scriptsize, text=black] at (\tikzlastnode-3-1.north west) (\tikzlastnode-read) {Read\strut};
                \node[anchor=south west, font=\scriptsize] at (\tikzlastnode-3-1.south west) (\tikzlastnode-write) {Write\strut};
                \node[anchor=north east, font=\scriptsize] at (\tikzlastnode-3-1.north east) (\tikzlastnode-a) {a\strut};
                \node[anchor=south east, font=\scriptsize] at (\tikzlastnode-3-1.south east) (\tikzlastnode-b) {b\strut};
                \endpgfextra}
        }   
    }

    \node[BRAM=R0, name=dbtable, at={(0,0)}];
    \node[BRAM=R1, name=db, above right=2 and 1 of dbtable];

    %Line between to nodes with perpendicular coordinate system
    \draw[->,blue, line width=2pt] (dbtable-a.east)--++(0.5,0)|-(db-write);

\end{tikzpicture}
\end{document}

在此处输入图片描述

第三版本:

最后,我终于成功地绘制了只带有一个节点(不再有matrix)的内存模块。水平线用path picture和 端口添加label。水平线按节点高度的比例绘制,但我不知道如何对 进行同样的操作labels,因此它们的位置从节点中心固定。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\tikzset{
    BRAM/.style=
    {
        draw,
            minimum width=2cm,
            minimum height=3cm,
            path picture={
                \foreach \i in {.1,.2,.8,.9}
              \draw ($(path picture bounding box.north west)!\i!(path picture bounding box.south west)$)--($(path picture bounding box.north east)!\i!(path picture bounding box.south east)$);
                },
        name=#1,
        label={[minimum height=1cm,font=\ttfamily\scriptsize,anchor=south west,name=\tikzlastnode-read]left:Read\strut},
        label={[minimum height=1cm,font=\ttfamily\scriptsize,anchor=north west,name=\tikzlastnode-write]left:Write\strut},
        label={[minimum height=1cm,font=\ttfamily\scriptsize,anchor=south east,name=\tikzlastnode-a]right:a\strut},
        label={[minimum height=1cm,font=\ttfamily\scriptsize,anchor=north east,name=\tikzlastnode-b]right:b\strut},
    },
}

\begin{tikzpicture}
    \node[BRAM, name=R, fill=red] at (0,0) {R0};
    \node[BRAM, name=R1, minimum height=5cm] at (5,0) {R1};
    \draw (R-a)--(R1-read);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容