跳轉到內容

LaTeX/PGF/TikZ

來自華夏公益教科書,一個開放的世界中的開放書籍

LaTeX

入門
  1. 簡介
  2. 安裝
  3. 安裝額外的包
  4. 基礎
  5. 如何獲得幫助

常用元素

  1. 文件結構
  2. 文字格式化
  3. 段落格式化
  4. 顏色
  5. 字型
  6. 列表結構
  7. 特殊字元
  8. 國際化
  9. 旋轉
  10. 表格
  11. 標題建立
  12. 頁面佈局
  13. 自定義頁首和頁尾‎
  14. 匯入圖形
  15. 浮動體、圖形和標題
  16. 腳註和邊注
  17. 超連結
  18. 標籤和交叉引用
  19. 首字母

機制

  1. 錯誤和警告
  2. 長度
  3. 計數器
  4. 規則和撐條

技術文字

  1. 數學
  2. 高階數學
  3. 定理
  4. 化學圖形
  5. 演算法
  6. 原始碼列表
  7. 語言學

特殊頁面

  1. 索引
  2. 詞彙表
  3. 參考文獻管理
  4. 更多參考文獻

特殊文件

  1. 科學報告(學士報告、碩士論文、博士論文)
  2. 信件
  3. 簡報
  4. 教師專欄
  5. 簡歷
  6. 學術期刊(MLA、APA 等)

建立圖形

  1. 介紹程式化圖形
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. 建立 3D 圖形

程式設計

  1. Plain TeX
  2. 建立包
  3. 建立包文件
  4. 主題

雜項

  1. 模組化文件
  2. LaTeX 文件的協作寫作
  3. 匯出到其他格式

幫助和建議

  1. 常見問題解答
  2. 技巧和竅門

附錄

  1. 作者
  2. 連結
  3. 包參考
  4. LaTeX 文件示例
  5. 索引
  6. 命令詞彙表

編輯此框編輯目錄

使用 Tikz 完成的圖形示例。注意稍微透明的頂層。

直接用 TeX 命令繪製圖形的一種方法是 PGF/TikZ。TikZ 可以使用純 (pdf)TEX、(pdf)Latex 或 ConTEXt 生成 PDF 和 PostScript 格式的可移植圖形。它附帶了非常好的 文件 以及大量示例:http://www.texample.net/tikz/

PGF(“可移植圖形格式”)是基礎層,提供了一組用於生成圖形的基本命令,而 TikZ(“TikZ ist kein Zeichenprogramm” 或“TikZ 不是繪圖程式”)是具有特殊語法的前端層,使 PGF 的使用更加容易。TikZ 命令與 Metafont 非常相似,選項機制類似於 PsTricks 語法。

雖然之前的系統(picture, epic, pstricksmetapost)側重於如何繪製,TikZ 更側重於什麼繪製。可以說 TikZ 對於picture就像 LaTeX 對於 TeX 一樣。如果您的 LaTeX 發行版包含它,建議您使用它。

其他建立在 TikZ 之上的包(例如,用於繪製電路圖)可以在此處找到:https://www.ctan.org/topic/pgf-tikz

下面介紹了 TikZ 的一些基礎知識。

載入包、庫 - tikzpicture 環境

[編輯 | 編輯原始碼]

在 LaTeX 文件中使用 TikZ 需要 tikz 包,可以透過在 latex 文件的前言部分新增以下命令來載入它

\usepackage{tikz}                    % in preamble

這將提供基本功能,並自動載入 pgf 包。對於特殊功能,必須包含特殊庫。在程式碼的前言部分需要以下內容。

\usetikzlibrary{⟨list of libraries separated by commas⟩}   % general syntax, in preamble

例如

\usetikzlibrary{arrow,pattern, snakes}               % in preamble

以下是常用庫的列表

庫名稱 描述
\usetikzlibrary{arrows.meta} 箭頭庫,提供不同型別的箭頭尖頭(注意:\usetikzlibrary{arrows} 已過時)。
\usetikzlibrary{automata} 自動機繪製庫,用於繪製有限狀態自動機和圖靈機。
\usetikzlibrary{backgrounds} 背景庫,“為圖片定義背景”。
\usetikzlibrary{calc} 用於進行復雜座標計算的庫。
\usetikzlibrary{calendar} 此庫用於顯示日曆(我想這很直觀)。
\usetikzlibrary{chains} 鏈庫,用於將節點對齊在鏈上。
\usetikzlibrary{decorations} 裝飾庫,用於裝飾路徑。
\usetikzlibrary{er} 實體關係相簿。
\usetikzlibrary{intersections} 用於計算路徑的交點。
\usetikzlibrary{matrix} 矩陣庫,將每個專案作為節點放置在與矩陣中相同的位置。然後可以識別和操作每個節點。
\usetikzlibrary{fadings} 允許以指定模式淡化形狀。
\usetikzlibrary{folding} 摺紙庫。
\usetikzlibrary{patterns} 它提供用於填充區域的不同圖案(水平線、垂直線、東北線、西北線、網格、交叉陰影、點、交叉陰影點、五角星、六角星)。
\usetikzlibrary{petrinet} 它用於繪製 Petri 網,用於數學建模。與其他類似的流程圖風格的圖表一樣,每個節點和邊都定義了,以及它們的樣式和位置。令牌也可以嵌入節點中,方法是將它們視為子節點和子節點。
\usetikzlibrary{shapes.geometric} 它可以幫助繪製不同型別的形狀,例如,n 邊形、n 點星形、禁止標誌(例如,禁止吸菸標誌)和分隔圓。
\usetikzlibrary{shapes.misc}
\usetikzlibrary{shadings} 允許將許多陰影宏和圖案應用於任何形狀。
\usetikzlibrary{shadows} 定義樣式,透過為路徑和/或節點新增(部分)透明陰影來提供幫助。
\usetikzlibrary{snakes} 它有助於繪製曲線,例如蛇形、彈簧、擴充套件波等。
\usetikzlibrary{spy} 允許放大先前定義的圖案和形狀的選定段,想想分形和非均勻繪圖。
\usetikzlibrary{trees} 樹上的每個點都被定義為一個節點,帶有子節點,每個子節點都可以有它自己的子節點。樹的方向也可以指定,以及子節點出現的角度,但是,當它自行決定時,結果是可以接受的。
\usetikzlibrary{mindmap} 思維導相簿,以更具創意的方式顯示父子關係。


