用 TikZ 繪圖
在 tikzpicture 中寫出路徑指令,例如 \draw (0,0) -- (2,1);、\draw (1,1) circle (0.5); 或 \fill[blue] (0,0) rectangle (1,1);,每個指令都以分號結尾。
線條與座標
用 \usepackage{tikz} 載入 TikZ。繪圖放在 tikzpicture 中。\draw 會描繪一條路徑,-- 則用直線連接各點。(2,1) 這類一般座標以公分為單位。
折線、封閉三角形和格線
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,3);
\draw[thick] (0,0) -- (1,2) -- (2,1) -- (3,2.5);
\draw[blue, fill=blue!15] (3.5,0.5) -- (5,0.5) -- (4.25,2) -- cycle;
\end{tikzpicture}-- cycle 會把路徑接回起點,讓填色的轉角乾淨俐落。
形狀
| 路徑 | 繪製 |
|---|---|
(0,0) rectangle (2,1) | 從一個角到對角的矩形 |
(1,1) circle (0.5) 或 circle[radius=0.5] | 以某點為圓心的圓 |
(1,1) ellipse (1 and 0.5) | 有兩個半徑的橢圓 |
(1,0) arc (0:90:1) 或 arc[start angle=0, end angle=90, radius=1] | 從目前位置開始的圓弧 |
(0,0) grid (3,2) | 間隔 1 公分的格線(其他間隔用 step=0.5) |
(0,0) .. controls (1,1) and (2,1) .. (3,0) | 貝茲曲線 |
(0,0) to[out=90, in=180] (2,2) | 以指定角度離開和進入的曲線 |
矩形、圓形、橢圓、圓弧和曲線
\begin{tikzpicture}
\draw[rounded corners, fill=yellow!30] (0,0) rectangle (2,1.2);
\draw[red, fill=red!20] (3,0.6) circle (0.6);
\draw[teal, thick] (5,0.6) ellipse (0.9 and 0.5);
\draw[->, thick] (7.2,0) arc (0:90:1);
\draw[purple, thick] (0,-1.5) .. controls (1,0) and (2,-3) .. (3,-1.5);
\draw[orange, thick] (4,-2) to[out=90, in=180] (6,-1);
\end{tikzpicture}顏色、線寬和虛線
方括號中的選項設定路徑的樣式。單獨的顏色名稱設定線條顏色;fill= 為形狀填色,\fill 則只填色而不描邊。\filldraw 兩者兼具。
線寬、虛線樣式、顏色和不透明度
\begin{tikzpicture}
\draw[ultra thin] (0,2) -- (3,2);
\draw[thick] (0,1.6) -- (3,1.6);
\draw[line width=3pt, blue!60] (0,1.2) -- (3,1.2);
\draw[dashed] (0,0.8) -- (3,0.8);
\draw[dotted, thick] (0,0.4) -- (3,0.4);
\draw[dash pattern=on 6pt off 2pt on 1pt off 2pt] (0,0) -- (3,0);
\fill[green!50!black] (4,1) circle (0.5);
\filldraw[fill=orange, draw=black, opacity=0.6] (4.5,0.6) rectangle (5.5,1.6);
\draw[<->, red, thick] (4,-0.2) -- (6,-0.2);
\end{tikzpicture}顏色可以是任何 xcolor 名稱(red、teal、violet 等)、blue!30!white 這樣的混色,或你用 \definecolor{mine}{HTML}{0072B2} 定義的顏色。
漸層
\shade 會以顏色漸層填滿形狀:left color 和 right color(或 top color 和 bottom color)產生線性漸層,inner color 和 outer color 產生放射狀漸層,ball color 則產生有光照效果的球體。
線性、放射狀和球體漸層
\begin{tikzpicture}
\shade[left color=blue!60, right color=white] (0,0) rectangle (2.5,1);
\shade[top color=orange, bottom color=yellow] (3,0) rectangle (4,1);
\shade[inner color=white, outer color=teal] (5.2,0.5) circle (0.5);
\shade[ball color=red] (6.7,0.5) circle (0.5);
\end{tikzpicture}用 foreach 重複
\foreach 會對列表中的每個值重複執行一個指令。{0,1,...,5} 會自動補上中間的步驟。迴圈變數可以用在座標、顏色和文字中。
用迴圈畫出的圓形和標籤
\begin{tikzpicture}
\foreach \i in {0,1,...,5} {
\draw[fill=blue!\i0] (1.2*\i,0) circle (0.45);
\node at (1.2*\i,-0.8) {$\i$};
}
\foreach \a in {0,45,...,315}
\draw[->] (9,0) -- ++(\a:0.9);
\end{tikzpicture}(\a:0.9) 是極座標:角度和距離。++ 讓座標相對於前一個點。
scope 與變換
scope 會把選項套用到其中的所有內容:顏色、線條樣式,以及 shift、rotate、scale、xscale 和 yscale 等變換。
同一個正方形經過平移、旋轉和縮放
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\begin{scope}[shift={(2,0)}, rotate=30, red]
\draw (0,0) rectangle (1,1);
\end{scope}
\begin{scope}[shift={(4,0)}, scale=0.5, blue, thick]
\draw (0,0) rectangle (1,1);
\end{scope}
\begin{scope}[shift={(5.5,0)}, xscale=2, dashed]
\draw (0,0) rectangle (1,1);
\end{scope}
\end{tikzpicture}內文中的小圖形
\tikz 會在段落中繪製一個小圖形,例如字旁邊的彩色圓點。在它後面直接寫出路徑,並以分號結尾。
句子中的 \tikz
Status: \tikz \fill[green!60!black] (0,0) circle (3pt); running,
\tikz \fill[red] (0,0) circle (3pt); stopped.與 TeX 安裝環境的差異
TikZ 手冊是 CTAN 上 PGF 的一部分:ctan.org/pkg/pgf。有座標軸的圖表請使用 pgfplots。
常見問題
怎麼在 LaTeX 中畫圓?
載入 TikZ 並寫 \begin{tikzpicture} \draw (0,0) circle (1); \end{tikzpicture}。括號中的值是以公分為單位的半徑。
TikZ 座標使用什麼單位?
一般數字以公分為單位。你也可以寫出單位,例如 (2pt,1mm) 或 (1in,0)。
怎麼為形狀填色?
用 \fill[blue] ...; 填色而不畫外框,或用 \draw[fill=blue!20] ...; 兩者兼具。
本頁的每個範例都在建置網站時由 Scissor LaTeX 排版。 最後更新:2026-11-07