如何排列共享边框的 tikz 中的相邻节点

如何排列共享边框的 tikz 中的相邻节点

这是我目前的绘制方式:

\documentclass{article}
\usepackage{tikz}
\usepackage[paperwidth=15cm]{geometry}

\begin{document}
\noindent

\begin{tikzpicture}
\node[draw,rectangle,text width=8cm,align=center] (qw) at (0,0) {\texttt{rax}} ;
\node[label={\footnotesize{63}}] (top) at (-4,0) {} ;
\node[label={\footnotesize{31}}] (low32) at (0,0) {} ;
\node[label={\footnotesize{15}}] (low16) at (2,0) {} ;
\node[label={\footnotesize{7}}] (low8) at (3,0) {} ;
\node[label={\footnotesize{0}}] (bot) at (4,0) {} ;
\node[draw,rectangle,text width=4cm,align=center,xshift=2cm,yshift=-0.7cm] (dw) {\texttt{eax}} ;
\node[draw,rectangle,text width=2cm,align=center,xshift=3cm,yshift=-1.4cm] (w) {\texttt{ax}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=2.5cm,yshift=-2.1cm] (hb) {\texttt{ah}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=3.5cm,yshift=-2.1cm] (lb) {\texttt{al}} ;
\end{tikzpicture}

\end{document}

它看起来像这样:

当前图像

除了最后一行之外,一切都很好,最后一行的两个矩形似乎重叠了。中间应该只有一条线ah——al正好在中间。我该如何解决这个问题?

答案1

您可以设置inner xsep=0pt

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\usepackage[paperwidth=15cm]{geometry}


\begin{document}
\noindent
\begin{tikzpicture}[inner xsep=0pt]
\node[draw,rectangle,text width=8cm,align=center] (qw) at (0,0) {\texttt{rax}} ;
\node[label={\footnotesize{63}}] (top) at (-4,0) {} ;
\node[label={\footnotesize{31}}] (low32) at (0,0) {} ;
\node[label={\footnotesize{15}}] (low16) at (2,0) {} ;
\node[label={\footnotesize{7}}] (low8) at (3,0) {} ;
\node[label={\footnotesize{0}}] (bot) at (4,0) {} ;
\node[draw,rectangle,text width=4cm,align=center,xshift=2cm,yshift=-0.7cm] (dw) {\texttt{eax}} ;
\node[draw,rectangle,text width=2cm,align=center,xshift=3cm,yshift=-1.4cm] (w) {\texttt{ax}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=2.5cm,yshift=-2.1cm] (hb) {\texttt{ah}} ;
\node[draw,rectangle,text width=1cm,align=center,xshift=3.5cm,yshift=-2.1cm] (lb) {\texttt{al}} ;
\end{tikzpicture}
\end {document}

答案2

结果类似,但带有bytefield

\documentclass{article}
\usepackage{bytefield}
\usepackage{lmodern}
\begin{document}
\begin{bytefield}[endianness=big,
    bitwidth=.015\linewidth,
    boxformatting={\centering\sffamily}]{64}
\bitheader[endianness=big]{0,7,15,31,63}\\
\wordbox{1}{rax}\\[3mm]
\bitbox[]{32}{}
\bitbox{32}{eax}\\[3mm]
\bitbox[]{48}{}
\bitbox{16}{ax}\\[3mm]
\bitbox[]{48}{}
\bitbox{8}{al}\bitbox{8}{ah}
\end{bytefield}
\end{document}

在此处输入图片描述

答案3

离题,为了乐趣和练习:

\documentclass{article}
\usepackage[paperwidth=15cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\noindent%
    \begin{tikzpicture}[
node distance = 3mm and 0mm,
   box/.style = {draw, text width=#1, align=center,minimum height=5mm,
                 inner sep=0pt, outer sep=0pt, font=\ttfamily},
   lbl/.style = {font=\footnotesize, inner sep=0pt, above=3mm}
                        ]
\node[box=8cm,right=of {(0,0)}] (qw) {rax};
\node[lbl] at (0,0) {63};
\node[lbl] at (4,0) {31};
\node[lbl] at (6,0) {15};
\node[lbl] at (7,0) {7} ;
\node[lbl] at (8,0) {0} ;
\node[box=4cm,below left=of qw.south east] (dw) {eax};
\node[box=2cm,below left=of dw.south east] (w)  {ax} ;
\node[box=1cm,below left=of w.south east]   (lb) {al} ;
\node[box=1cm,left=of hb]  (hb) {ah} ;
    \end{tikzpicture}
\end {document}

结果:

在此处输入图片描述

附录: 一个矿石解决方案,使用chains库:

\documentclass{article}
\usepackage[paperwidth=15cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{chains,positioning}

\begin{document}
\noindent%
    \begin{tikzpicture}[
node distance = 3mm and 0mm,
    start chain= going below,
   box/.style = {draw, text width=#1, align=center,minimum height=5mm,
                 inner sep=0pt, outer sep=0pt, font=\ttfamily,
                 on chain,anchor=north west},
   lbl/.style = {font=\footnotesize, inner sep=0pt, above=3mm}
                        ]
\node[box=8cm,right=of {(0,0)}] (qw) {rax};
\node[lbl] at (0,0) {63};
\node[lbl] at (4,0) {31};
\node[lbl] at (6,0) {15};
\node[lbl] at (7,0) {7} ;
\node[lbl] at (8,0) {0} ;
\node[box=4cm] (dw) {eax};
\node[box=2cm] (w)  {ax} ;
\node[box=1cm] (lb) {al} ;
\node[box=1cm,left=of lb] {ah} ;
    \end{tikzpicture}
\end {document}

结果和以前一样。

相关内容