Tikz 環境

[編輯 | 編輯原始碼]

圖形繪製在 Tex 文件的主體部分。有兩種方法可以使用它

  1. 內聯模式:當您想在文字中內聯繪製時,應使用此模式。
    \tikz[⟨options⟩]{⟨tikz commands⟩}
    
    在這種情況下,一個特殊的選項是baseline = <dimension>。如果沒有此選項,圖片的下端將放在周圍文字的基線上。使用此選項,您可以指定圖片應該向上或向下移動,以便高度⟨dimension⟩位於基線上。
  2. Tikzpicture 環境:繪圖命令必須包含在“tikzpicture”環境中。
    \begin{tikzpicture}[⟨options⟩] 
      ⟨tikz commands⟩
    \end{tikzpicture}
    

整個圖形可以使用

scale=⟨factor⟩

或對高度和寬度進行不同的縮放,例如

xscale=2.5, yscale=0.5

指定座標

[編輯 | 編輯原始碼]

座標以圓括號指定,以任意 TEX 維度表示,可以使用笛卡爾座標(逗號分隔),例如 x 方向上的 1cm 和 y 方向上的 2pt

座標型別 語法 示例
笛卡爾 (x,y) (1cm,2pt)
極座標 (theta:radius) (30:1cm)
相對於最後的位置 ++(x,y) ++(2cm,2cm)

在表格的第一行,顯示了笛卡爾座標(逗號分隔)。在第二行,顯示了極座標(冒號分隔),例如 30 度方向上的 1cm。

相對於先前給定點的相對座標,是在座標前面新增一個或兩個加號。使用“++”,路徑的最後一點將成為當前位置,使用“+”,先前點將保持當前路徑位置。例如:使用最後一點右邊的 2 個標準單位。

注意

  1. 如果沒有指定單位(1,2),則使用標準單位 cm (1cm,2cm)
  2. 正 x 和 y 方向分別對應於圖表上的右和上。
  3. 角度是從 x 軸測量的,逆時針方向為正。這意味著 0 度指向正右,90 度指向正上。

座標可以與名稱相關聯,例如 A= (2,3),以下列出了幾種方法。

  1. 當我們知道點的確切座標值時,可以使用以下命令。\coordinate (A) at (2,3);
  2. 當某個點相對於另一個點指定時,應該使用路徑命令。\path (A)  ++(45:2) coordinate (B);。此命令表示 - 從座標 A 開始,沿 45 度方向移動 2cm,並將此最終位置座標分配給 B。
  3. 要定義座標並放置文字,請使用節點命令。\node (A) at (90:0) {Coordinate Name}

路徑語法

[編輯 | 編輯原始碼]

路徑是一系列直線和曲線段(簡化解釋)。指令必須以分號結尾。

\path[<options>]⟨specification⟩;

一條指令可以跨多行,也可以將多條指令放在一行上。

路徑操作

[編輯 | 編輯原始碼]

路徑操作的選項例如:“draw”、“fill”、“pattern”、“shade”、“clip”、“use as bounding box”。這些選項可以按以下方式使用。

\path[draw]             % Draw the line/curve 
\path[fill]             % Fill the area under the curve
\path[fill,draw]        % Fill as well as draw the lines (borders)
\path[pattern]          % 
\path[shade]            % a variation on filling that changes colors smoothly from one to another
\path[shade,draw]       % shade as well as draw 
\path[clip]             % all subsequent drawings up to the end of the current scope are clipped against the current path and the size of subsequent paths will not be important for the picture size 
\path[use as bounding box]

上面的命令也可以等效地寫為“\draw”、“\fill”、“\filldraw”、“\pattern”、“\shade”、“\shadedraw”、“\clip”、“\useasboundingbox”。這些命令將在後續章節中詳細解釋。

幾何路徑操作

