tikz 定位。如何仅通过 y 坐标将一个节点放置在另一个节点下方,而 x 坐标保持不变

tikz 定位。如何仅通过 y 坐标将一个节点放置在另一个节点下方,而 x 坐标保持不变

我在表格的不同列中有 2 个链。我需要移动第二个链,使 位于{mv r0 <- a}下方{br a = 0 $end},如图所示:红线正下方。

如何在不插入假节点和调整其长度的情况下做到这一点?

所期望的职位
编辑[如@Werner 所建议的]:这是重现示例的代码。在上图中,右列已被图形编辑器全部移动,原本它们是对齐的。

\newcommand*{\ld}[2]{ld\ #1\gets \$#2}
\newcommand*{\st}[2]{st\ #1\to \$#2}
\newcommand*{\add}[2]{add\ #1\gets #2}
\newcommand*{\mul}[2]{mul\ #1\gets #2}
\newcommand*{\mov}[2]{mv\ #1\gets #2}
\newcommand*{\inc}[1]{inc\ #1}
\newcommand*{\brz}[2]{br\ #1=0\ \$#2}

\begin{tikzpicture}
[-,auto,
up/.style={draw=none,above=.5cm},
down/.style={draw=none,below=.5cm},
long/.style={minimum height=2.5cm}]
\matrix [matrix,row sep=0.2cm,nodes={draw,font=\footnotesize},every even column/.style={text centered,text width=2.3cm}]
{
    \node (up1)[up]{};&&\node(up2)[up]{};&&\node (up3)[up]{};&&\node(up4)[up]{};&\\
&\begin{scope}
    [node distance=1mm, start chain=going below];
    \node [anchor=north, on chain] {$\inc a$};
    \node [on chain, long] {$\ld a a$};
    \node [on chain] {$\brz a{end}$};
    \node [on chain] {$\inc a$};
    \node [on chain, long] {$\ld a a$};
    \node [on chain] {$\brz a{end}$};
\end{scope}&
&\begin{scope}
    [node distance=1mm, start chain=going below];
    \node [anchor=north, on chain] {$\mov {r_0}a$};
    \node [on chain, long] {$\ld {r_1} {r_0}$};
    \node [on chain] {$\mul {r_1}{r_1*r_1}$};
    \node [on chain, long] {$\st {r_1} {r_0}$};
\end{scope}\\
    \node (down1)[down]{};&&\node (down2)[down]{};&&\node(down3)[down]{};&&\node(down4)[down]{};&\\
};
\begin{scope}
\draw (up1) to (down1);
\draw (up2) to (down2);
\draw (up3) to (down3);
\draw (up4) to (down4);
\end{scope}
\end{tikzpicture}

答案1

at选项与命名链相结合给出了一个解决方案:第一个链被命名ch;第二个链的第一个节点位于at第一个链的第四个节点北侧的北锚点处(ch-4.north)。

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}

\newcommand*{\ld}[2]{ld\ #1\gets \$#2}
\newcommand*{\st}[2]{st\ #1\to \$#2}
\newcommand*{\add}[2]{add\ #1\gets #2}
\newcommand*{\mul}[2]{mul\ #1\gets #2}
\newcommand*{\mov}[2]{mv\ #1\gets #2}
\newcommand*{\inc}[1]{inc\ #1}
\newcommand*{\brz}[2]{br\ #1=0\ \$#2}

\begin{tikzpicture}
[-,auto,
up/.style={draw=none,above=.5cm},
down/.style={draw=none,below=.5cm},
long/.style={minimum height=2.5cm}]
\matrix [matrix,row sep=0.2cm,nodes={draw,font=\footnotesize},every even column/.style={text centered,text width=2.3cm}]
{
    \node (up1)[up]{};&&\node(up2)[up]{};&&\node (up3)[up]{};&&\node(up4)[up]{};&\\
   &\begin{scope}
    [node distance=1mm, start chain=ch going below];
    \node [on chain] {$\inc a$};
    \node [on chain, long] {$\ld a a$};
    \node [on chain] {$\brz a{end}$};
    \node [on chain] {$\inc a$};
    \node [on chain, long] {$\ld a a$};
    \node [on chain] {$\brz a{end}$};
\end{scope}&
&\begin{scope}
    [node distance=1mm, start chain=going below];
    \node [on chain,anchor=north,at={(ch-4.north)}] {$\mov {r_0}a$};
    \node [on chain, long] {$\ld {r_1} {r_0}$};
    \node [on chain] {$\mul {r_1}{r_1*r_1}$};
    \node [on chain, long] {$\st {r_1} {r_0}$};
\end{scope}\\
    \node (down1)[down]{};&&\node (down2)[down]{};&&\node(down3)[down]{};&&\node(down4)[down]{};&\\
};
\begin{scope}
\draw (up1) to (down1);
\draw (up2) to (down2);
\draw (up3) to (down3);
\draw (up4) to (down4);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以使用matrix of nodes键删除\node矩阵单元中的附加声明。您还可以锚定范围,以便从声明的点进行处理,最后,您可以随时使用 移动节点,以便cm={a,b,c,d,(coord1,coord2}与转换矩阵一起缩放[xnew;ynew] = [a,c;b,d]*[x;y] + [coord1;coord2]

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains}
\newcommand*{\ld}[2]{ld\ #1\gets \$#2}
\newcommand*{\st}[2]{st\ #1\to \$#2}
\newcommand*{\add}[2]{add\ #1\gets #2}
\newcommand*{\mul}[2]{mul\ #1\gets #2}
\newcommand*{\mov}[2]{mv\ #1\gets #2}
\newcommand*{\inc}[1]{inc\ #1}
\newcommand*{\brz}[2]{br\ #1=0\ \$#2}
\begin{document}
\begin{tikzpicture}
[-,auto,
up/.style={draw=none,above=.5cm},
down/.style={draw=none,below=.5cm},
long/.style={minimum height=2.5cm}]
\matrix [row sep=0.2cm,nodes={draw,font=\footnotesize},every even column/.style={text centered,text width=2.3cm}]
{
    \node (up1)[up]{};&&\node(up2)[up]{};&&\node (up3)[up]{};&&\node(up4)[up]{};&\\
&\begin{scope}
    [node distance=1mm, start chain=going below];
    \node [anchor=north, on chain] {$\inc a$};
    \node [on chain, long] {$\ld a a$};
    \node [on chain] {$\brz a{end}$};
    \node [on chain] (inca) {$\inc a$};
    \node [on chain, long] {$\ld a a$};
    \node [on chain] {$\brz a{end}$};
\end{scope}&
&\begin{scope}[anchor=north west,cm={1,0,0,1,(inca.north east)},
    node distance=1mm, start chain=going below];
    \node [anchor=north, on chain] {$\mov {r_0}a$};
    \node [on chain, long] {$\ld {r_1} {r_0}$};
    \node [on chain] {$\mul {r_1}{r_1*r_1}$};
    \node [on chain, long] {$\st {r_1} {r_0}$};
\end{scope}\\
    \node (down1)[down]{};&&\node (down2)[down]{};&&\node(down3)[down]{};&&\node(down4)[down]{};&\\
};
\begin{scope}
\draw (up1) to (down1);
\draw (up2) to (down2);
\draw (up3) to (down3);
\draw (up4) to (down4);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容