4

I'm trying to align 2 nodes (instr0 and instr1) to the left. I've followed How can I align two nodes to the left in TikZ? however it doesn't quite work as I'm getting the following error:

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

Here is a minimal reproducible example.

\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}

It works if I remove the .west bit but the nodes are no longer aligned. I'm not sure why the .west attribute is not recognised.

Can anyone see what the issue is?

John
  • 323
  • 2
    write below = of instr0.west, – AndréC Oct 26 '19 at 16:11
  • @AndréC I've tried that. It would cause the top node to be centred and the bottom node to move all the way to the left. – John Oct 26 '19 at 16:14
  • 1
    Not when I compile with pdflatex. Give your complete and compileable code. With which engine do you compile your code? – AndréC Oct 26 '19 at 16:17
  • @AndréC I've updated the example – John Oct 26 '19 at 16:22
  • @AndréC You're correct. That seems to have solved it. Please post it as an answer. – John Oct 26 '19 at 16:24
  • 1
    \tikzstyle is deprecated, please use \tikzset{instruction-bits-variable/.style={rectangle split, rectangle split horizontal, draw, text centered, minimum height=1em}} instead. –  Oct 26 '19 at 16:30
  • @Schrödinger'scat Good point. I'll start using that. – John Oct 26 '19 at 16:33
  • 1
    BTW, you didn't make a "syntax error", you were just using the syntax that does not use the positioning library, which is also deprecated, one reason being that it does not support the positioning relative to anchors. But by itself the syntax is not an error, just deprecated and does not support the same features as the positioning syntax does. –  Oct 26 '19 at 16:37
  • @Schrödinger'scat So below of = instr0 is deprecated and one should use below = of instr0 when using the positioning library. – John Oct 26 '19 at 16:40
  • Yes, that's correct. You did not make an error, but were using a deprecated tool for a task it cannot deal with. –  Oct 26 '19 at 16:41
  • @Schrödinger'scat As comments are transitive, could you suggest an edit to the answer to include the points you mentioned? – John Oct 26 '19 at 16:46
  • @John I'd prefer if someone else does that. Perhaps it is simplest if we keep things as they are. I do not want to start an edit war. –  Oct 26 '19 at 16:48
  • 1
    @John below of = instr0 is not depreciated and works. But not below of = instr0.west which requires the use of the positioning library. – AndréC Oct 26 '19 at 17:12

2 Answers2

5

enter image description here

You made a syntax error when using the positioning library. It is necessary to write 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}
AndréC
  • 24,137
2

Slightly shorter code, recent syntax for determining of node style:

\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}

Result is the same as it is in other answer.

Zarko
  • 296,517