[編輯 | 編輯原始碼]

幾何路徑選項:“rotate=<angle in degree>”、“xshift=<length>”、“yshift=<length>”、“scale=<factor>”、“xscale=<factor>”、“yscale=<factor>”。

顏色和不透明度

[編輯 | 編輯原始碼]

最常用的方法是僅指定顏色名稱或“color=<color name>”。在這種情況下,它將根據使用的命令(\draw、\fill)對邊框/區域進行著色。

繪圖中可能存在不同的元素,因此可能需要分別指定它們,為此可以使用

"draw=<line color>", "draw opacity=<factor>"

"fill=<fill color>", "fill opacity=<factor>"

"text=<text color>", "text opacity=<factor>"

"pattern color=<color>",

等等

預定義顏色:紅色、綠色、藍色、青色、洋紅色、黃色、黑色、灰色、深灰色、淺灰色、棕色、青綠色、橄欖色、橙色、粉紅色、紫色、藍綠色、紫羅蘭色和白色。

不透明度因子值可以介於 0(=完全透明)到 1(=完全不透明)之間。

線寬選項:“line width=<dimension>”,以及縮寫“ultra thin”表示 0.1pt,“very thin”表示 0.2pt,“thin”表示 0.4pt(預設寬度),“semithick”表示 0.6pt,“thick”表示 0.8pt,“very thick”表示 1.2pt,“ultra thick”表示 1.6pt。

線端、線連線選項:“line cap=<type: round, rect, or butt>”、“arrows=<start arrow kind>-<end arrow kind>”、“rounded corners”、“rounded corners=<size>”、“line join=<type: round, bevel, or miter>”。

線型選項:“dash pattern=<dash pattern>”(例如“dash pattern=on 2pt off 3pt on 4pt off 4pt”)、“dash phase=⟨dash phase⟩”、“solid”、“dashed”、“dotted”、“dashdotted”、“densely dotted”、“loosely dotted”、“double”。


填充路徑的選項例如:“fill=<fill color>”、“pattern=<name>”、“pattern color=<color>”。

\draw 命令

[編輯 | 編輯原始碼]

draw 命令可以用多種方式使用,並帶有不同的選項。以下提供了一些示例。

繪製直線

[編輯 | 編輯原始碼]
  1. 直線由用雙減號分隔的座標給出。
\draw (1,0) -- (0,0) -- (0,1);

  1. 可以使用“--cycle”選項關閉連線路徑,該選項將最後座標和第一個座標用直線連線起來。
\draw (1,0) -- (0,0) -- (0,1) -- cycle;

  1. 現有路徑中的進一步移動操作將啟動路徑的新部分,該部分與路徑的先前部分不連線。這裡:移動到 (0,0),直線到 (2,0),移動到 (0,1),直線到 (2,1)。
\draw (0,0) -- (2,0) (0,1) -- (2,1);

  1. 可以使用直線連線兩點,這些直線僅為水平線和垂直線。對於先水平後垂直的連線,請使用
\draw (0,0) -| (1,1);

或先垂直後水平,請使用

\draw (0,0) |- (1,1);

繪製曲線路徑

[貝塞爾曲線]
  1. 可以使用“..controls() ..()”命令繪製貝塞爾曲線,使用一個或兩個控制點。
\draw (0,0) .. controls (1,1) .. (4,0)
      (5,0) .. controls (6,0) and (6,1) .. (5,2);

  1. 可以使用“to”操作建立使用者定義的路徑。如果沒有選項,它將對應於直線,與雙減號命令完全相同。使用“out”和“in”選項,可以建立曲線路徑。例如,“[out=135,in=45]”會導致路徑在第一個座標處以 135 度角離開,並在第二個座標處以 45 度角到達。
\draw (0,0) to (3,2);
\draw (0,0) to[out=90,in=180] (3,2);
\draw (0,0) to[bend right] (3,2);

(向右彎曲的語法可能看起來有點違反直覺。將其視為在路徑開始處向右轉向,然後平滑地彎曲到終點的指令,而不是表示路徑在其整個長度上向右彎曲。)

繪製特殊曲線 

[編輯 | 編輯原始碼]
  1. 矩形
    \draw (0,0) rectangle (2,3);
    
  2. 圓和橢圓: 命令 "circle" 可用於繪製圓和橢圓。僅圓需要半徑,而橢圓需要長軸和短軸長度。
\draw (0,0) circle [radius=1.5];
\draw (0,0) circle (2cm); % old syntax with round brackets instead of square brackets
\draw (0,0) circle [x radius=1.5cm, y radius=10mm];
\draw (0,0) circle (1.2cm and 8mm); % old syntax
\draw (0,0) circle [x radius=1cm, y radius=5mm, rotate=30];
\draw[rotate=30] (0,0) ellipse (20pt and 10pt);  % old syntax

  1. 圓弧: 命令 "arc" 建立圓或橢圓的一部分。
\draw (0,0) arc (0:270:8mm);
\draw (0,0) arc (0:315:1.75cm and 1cm);
\filldraw[fill=cyan, draw=blue] (0,0) -- (12mm,0mm) arc (0:30:12mm) -- (0,0);

