tikz 矩形节点无法对齐

tikz 矩形节点无法对齐

我正在尝试将 2 个节点(instr0 和 instr1)向左对齐。我已关注如何在 TikZ 中将两个节点左对齐?但是它不能完全工作,因为我收到了以下错误:

! Package pgf Error: No shape named instr0.west is known.

这是一个最小的可重现示例。

\documentclass{report}

\usepackage{float}
\usepackage{tikz}
\usepackage{listings}

\usetikzlibrary{shapes,arrows, decorations.pathreplacing, positioning}

\tikzstyle{instruction-bits-variable} = [rectangle split,
    rectangle split horizontal,
    draw,
    text centered,
    minimum height=1em]

\begin{document}

\begin {figure}[H]
\centering
\begin{tikzpicture}[auto]

    \node [instruction-bits-variable,
           rectangle split parts=7,
           label={west:{instr 0:}}] (instr0) {};

    \node [instruction-bits-variable,
           rectangle split parts=8,
           node distance = 2em,
           label={west:{instr 1:}},
           below of = instr0.west,
           anchor=west] (instr1) {};

\end{tikzpicture}
\caption {Fixed length instructions}
\label {fig:instruction-format-components}
\end {figure}

\end{document}

如果我删除.west位,但节点不再对齐,它就会起作用。我不确定为什么.west无法识别该属性。

谁能看到这是什么问题吗?

答案1

在此处输入图片描述

使用定位库时出现语法错误。需要编写below = of...

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{shapes,arrows, decorations.pathreplacing, positioning}

\tikzstyle{instruction-bits-variable} = [rectangle split,
    rectangle split horizontal,
    draw,
    text centered,
    minimum height=1em]

\begin{document}

\begin{figure}%[H]
\centering
\begin{tikzpicture}[auto]

    \node [instruction-bits-variable,
           rectangle split parts=7,
           label={west:{instr 0:}}] (instr0) {};

    \node [instruction-bits-variable,
           rectangle split parts=8,
           node distance = 2em,
           label={west:{instr 1:}},
           below  =of instr0.west,
           anchor=west] (instr1) {};

\end{tikzpicture}
\caption {Fixed length instructions}
\label {fig:instruction-format-components}
\end {figure}

\end{document}

答案2

稍微短一点的代码,确定节点样式的最新语法:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{arrows, 
                decorations.pathreplacing, positioning,
                shapes,}

\tikzset{instruction-bits-variable/.style = {% <---
            rectangle split, 
            rectangle split horizontal,
            rectangle split parts=#1,        % <---
            draw, align=center, minimum height=1em}
        } 

\begin{document}
    \begin {figure}[ht]
\centering
    \begin{tikzpicture}[
    node distance = 7mm and 4mm % <--- 
                        ]
\node [instruction-bits-variable=7,
       label=west:{instr 0:}] (instr0) {};
\node [instruction-bits-variable=8,
       label=west:{instr 1:},
       below = of instr0.west, anchor=west] (instr1) {};

\end{tikzpicture}
\caption {Fixed length instructions}
\label {fig:instruction-format-components}
    \end {figure}
\end{document}

结果与其他答案相同。

相关内容