Position nodes in TikZ with relative directions and anchors.
Place nodes relative to each other using the positioning library and direction keywords like right=of instead of manual coordinates. Control spacing with node distance and align shapes precisely with named anchors.
The short answer: load \usetikzlibrary{positioning} in your preamble, name your nodes, and use direction keywords like right=of to build diagrams that adjust automatically when text changes.
Load the positioning library
| Goal | Older Syntax | Modern Syntax (Positioning Library) |
|---|---|---|
| Place node to the right | \node[right of=a] (b) {B}; | \node[right=of a] (b) {B}; |
| Custom distance | \node[right=2cm of a] (b) {B}; | \node[right=2cm of a] (b) {B}; |
| Anchor alignment | Requires manual shifts |
Four steps for relative placement
Follow these steps to build clean, maintainable flowchart and diagram layouts.
Positioning methods and syntax
Basic direction keywords
Use above, below, right, left, and their diagonal combinations to place nodes relative to a named anchor without calculating coordinates.
\node[draw] (a) {A}; \node[draw, right=of a] (b) {B}; \node[draw, below=of a] (c) {C};Adjusting distance locally
Override the global node distance for a single node by passing a dimension directly into the placement key.
\node[draw] (a) {A}; \node[draw, right=2cm of a] (b) {B};Aligning specific anchors
Connect a specific anchor of the reference node to an anchor of the new node for precise alignment.
\node[draw] (a) {A}; \node[draw, above=of a.north west, anchor=south west] (b) {B};Setting global spacing
Configure default spacing across the entire picture scope using node distance in the tikzpicture options.
\begin{tikzpicture}[node distance=8mm and 12mm] \node[draw] (a) {A}; \node[draw, right=of a] (b) {B}; \end{tikzpicture}
Common positioning errors and fixes
Unknown operator of
If TikZ throws an error on right=of, check that \usetikzlibrary{positioning} is loaded in the preamble and appears before \begin{document}.
Nodes overlapping unexpectedly
The modern positioning library measures distance between node outer borders rather than centers. Adjust node distance or check inner and outer separator values.
Text baselines out of alignment
When placing text-heavy nodes side by side, use base right=of or base left=of to keep text baselines aligned horizontally.
よくある質問
Why does right=of fail in my TikZ diagram?
The right=of syntax requires the positioning library. Add \usetikzlibrary{positioning} to your preamble. Without it, TikZ only recognizes the older right of= syntax.
What is the difference between right of=a and right=of a?
The older right of= syntax places the node center at a fixed distance from the reference node center. The modern right=of syntax measures distance between the outer borders of the node boxes.
How do I change the distance between two specific nodes?
Pass a dimension directly into the placement key, such as \node[right=2.5cm of a] (b) {B};. This overrides the default node distance for that single placement.
あなたの論文を Texeno で試してみましょう。
無料で始めて、プロジェクトを .zip でアップロードし、リコンパイルを押すだけ。コンパイル時間を延ばしたくなったらアップグレードしてください。
書き始める