或者使用另一種語法

\draw (0,0)  arc[radius = 8mm, start angle= 0, end angle= 270];
\draw (0,0)  arc[x radius = 1.75cm, y radius = 1cm, start angle= 0, end angle= 315];
  1. 輔助線、拋物線、正弦和餘弦曲線: 還有許多用於特殊路徑的預定義命令,例如 "grid"、"parabola"、"sin"、"cos"([0,π/2] 區間的正弦或餘弦曲線)。選項 "help lines" 表示 "細灰色"。
\draw[help lines] (0,0) grid (2,3);
\draw[step=0.5, gray, very thin] (-1.4,-1.4) grid (1.4,1.4);
\draw (0,0) parabola (1,1.5) parabola[bend at end] (2,0);
\draw (0,0) sin (1,1) cos (2,0) sin (3,-1) cos (4,0) sin (5,1);

使用選項更改線條外觀

[編輯 | 編輯原始碼]

線條有許多屬性可以根據需要更改。例如,在以下示例中,我們選擇線條顏色為紅色,線條樣式為虛線,線條寬度為極粗。

\draw[red, dashed, very thick, rotate=30] (1,0) -- (0,0) -- (0,1);

更改箭頭尖端的示例

\draw [->] (0,0) -- (30:20pt); 
\draw [<->] (1,0) arc (180:30:10pt); 
\draw [<<->] (2,0) -- ++(0.5,10pt) -- ++(0.5,-10pt) -- ++(0.5,10pt);

更改線條外觀的選項列表。
描述 傳遞的選項 可接受的值 備註
更改線條寬度 line width=1mm 任何 值可以以 pt、mm、cm、in 等提供
line width= thick ultra thin,

very thin, thin semi thick thick very thick ultra thick

這些值分別對應 (0.1pt, 0.2pt, 0.4pt, 0.6pt, 0.8pt, 1.2pt, 1.6pt)
更改線條末端形狀 line cap = round round, rect, butt
更改線條樣式 solid solid, dashed, dotted, dash dotted, densely dotted, loosely dotted, double, 預定義線條樣式
dash pattern=on 2pt off 3pt on 4pt off 4pt 自定義線條樣式
箭頭型別 arrow= <-> >,<, >>, >>, latex, stealth,
連線線條 line join= miter round, bevel, miter
示例: \draw[very thick, line cap=round, arrow=latex-latex, line join=round] (0,0)--(3,2);

對於矩形,存在一種特殊的語法。使用移動到操作到一個角,並在 "rectangle" 後面是對角線的座標。最後一個成為新的當前點。

\draw (0,0) rectangle (1,1);
\shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
\filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);

填充顏色 "green!20!white" 表示 20% 綠色和 80% 白色混合在一起。

節點用於在給定座標處放置一些文字。節點本身不屬於路徑的一部分,它們是在路徑繪製後新增到圖片中的。

節點可以放置在矩形、圓形或其他簡單形狀內部。節點可以透過多種方式放置

  1. 使用命令。命令的語法是 \node[options] (Name) at (coordinates) {Text};
    %% syntax
    %% \node[options] (Name of node) at (coordinates) {Text to appear};
    % Example for usage
    \node[red,rectangle] at (0,0) {Some text}; % To place some text only
    \node (A) at (0,0) {}; % To define a new coordinate
    
    節點的名稱應該用括號給出。下面給出一個例子,其中繪製了兩個節點,第一個在圓內,第二個在矩形內。
    \begin{tikzpicture}
    	\node (A) at (0,0) [thick,blue, circle,fill=blue!50] {Encoder};   % node A
    	\node (B) at (3,0) [thick,blue,rectangle,fill=green!80!black] {Decoder}; % node B
    	\draw[->,ultra thick] (A)--(B); % line joining node A and B
    \end{tikzpicture}
    
  2. 在其他命令(例如 \draw 和 \path 命令)中使用關鍵字 node
    \draw (0,0) node{a} -- (1,1) node {b};
    \path (0,0) node{a} -- (1,1) node {b};
    

提供了不同的選項,其中一些在下面的表格中進行了描述。後面將提供一些示例以更好地解釋這些選項。

描述 傳遞的選項 允許的值 備註
路徑上的文字位置 pos=.5 > 0 文字將放置在路徑的開頭 (=0)、中間 (=.5) 和末尾 (=1) 處。(預設值為 1)
文字對齊方式 align=left left, right, center 文字/段落將相應地對齊(預設值為中心)。
相對於給定點放置文字 anchor=top left, right, top, bottom, top left, top right, bottom left, bottom right. 假設該點為原點,有 4 個方向 (x,-x, y, -y),分別對應 left、right、top、bottom。4 個象限 (I、II、III、IV)。
anchor=north east,west, north, south, north east, north west, south east, south west 與上面相同,但現在方向用 north、east、west、south 指定。
文字包含在形狀中 rectangle, circle, 應將相關引數與選項一起傳遞。
包含的形狀和文字之間的距離 Inner sep =.5
包含的形狀到該點的距離 Outer sep =.8
形狀的最小尺寸,與文字長度無關。 minimum width = 2cm 任何值 > 0 即使只有一個字母,形狀(假設為矩形)的寬度也至少為 2cm。但是,對於非常長的文字,尺寸可能會增加。

