सामग्री पर जाएँ

लेसन 28 · TikZ and Graphics

TikZ Nodes and Diagrams

Build flowcharts and graphs with TikZ nodes: shapes, text, anchors, the positioning library, edges, named nodes, matrices and trees.

Lines and circles are fine for geometry, but most diagrams are made of boxes with text connected by arrows: flowcharts, state machines, block diagrams, network sketches. TikZ handles these through nodes. A node is a shape with text that you can name, position, style and connect.

Your first nodes

LATEX
\begin{tikzpicture}
  \node at (0,0) {Hello};                        % text at a position
  \node[draw] at (3,0) {Boxed};                  % draw its border
  \node[draw, circle, fill=yellow!30] (a) at (6,0) {A};
  \node[draw, rectangle, rounded corners, minimum width=2cm, minimum height=1cm] (b) at (9,0) {B};
  \draw[->] (a) -- (b);
\end{tikzpicture}

Inside the braces you can put any LaTeX, including math: {$\int_0^1 x\,dx$}. Names in parentheses ((a)) let you refer to the node later; TikZ then draws to its border, not its centre, so arrows end neatly at the edge.

Useful node options:

  • Shape: rectangle, circle, ellipse, diamond, trapezium (load \usetikzlibrary{shapes.geometric} for diamond and others).
  • Size: minimum width, minimum height, minimum size, inner sep, text width=3cm with align=center for wrapped text.
  • Look: draw, fill, thick, rounded corners, drop shadow.

Anchors and the node's compass

Each node has anchors: north, south east, west, center, text, base and so on. Refer to them with a dot: (a.east), (b.north west). Place a node relative to an anchor: \node[anchor=west] at (2,0) {Text}; puts its west side at that point. Angles also work: (a.30).

The stand-in below computes where TikZ ends an arrow on a rectangle's border, which is why arrows never overlap the text.

JS Where does an arrow touch a box?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Placing nodes: the positioning library

Typing coordinates for every node is slow. The positioning library lets you say where a node goes relative to another:

LATEX
\usetikzlibrary{positioning, arrows.meta, shapes.geometric}

\begin{tikzpicture}[
  node distance = 1.2cm and 1.8cm,
  box/.style = {draw, rounded corners, minimum width=2.4cm, minimum height=0.9cm, align=center},
  decision/.style = {box, diamond, aspect=2},
  >={Stealth[round]}
]
  \node[box]                          (start) {Start};
  \node[box, below=of start]          (read)  {Read input};
  \node[decision, below=of read]      (ok)    {Valid?};
  \node[box, right=of ok]             (err)   {Show error};
  \node[box, below=of ok]             (done)  {Process};

  \draw[->] (start) -- (read);
  \draw[->] (read)  -- (ok);
  \draw[->] (ok)    -- node[left]  {yes} (done);
  \draw[->] (ok)    -- node[above] {no}  (err);
  \draw[->] (err)   |- (read);
\end{tikzpicture}

below=of read places the new node under read at node distance; with two values the first is vertical, the second horizontal. right=3cm of x, above left=of y and below=1cm of z give more control. The |- connector goes vertically then horizontally, useful for loops back to an earlier step.

The box/.style line is a style: a reusable bundle of options. See loops and styles.

Edges and labels

You can draw edges as part of a node definition or after it:

LATEX
\node (a) {A};
\node[right=of a] (b) {B} edge[->] node[above] {label} (a);

\draw[->] (a) to[bend left=30] node[midway, above] {$\alpha$} (b);
\draw[->] (b) edge[loop above] (b);                 % self loop (needs loop style)
\draw (a) -- node[sloped, below] {name} (b);        % rotated along the line

Positions on a line use pos=0.3 (30% along) or near start, midway, near end. The quotes library offers a lighter syntax: \draw[->] (a) to ["label", bend left] (b);.

Graphs, trees and matrices

Trees with child:

LATEX
\node {root}
  child { node {left}
    child { node {a} }
    child { node {b} } }
  child { node {right} };

For large trees, use the forest package, which understands a bracket syntax: \begin{forest} [root [left [a][b]] [right]] \end{forest}.

Matrices of nodes align content in a grid:

LATEX
\usetikzlibrary{matrix}
\matrix[matrix of nodes, nodes={draw, minimum size=8mm}, column sep=2mm, row sep=2mm] {
  1 & 2 & 3 \\
  4 & 5 & 6 \\
};

Graph syntax (a library) writes a graph in a compact form:

LATEX
\usetikzlibrary{graphs, graphdrawing}
\tikz \graph [nodes={draw, circle}] { a -> b -> c, a -> c };

The graphdrawing library needs LuaLaTeX; with pdfLaTeX place nodes yourself.

Remember

  • Use positioning for most diagrams and node distance for consistent spacing.
  • Name nodes in short, meaningful keys such as (read).
  • Put the diagram inside a figure with a caption and a label so you can refer to it (labels and cross-references).
  • For overlays on images use overlay and remember picture; they need two compilation runs.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does \node[draw] (a) at (0,0) {A}; create?

    draw makes the border visible, (a) names it and at sets the position.
  2. 2. Which library lets you write below=of start?

    The positioning library adds the of syntax for relative placement.
  3. 3. What does (a.east) refer to?

    Anchors such as east, north and south west are points on a node's border.
  4. 4. Which path operation goes horizontally and then vertically?

    -| goes horizontal first then vertical; |- goes vertical first.

Summary

  • Nodes are shapes with text; name them to connect with \draw or edge.
  • Anchors such as .east and .north west give exact attachment points.
  • The positioning library places nodes relative to each other with node distance.
  • Labels on edges use node[midway, above]; use |- and -| for right-angle routes.
  • Trees, matrices and graph syntax cover structured layouts.

Next: TikZ Loops, Styles and Libraries

अपनी प्रगति ट्रैक करें

मुफ़्त अकाउंट बनाएँ, लेसन पूरे के रूप में चिह्नित करें और किसी भी डिवाइस पर वहीं से शुरू करें जहाँ छोड़ा था।