52

I am getting a unhelpful error while trying to place a node above another node using the below= syntax.

\documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \node[] (node1) at (0, 0) {Node 1}; \node[ below = 5pt of node1 ] (node2) {Node 2}; \end{tikzpicture} \end{document} 

The error message is:

Package PGF Math Error: Unknown operator 'o' or 'of' (in '5pt of node1'). 

How can I place nodes in relation to other nodes without getting an error message?

1
  • I recently had the same, even with a documented example. I used ktikz. I then copied it to a normal document and it word. I then made sure that the programs in ktikz had the full path and restarted ktikz. The problem seems to be gone. Commented Feb 2, 2022 at 9:03

2 Answers 2

52

The <placement>=<distance> of <node>.<anchor> syntax only works with the advanced positioning library. The parts

  • distance and
  • of <node>.<anchor> are both optional as
  • .<anchor> is too.

Depending of <placement> the anchor key of the to-be-placed node is internally set according to the following table. The third column shows the default <anchor> if the .<anchor> part is omitted. (The sans-positioning <placement> key is just a better-to-comprehend version of the anchor key.)

 <placement> anchor .<anchor> —————————————————————————————————————————— above south .north left east .west below north .south right west .east –––––––––––––––––––––––––––––––––––––––––– base left base east .base west base right base west .base east mid left mid east .mid west mid right mid west .mid east –––––––––––––––––––––––––––––––––––––––––– above left south east .north west above right south west .north east below left north east .south west below right north west .south east 

If no <distance> is given, the value of node distance is taken.

Note that for the four bottom ones the <distance> part can consist of <vertical distance> and <horizontal distance>. (The same is true for the node distance key that is enhanced by the positioning library also.)

The on grid option sets both anchors (the one of the to-be-placed node as well as the referenced .<anchor>) to .center so that the nodes are placed in a grid. The on grid key basically makes all <placement> key acts like the old <placement> of keys; those however do not use both values given to node distance but only the first.

_The topics covered by the last two paragraphs are further explained in the PGF manual.

Example (with positioning library)

Code

\documentclass[tikz, border=2pt]{standalone} \usetikzlibrary{positioning} \begin{document} \begin{tikzpicture}[nodes=draw] \node (node1) at (0, 0) {Node 1}; \node[below=5pt of node1] (node2) {Node 2}; \end{tikzpicture} \end{document} 

Example (without positioning library)

Without the positioning library this can be achieved by combining

  • the old <placement>=<distance> and
  • the at=<node>.<opposite anchor> key,

where <opposite anchor> is the corresponding .<anchor> of the table above.

The correct anchor part is set without the positioning library, too.

Code

\documentclass[tikz,border=2pt]{standalone} \begin{document} \begin{tikzpicture}[nodes=draw] \node (node1) at (0, 0) {Node 1}; \node[below=5pt,at=(node1.south)] (node2) {Node 2}; \end{tikzpicture} \end{document} 

Output

enter image description here

1
26

The positioning library is missing:

\usetikzlibrary{positioning} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.