注意: 節點的尺寸 = max(minimum width, inner sep)。為了獲得更小的尺寸,必須同時縮減這兩個值。

minimum height = 2cm 任何值 > 0 包含形狀的節點的最小高度
minimum size = 2cm 任何值 > 0 形狀的最小高度和寬度。
要保持的形狀的縱橫比 shape aspect = 1 包含文字的形狀將更改其尺寸,以保持此縱橫比。
給文字著色、形狀著色等。 fill= red, draw=green 這與之前描述的顏色和不透明度選項相同。
沿曲線對齊文字 sloped

下面給出了幾個示例來解釋這些選項


使用 node 命令沿著給定路徑編寫文字作為一個簡單的例子

\draw[dotted]
    (0,0) node {1st node}
 -- (1,1) node {2nd node}
 -- (0,2) node {3rd node}
 -- cycle;

\fill[fill=yellow]
    (0,0) node {1st node}
 -- (1,1) node[circle,inner sep=0pt,draw] {2nd node}
 -- (0,2) node[fill=red!20,draw,double,rounded corners] {3rd node};

要在直線或曲線上放置節點,請使用 "pos=<fraction>" 選項,其中 fraction 是一個浮點數,介於 0(表示上一個座標)和 1(表示當前座標)之間。

\draw (0,0) -- (3,1)
  node[pos=0]{0} node[pos=0.5]{1/2} node[pos=0.9]{9/10};

存在一些縮寫:"at start" 表示 "pos=0","very near start" 表示 "pos=0.125","near start" 表示 "pos=0.25","midway" 表示 "pos=0.5","near end" 表示 "pos=0.75","very near end" 表示 "pos=0.875","at end" 表示 "pos=1"。

選項 "sloped" 使節點旋轉以成為曲線的切線。

由於節點通常是路徑上的唯一路徑操作,因此存在一些用於建立的特殊命令

僅包含節點的路徑,第一個具有文字輸出,第二個沒有

\node[<options>](<name>) at (<coordinate>){<text>};
\coordinate[<options>](<name>) at (<coordinate>);

可以使用節點的標籤作為座標連線節點。定義 "\path(0,0) node(x) {} (3,1) node(y) {};" 後,(0,0) 處的節點將被命名為 "(x)",(3,1) 處的節點將被命名為 "(y)"。

\path (0,0) node(x) {} 
      (3,1) node(y) {};
\draw (x) -- (y);

等同於

\coordinate (x) at (0,0); 
\coordinate (y) at (3,1);
\draw (x) -- (y);

可以在節點內包含多行文字。新行用雙反斜槓 "\\" 表示,但此外還需要使用節點選項 "align=" 指定對齊方式。這是一個例子

\filldraw 
(0,0) circle (2pt) node[align=left,   below] {test 1\\is aligned left} --
(4,0) circle (2pt) node[align=center, below] {test 2\\is centered}     -- 
(8,0) circle (2pt) node[align=right,  below] {test 3\\is right aligned};


路徑構造操作試圖做到巧妙,以便路徑從節點形狀的邊界開始,而不是從節點的中心開始。

\path (0,0) node(x) {Hello World!}
(3,1) node[circle,draw](y) {$\int_1^2 x \mathrm d x$};
\draw[->,blue] (x) -- (y);
\draw[->,red] (x) -| node[near start,below] {label} (y);
\draw[->,orange] (x) .. controls +(up:1cm) and +(left:1cm) .. node[above,sloped] {label} (y);

一旦定義了節點 x,就可以使用上面定義的錨點相對於 (x) 使用 "(x.<anchor>)",例如 "(x.north)"。


沿著繪製的曲線放置圓形:

在這種情況下,繪製了一條曲線,然後在曲線上以指定的位置放置一些圓形。此技巧利用了 "foreach 命令",有關詳細資訊,請參閱特殊命令部分。

\draw[very thick] (0.5,0.5) .. controls (2,1).. (2.5,2.5)
	node foreach \p in {0,0.25,...,1} [circle,fill=red,pos=\p,inner sep=0pt,minimum size=1.5mm]{};	
\node[left] at (.5,.30){A};
\node[right] at (2.5,2.5){B} ;	
\path[ultra thick,latex-latex] (2,0) --(0,0)--(0,1.5);

命令用於刪除給定形狀(例如矩形或圓形)外部的部分。

\begin{tikzpicture}
	\clip (1,1)  circle (2);
	\draw[red,fill] (0,0) rectangle (3,3);		
\end{tikzpicture}

特殊命令

[編輯 | 編輯原始碼]

Tikzstyle

[編輯 | 編輯原始碼]

當需要設定具有相同引數(即寬度、顏色等)的多個不同形狀時,這是一個非常有用的命令。因此,可以在開頭定義一個或多個樣式,如下所示,然後可以在 Tikz 程式碼中的任何位置使用它。

