跳到主要內容

TikZ 節點、箭頭與示意圖

用 \node[draw] (name) {text}; 放置每個方框,用 positioning 函式庫(right=of name)安排後續方框的位置,再用 \draw[->] (a) -- (b); 把它們連起來。

節點

節點是放在某一點上的一段文字,可以選擇加上外框。draw 為它加上外框,fill 加上背景,circle 或 ellipse 則讓它變成圓形(預設是 rectangle)。括號中的名稱讓你之後可以參照這個節點。

不同形狀、填色和大小的節點

LaTeX導言區: \usepackage{tikz}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \node[draw] (a) at (0,0) {plain box};
  \node[draw, rounded corners, fill=blue!10] (b) at (3,0) {rounded};
  \node[draw, circle, fill=yellow!40] (c) at (5.5,0) {44};
  \node[draw, ellipse] (d) at (8,0) {ellipse};
  \node[draw, minimum width=2.5cm, minimum height=1cm] at (1,-1.6) {fixed size};
  \node[draw, text width=2.6cm, align=center] at (5,-1.6) {text that wraps onto two lines};
\end{tikzpicture}

節點之間的箭頭

\draw[->] (a) -- (b); 會畫出一個從節點 a 指向節點 b 的箭頭。線條從節點的邊框開始並結束於邊框,而不是節點的中心。-| 和 |- 會畫出直角連線。

使用 positioning 函式庫的小型流程圖

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[
    node distance=1.2cm and 1.6cm,
    box/.style={draw, rounded corners, fill=blue!8, minimum width=2.2cm, minimum height=0.8cm},
    >={Stealth},
  ]
  \node[box] (in) {Read data};
  \node[box, right=of in] (clean) {Clean};
  \node[box, right=of clean] (fit) {Fit model};
  \node[draw, circle, below=of clean] (ok) {OK?};
  \draw[->] (in) -- (clean);
  \draw[->] (clean) -- (fit);
  \draw[->] (fit) |- (ok);
  \draw[->] (ok) -| node[pos=0.75, left] {no} (in);
\end{tikzpicture}
\end{document}

right=of in 會把節點放在 in 的右邊,間隔為 node distance。其他方向有 left、above、below,以及 above right 等斜向方向。right=2cm of in 則為單一節點設定距離。

箭頭與節點上的標籤

寫在路徑中間的 node 會為該路徑加上標籤。above、below、left、right 設定位於哪一側,midway、near start、near end 或 pos=0.3 則設定沿路徑的位置。label=below:text 會為節點加上標籤。

有彎曲且附標籤連線的狀態圖

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[state/.style={draw, circle, minimum size=1cm}, >={Stealth}, node distance=2.4cm]
  \node[state, label=below:start] (s0) {$q_0$};
  \node[state, right=of s0] (s1) {$q_1$};
  \node[state, right=of s1, fill=green!15] (s2) {$q_2$};
  \draw[->] (s0) to[bend left] node[above] {$a$} (s1);
  \draw[->] (s1) to[bend left] node[below] {$b$} (s0);
  \draw[->] (s1) -- node[above] {$a$} (s2);
\end{tikzpicture}
\end{document}

錨點

每個節點的邊框上都有錨點:north、south、east、west,以及 north east 等角落。(box.north) 是 box 頂端中央的點。

從節點錨點出發的箭頭

LaTeX導言區: \usepackage{tikz}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \node[draw, minimum width=3cm, minimum height=1cm] (box) {Server};
  \draw[->] (box.north) -- ++(0,0.7) node[above] {north};
  \draw[->] (box.east) -- ++(1,0) node[right] {east};
  \draw[->] (box.south west) -- ++(-0.6,-0.6) node[below left] {south west};
  \node[anchor=west] at (box.south east) {\small (anchor=west)};
\end{tikzpicture}

節點上的 anchor=west 會讓節點的 west 錨點落在指定的點上。

箭頭端點

選項端點在 Scissor LaTeX 中
->、<-、<->TikZ 預設的端點可以使用
-stealth、-latex經典的 stealth 和 LaTeX 端點可以使用
-{Stealth}、-{Latex}(arrows.meta)較新的端點可以使用
|-|兩端各一條短橫線可以使用
圖形上的 >={Stealth}-> 使用的端點可以使用
-{Stealth[length=3mm]} 等端點選項端點的大小和形狀部分支援:端點以預設大小繪製

端點大小和 TikZ 很接近,但並不完全相同。

不支援的功能

這些功能在 TeX 安裝環境中可以編譯,但在這裡不行。大多數會在「問題」分頁中回報。

功能在 Scissor LaTeX 中的結果替代寫法
\matrix(以及 matrix of nodes)整個圖形變成一個帶有訊息的虛線框用 at (x,y) 或 positioning 放置節點
\pic整個圖形變成一個帶有訊息的虛線框在帶有 shift 的 scope 中重複繪製
裝飾(decorate、decoration=...)被忽略並顯示訊息;路徑以一般方式繪製一般路徑或虛線路徑
shapes.geometric 的節點形狀(diamond、trapezium 等)繪製成矩形,並顯示訊息rectangle、circle 或 ellipse
double 雙線被忽略並顯示訊息兩個節點或兩條路徑
to[loop above](單一節點上的迴圈)不會畫出迴圈,也不會顯示訊息;它的標籤會落在節點上從節點邊框出發的 arc 或 .. controls .. 曲線
pin=會顯示 pin 的文字,但沒有 pin 的連線label=
-| 或 |- 路徑上的 pos=標籤只會沿第二段放置,所以 pos=0.25 會落在轉角附近用 pos=0.75 標示第二段,或把路徑拆成兩段
tikz-cd、circuitikz、forest圖形會被省略,並顯示訊息簡單的 TikZ,或改用圖片

\matrix 會變成預留框

LaTeX導言區: \usepackage{tikz}在編輯器中開啟 (在新分頁中開啟)
\begin{tikzpicture}
  \matrix[draw] {
    \node {a}; & \node {b}; \\
    \node {c}; & \node {d}; \\
  };
\end{tikzpicture}

「問題」分頁: \matrix in a picture is not supported yet

菱形節點會繪製成矩形

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
  \node[draw, diamond] at (0,0) {Yes?};
  \node[draw, ellipse] at (2.5,0) {ellipse works};
\end{tikzpicture}
\end{document}

「問題」分頁: TikZ node option 'diamond' is not supported yet

TikZ 手冊(在 PGF 套件中)說明了節點、positioning 函式庫和箭頭端點:ctan.org/pkg/pgf。形狀和路徑請參閱用 TikZ 繪圖。

另請參閱 TikZ 基礎、TikZ 和圖片與圖形。

常見問題

怎麼在 LaTeX 中繪製流程圖?

用帶有 draw 的 TikZ 節點作為方框,用 positioning 函式庫(right=of a、below=of b)安排位置,再用 \draw[->] (a) -- (b); 畫出箭頭。

怎麼在 TikZ 的箭頭上加上文字?

在路徑中間寫一個節點:\draw[->] (a) -- node[above] {text} (b);。

tikz-cd 可以在 Scissor LaTeX 中使用嗎?

目前還不行。tikzcd 圖會被省略並顯示訊息。請改用節點和箭頭繪製簡單的示意圖。

本頁的每個範例都在建置網站時由 Scissor LaTeX 排版。 最後更新:2026-11-07