![如何根据其他坐标设置 tikz 节点大小?](https://linux22.com/image/364976/%E5%A6%82%E4%BD%95%E6%A0%B9%E6%8D%AE%E5%85%B6%E4%BB%96%E5%9D%90%E6%A0%87%E8%AE%BE%E7%BD%AE%20tikz%20%E8%8A%82%E7%82%B9%E5%A4%A7%E5%B0%8F%EF%BC%9F.png)
我需要一种方法让 LaTeX 知道我正在尝试绘制一个节点,其边框设置在坐标处。如果我可以告诉它创建一个框的对角线角度,或者其他任何可以绘制形状的东西,我就会这么做。我有以下内容:
\node[draw,minimum width=1cm,below=.5cm of bytes] (byte1) {$6A$} ; %valid
\node[draw,minimum width=1cm,below=0pt of byte1] (byte2) {$00$} ; %invalid
\draw let \p1 = (byte1.north west),
\p2 = (byte2.south west) in
(\p1) -- (\p2) node [name=insn1,draw,left=of byte1] {push 0x0;};
然而,我真正想要的是 的insn1
大小足以容纳每个byte1
和byte2
,但向每个 的左侧移动。我所表达的只是使 的insn1
大小足以容纳文本push 0x0;
,但向左移动 ob byte1
。我认为我通过使用(\p1) -- (\p2)
部分得到的是放置框的边缘。
答案1
\draw (\p1) -- (\p2)
只是从\p1
到画一条线\p2
。node [...] {...}
在这样的路径的末尾附加节点只是将节点放置在路径的最后一个坐标处(此处为\p2
)。到该点的路径与节点本身的大小无关。(不过,在您的情况下, 优先left=byte1
,并且决定了位置。)
例如,\draw (0,0) -- (2,3) node{foo};
您在两个坐标之间得到一条线,并且foo
节点位于(2,3)
。
但是你可以使用\p1
和\p2
计算和的总高度byte1
,byte2
并将节点的设置minimum height
为该值。或者你可以使用fit
库及其fit
选项。两者如下所示。
byte1
(无关:如果您不想要和之间的双倍线宽byte2
,请below=-\pgflinewidth of byte1
在的设置中设置byte2
。您目前有below=0pt of byte1
。)
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,fit}
\begin{document}
\begin{tikzpicture}
\coordinate (bytes);
\node[draw,minimum width=1cm,below=.5cm of bytes] (byte1) {$6A$} ; %valid
\node[draw,minimum width=1cm,below=0pt of byte1] (byte2) {$00$} ; %invalid
\node [name=insn1,
draw,
fit=(byte1)(byte2), %make the node so large that these two fit inside
inner sep=0pt, % and with zero padding, the fit is snug
left=of byte1.north west, % this and the next line to align the tops
anchor=north,
text height=2.2ex % for better vertical alignment of the text inside the node
] {push 0x0;};
\path let
\p1 = (byte1.north west),
\p2 = (byte2.south west),
\n1 = {\y1-\y2} %\yN is y-coordinate of \pN
in
node [name=insn1,
draw,
minimum height=\n1, % set minimum height to the calculated \n1
right=of byte1.north east,
anchor=north
] {push 0x0;};
\end{tikzpicture}
\end{document}
答案2
作为对 nice 的补充Torbjørn T.答案,不使用fit
库:
\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{calc, positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 0mm and 10mm,
N/.style = {draw, minimum width=10mm, align=center, outer sep=0mm}
]
\node (byte1) [N] {$6A$}; %valid
\node (byte2) [N, below=0pt of byte1] {$00$}; %invalid
%
\path let \p1 = (byte1.north),
\p2 = (byte2.south),
\n1 = {\y1-\y2} in
node (insn1) [N, minimum height=\n1, % set minimum height by \n1
left=of byte1.south west,
] {push\\ 0x0}
node (insn2) [N, minimum height=\n1, % set minimum height by \n1
right=of byte1.south east,
] {push 0x0};
\end{tikzpicture}
\end{document}