\begin{tikzpicture}[
mycircle/.style={circle, draw=green!60, fill=green!5, ultra thick, minimum size=7mm},
myline/.style={dotted, blue!60,->},
]
\draw[myline] (0,0)node[mycircle]{$\pi$} to[out=45,in=135]  (5,1) node[mycircle]{3.414...};
\end{tikzpicture}

如果希望只對程式碼的某一部分應用一些更改,則可以使用 scope 命令。

\begin{tikzpicture}[
mycircle/.style={circle, draw=green!60, fill=green!5, ultra thick, minimum size=7mm},
myline/.style={dotted, blue!60,->},
]
\draw[myline] (0,0)node[mycircle]{$\pi$} to[out=45,in=135]  (5,1) node[mycircle]{3.414...};
% following drawing in the scope is shifted along y by 2cm
\begin{scope}[yshift=2cm]
	\draw[myline] (0,0)node[mycircle]{$\pi$} to[out=45,in=135]  (5,1) node[mycircle]{3.414...};
\end{scope}
\end{tikzpicture}

Foreach 命令

[編輯 | 編輯原始碼]

此命令類似於程式設計中使用的迴圈。它可以透過 "\foreach ⟨variable⟩ in {⟨list of values⟩} ⟨commands⟩" 實現。

\foreach \x in {0,...,9} 
  \draw (\x,0) circle (0.4);

Beamer 風格的動畫

[編輯 | 編輯原始碼]

為了在 beamer 中以最簡單的方式實現動畫,我們可以列印 N 幀,其中物件在 N 步中移動。下面給出一個例子

\begin{frame}
\newcount\p
\animatevalue<3-10>{\p}{0}{100}
\begin{tikzpicture}
	\path(0,0)rectangle(0.75\paperwidth,-0.75\paperheight);
	\path[draw](0,0)..controls +(30:2) and +(40:2)..+(4,-1) node [pos=\p/100,sloped,above]{a};

\end{tikzpicture}
\end{frame}

有三個重要的步驟,如下所示。

  1. 定義一個新變數:在上面的示例中,我們使用 \p 作為儲存計數的變數。
  2. 定義變數可以取的範圍。我們使用命令 \animatevalue<3-10>{\p}{0}{100},它表示:對於 0-100 的範圍,建立 10 個值。<1-10> 表示新變數具有以下值集 \p =[1,2,3,...,10] <3-10> 表示新變數具有以下值集 \p =[0,0,0,1.25,2.5,...,10].
  3. 將變數用於物件位置,這使用 \path[draw](0,0)..controls +(30:2) and +(40:2)..+(4,-1) node [pos=\p/100,sloped,above]{a}; 完成。


PGF 圖層

[編輯 | 編輯原始碼]

有兩種方法可以將曲線放置在背景中 -

  1. 第一種方法是按順序編寫命令,即先繪製背景曲線(座標已經可用),然後繪製在其上方的曲線,依此類推。
  2. 第二種方法是使用 PGF 圖層。當背景曲線座標尚未可用時,這特別有用。它們相對於上層曲線定義。

PGF 圖層提供以下命令

% Define two layers (names could be changed)
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
%% Define there order of layer, here background is the bottom most and foreground is the topmost layer
\pgfsetlayers{background,main,foreground}

%% Whatever you draw here is drawn in the main layer
\draw (1,0) circle (2);

\begin{pgfonlayer}{foreground}  %% draw the curves that should be placed in foreground/top layer
\draw (0,0) arc (0:120:1);
\end{pgfonlayer}

\begin{pgfonlayer}{background}  %% draw the curves that should be placed in background/bottm layer
\draw (0,0) rectangle(2,2);
\end{pgfonlayer}

模式庫

[編輯 | 編輯原始碼]

該庫可以提供 11 種類型的圖案,用於填充給定區域。 您可以使用關鍵字 `pattern color` 選擇顏色。 這些圖案列在下面

\usetikzlibrary{tikz}   
\usetikzlibrary{patterns}   %% in the beginning of tex file

\begin{document}
\begin{tikzpicture}
\draw[pattern=dots, pattern color=blue] (0,0) rectangle ++(1,1);
\draw[pattern=grid, pattern color=blue] (0,1) rectangle ++(1,1);
\draw[pattern=crosshatch, pattern color=blue] (0,2) rectangle ++(1,1);
\draw[pattern=crosshatch dots, pattern color=blue] (0,3) rectangle ++(1,1);
\draw[pattern=fivepointed stars, pattern color=blue] (0,4) rectangle ++(1,1);
\draw[pattern=sixpointed stars, pattern color=blue] (0,5) rectangle ++(1,1);

\draw[pattern=vertical lines, pattern color=blue] (0,6) rectangle ++(1,1);
\draw[pattern=horizontal lines, pattern color=blue] (0,7) rectangle ++(1,1);
\draw[pattern=north east lines, pattern color=blue] (0,8) rectangle ++(1,1);
\draw[pattern=north west lines, pattern color=blue] (0,9) rectangle ++(1,1);
\end{tikzpicture}
\end{document}

蛇形庫

[編輯 | 編輯原始碼]

該庫將路徑結構從直線更改為以下內容。

