如何在没有偏移的情况下将一个位置置于 Tikz 矩形节点的东北部?

如何在没有偏移的情况下将一个位置置于 Tikz 矩形节点的东北部?

有没有办法让顶行和底行看起来一样,或者这种偏移总是会发生?

\documentclass[twoside,titlepage,12pt,appendixprefix=true]{scrbook}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfmath}

\begin{document}
{
    \centering
    \begin{tikzpicture}
        \tikzset{byte/.append style={rectangle,
                draw=black, fill=white,
                minimum width=0.75cm,
                minimum height=1cm,
                anchor=north west,
                align=center, font=\scriptsize}}
        \tikzset{register/.append style={byte,
                minimum height=0.5cm,
                fill=blue!20}}
        \node [byte,minimum height=1cm] (Address Label) at (0,0) {Addr};
        
        \node [byte, minimum height=0.5cm, minimum width=6cm] (Register Array 0) at (Address Label.north east) {Register Array 0};
        \node [byte, minimum height=0.5cm, minimum width=6cm] (Register Array 1) at ($(Register Array 0.north east) + (0.25cm,0)$) {Register Array 1};
        
        \foreach \r in {0,1}
        {
            \node [byte, minimum height=0.5cm] (Heading R\r D7) at (Register Array \r.south west) {D7};
            \foreach \b in {6,...,0}
            {
                \pgfmathtruncatemacro{\bp}{\b + 1}
                \node [byte, minimum height=0.5cm] (Heading R\r D\b) at ($(Heading R\r D\bp.north west) + (0.75cm,0)$) {D\b};
            }
        }
        \foreach \r in {0,1}
        {
            \node [byte, minimum height=0.5cm] (Heading R\r Da7) at (Heading R\r D7.south west) {D7};
            \foreach \b in {6,...,0}
            {
                \pgfmathtruncatemacro{\bp}{\b + 1}
                \node [byte, minimum height=0.5cm] (Heading R\r Da\b) at (Heading R\r Da\bp.north east) {D\b};
            }
        }
    \end{tikzpicture}
}
\end{document}

输出

答案1

对于这种结构,我认为使用matrix节点并在寄存器完成后添加 Addr 节点而不是从它开始会更容易。

\documentclass[twoside,titlepage,12pt,appendixprefix=true]{scrbook}

\usepackage{tikz}
\usetikzlibrary{matrix, positioning, fit}

\begin{document}

    \centering
    \begin{tikzpicture}[font=\scriptsize,
        register/.style={matrix of nodes, font=\scriptsize, nodes in empty cells,
            column sep=-\pgflinewidth, row sep=-\pgflinewidth,
            nodes={anchor=center, draw, minimum height=.5cm, minimum width=8mm},
            row 1/.style={nodes={draw=none}}},
        byte/.style={rectangle, draw, minimum width=.75cm, minimum height=1cm, font=\scriptsize},
         ]

        \matrix (RA0) [register]{
        &&&&&&&\\D7&D6&D5&D4&D3&D2&D1&D0\\D7&D6&D5&D4&D3&D2&D1&D0\\};
        
        \matrix (RA1) [register, right= 3mm of RA0.east]{
        &&&&&&&\\D7&D6&D5&D4&D3&D2&D1&D0\\D7&D6&D5&D4&D3&D2&D1&D0\\};

        \node[fit=(RA0-1-1) (RA0-1-8), draw, inner sep=-.5\pgflinewidth, label=center:Register Array 0] {};
        \node[fit=(RA1-1-1) (RA1-1-8), draw, inner sep=-.5\pgflinewidth, label=center:Register Array 1] {};
        
        \node [byte, left = -\pgflinewidth of RA0-1-1.north west, anchor=north east] (Address Label) {Addr};
        
    \end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

经过进一步搜索,我发现这个答案关于填充和边框。解决方案是outer sep=0在节点样式上进行设置。

相关内容