\usetikzlibrary{snakes}
\begin{tikzpicture}[thick]
\draw[snake=bumps] (0,0) -- (3,0);                          % Semicircle/bumps along the line.
\draw[snake=zigzag] (0,1)-- (3,1);                          % a zig-zag pattern   
\draw[snake=saw] (0,2) -- (3,2);                            % saw type line  
\draw[snake=brace] (0,3)-- (3,3);                           % a brace between two points
\draw[snake=coil,segment length=4pt] (0,4)-- (3,4);         % like a coil
\draw[snake=coil,segment aspect=0] (0,5) -- (3,5);          % like a sinusoidal wave
\draw[snake=snake] (0,6) -- (3,6);                          % sinusoidal wave (similar to coil)
\draw[snake=expanding waves,segment angle=7] (0,7)-- (3,7); % like a expaning wavefront
\draw[snake=border,segment angle=-45] (0,8) -- (3,8);   % slanted lines along the path
\draw[snake=triangles] (0,9) -- (3,9);                  % Triangles along the path
\draw[snake=ticks] (0,10) -- (3,10);                    % vertical ticks along the path
\draw[snake=crosses] (0,11) -- (3,11);                  % crosses along the path  
\end{tikzpicture}

一些影響曲線性質的引數是

segment amplitude=.4mm,

segment length=2mm,

segment object length=.5mm

segment angle = 20

segment aspect=0

raise snake = .2mm

mirror snake

line before snake=1mm, line after snake=1mm, line around snake=1mm,

gap before snakes=1mm, gap after snakes=1mm, gap around snake=1mm

計算包

[編輯 | 編輯原始碼]

可以使用命令 `\usetikzlibrary{calc}` 包含計算包。 該包可用於對座標執行簡單計算。

  • 座標代數。
    \coordinate (A) at (2,3);
    \coordinate (B) at (1.5,2.5);
    \coordinate (B) at ($(A) + (B)$)
    \coordinate (B) at ($(A) - (B)$)
    \coordinate (B) at ($(A) + 2*(A)$)
    
  • 查詢中點
    \coordinate (A) at (0,0);
    \coordinate (B) at (90:3);
    \coordinate (C) at (0:4);
    \coordinate (Ap) at ($(B)!0.5!(C)$);
    \coordinate (Bp) at ($(A)!0.5!(C)$);
    \coordinate (Cp) at ($(A)!0.5!(B)$);
    \draw (A)--(B)--(C)--cycle
          (A)--(Ap) 
          (B)--(Bp) 
          (C)--(Cp);
    
    該包提供了一個特殊函式 `veclen`,可用於計算點之間的距離,如下所示(摘自 https://tikz.dev/tikz-paths 中的第 14.15 節)
    \usetikzlibrary {calc}
    \begin{tikzpicture}
      \draw [help lines] (0,0) grid (3,3);
      \coordinate (a) at (rnd,rnd);
      \coordinate (b) at (3-rnd,3-rnd);
      \draw (a) -- (b);
      \node (c) at (1,2) {x};
      \draw let \p1 = ($ (a)!(c)!(b) - (c) $),
                \n1 = {veclen(\x1,\y1)}
            in circle [at=(c), radius=\n1];
    \end{tikzpicture}
    

交叉庫

[編輯 | 編輯原始碼]

該庫用於查詢任何兩條曲線的交點。 下面給出了一個示例

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\draw[name path=line,smooth] (0,5)--(1.5,5);
\draw[name path=curve,smooth] (1.5,6) to[out=270,in=90] (0,3);
\draw[name intersections={of=line and curve}] (intersection-1) circle[radius=0.1];
\end{tikzpicture}
\end{document}

將交點應用於多條曲線

\begin{tikzpicture}
\draw[name path=grid] [xstep=3,ystep=2] (0,0) grid (9,8);
\draw[->, name path=line] (2,1) -- (7,7);
\draw[name intersections={of=grid and line, sort by=line, name=i, total=\t}]
\foreach \s in {1,...,\t}{(i-\s) node {\s}};
\end{tikzpicture}

PGF 數學

[編輯 | 編輯原始碼]

可以使用 `\usepackage[pgfmath]` 執行許多數學運算,它提供了一個核心命令 `\pgfmathparse` 並將結果返回到 `\pgfmathresult` 中。 一些示例如下

\pgfmathparse{add(75,6)} \pgfmathresult

在上面的示例中,我們使用了函式 `add(x,y)`。 上述命令的替代方法是 `\pgfmathadd{x}{y}`。

許多可用的函式,如下所示。 可以獲得與加法函式相同的替代命令,即透過在命令名稱前加上 `\pgfmath` 字首。

  • add(a,b), subtract (a,b), multiply(a,b), divide(a,b), div(a,b), neg (-a), sqrt(a), pow (a^b), exp(a), ln(a), log10(a), log2(a), abs(a), mod(a,b)
  • round(a), floor(a), ceil(a), int(a), frac(a)
  • 檢查型別:isodd(a), iseven(a), isprime(a)
  • 常量:e, pi
  • 轉換:rad(x), deg(y)
  • 三角函式:sin(x), cos(x), tan(x), sec(x), cosec(x) , cot(x),
  • 反三角函式:asin(x), acos(x), atan(x)
  • 比較:equal(x,y), greater(x,y), less(x,y), notequal(x,y), notgreater(x,y), notless(x,y),
  • 邏輯函式:and(x,y), or(x,y), not(x), ifthenelse(x,y,z),以及邏輯常量,例如 -- true, false。
  • 隨機數生成器:rnd, rand, random(x,y)
  • 雜項:從元素列表中獲取最小值/最大值:min(x1,x2,...,xn), max(x1,x2,...,xn),向量的長度:veclen(x,y) 訪問給定陣列 x 的第 i 個元素:array({x1,x2,x3,...,xn},i)
  • 計算角度:`\pgfmathanglebetweenpoints{P}{Q}`:查詢經過點 P 到 Q 的線的角度。 `\pgfmathanglebetweenlines{P1}{Q1}{P2}{Q2}`:查詢兩條線 L1 和 L2 之間的角度,其中 L1 經過 P1 和 Q1,第二條線經過 P2 和 Q2。
命令 描述
\pgfmathsetlength{name}{value} \pgfmathaddtolength{name}{value} 為給定值設定相應的“name” tex 暫存器
\pgfmathsetcount{name}{value} \pgfmathaddtocount{name}{value}
\pgfmathsetcounter{name}{value} \pgfmathaddtocounter{name}{value}
\pgfmathsetmacro{name}{value} \pgfmathtruncatemacro{name}{value}

PGF 圖表

[編輯 | 編輯原始碼]

PGF 還具有一個數學引擎,使您可以繪製函式

\draw [domain=xmin:xmax] plot (\x, {function});

許多函式都是可能的,包括 factorial(\x), sqrt(\x), pow(\x,y), exp(\x), ln(\x), log10(\x), log2(\x), abs(\x), mod(\x,y), round(\x), floor(\x), ceil(\x), sin(\x), cos(\x), tan(\x), min(\x,y,), 和 max(\x,y)。

注意:

1) 三角函式假設 x 以度為單位;要以弧度表示 x,請在後面加上表示法“r”,例如 sin(\x r)。

2) 兩個有用的常量是 e =2.718281828 和 pi = 3.141592654,可以直接使用 `e` 和 `pi` 在表示式中指定。

兩個函式的示例

\draw [help lines] (-2,0) grid (2,4); 
\draw [->] (-2.2,0) -- (2.2,0); 
\draw [->] (0,0) -- (0,4.2); 
\draw [green, thick, domain=-2:2] plot (\x, {4-\x*\x}); 
\draw [domain=-2:2, samples=50] plot (\x, {1+cos(pi*\x r)});

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
  \draw[thick,rounded corners=8pt] (0,0) -- (0,2) -- (1,3.25) 
   -- (2,2) -- (2,0) -- (0,2) -- (2,2) -- (0,0) -- (2,0);
  \end{tikzpicture}
\end{document}

\documentclass{article}
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}[scale=3]
 \draw[step=.5cm, gray, very thin] (-1.2,-1.2) grid (1.2,1.2); 
 \filldraw[fill=green!20,draw=green!50!black] (0,0) -- (3mm,0mm) arc (0:30:3mm) -- cycle; 
 \draw[->] (-1.25,0) -- (1.25,0) coordinate (x axis);
 \draw[->] (0,-1.25) -- (0,1.25) coordinate (y axis);
 \draw (0,0) circle (1cm);
 \draw[very thick,red] (30:1cm) -- node[left,fill=white] {$\sin \alpha$} (30:1cm |- x axis);
 \draw[very thick,blue] (30:1cm |- x axis) -- node[below=2pt,fill=white] {$\cos \alpha$} (0,0);
 \draw (0,0) -- (30:1cm);
 \foreach \x/\xtext in {-1, -0.5/-\frac{1}{2}, 1} 
   \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north,fill=white] {$\xtext$};
 \foreach \y/\ytext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1} 
   \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east,fill=white] {$\ytext$};
 \end{tikzpicture}
\end{document}

示例 3:環面

[編輯 | 編輯原始碼]
\documentclass{article}
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}
  \draw (-1,0) to[bend left] (1,0);
  \draw (-1.2,.1) to[bend right] (1.2,.1);
  \draw[rotate=0] (0,0) ellipse (100pt and 50pt);
\end{tikzpicture}
\end{document}

示例 4:一些函式

[編輯 | 編輯原始碼]
\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}[domain=0:4] 
    \draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
    \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$}; 
    \draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
    \draw[color=red]    plot (\x,\x)             node[right] {$f(x) =x$}; 
    \draw[color=blue]   plot (\x,{sin(\x r)})    node[right] {$f(x) = \sin x$}; 
    \draw[color=orange] plot (\x,{0.05*exp(\x)}) node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
  \end{tikzpicture}
\end{document}

參考資料

[編輯 | 編輯原始碼]

https://tikz.dev/http://tug.ctan.org/info/visualtikz/VisualTikZ.pdf

前一頁:圖片 索引 下一頁:PSTricks
華夏公益教科書