TIKZ是LaTeX的一個繪圖包,可以繪制其他軟件很難畫出來的圖像。
基本用法
直線、垂足、矩形、橢圓
代碼:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}%畫箭頭用的包
\begin{document}
\begin{tikzpicture}
\draw[->] (0,0)--(7,0);
\draw[->] (0,0)--(0,7); %箭頭線
\draw[red] (2,1) -| (1,2);%直角1
\draw[blue] (2,1)|-(1,2);%直角2
\draw[green] (2,2) circle (1);%圓:圓心、半徑
\draw[black] (4,4) ellipse (1 and 3);%橢圓:短、長半軸
\draw[yellow] (3,3) rectangle (4,1);%矩形
\draw[orange] (0,0) -- (2,1-|1,2);%找到垂點並與(0,0)連線
\draw[purple] (0,1)--(1,1.5)--(0,2)--cycle %封閉的線段
(0,2)--(1,3);%不加分號的連寫
\end{tikzpicture}
\end{document}
效果圖:
圓弧、橢圓弧、三角函數曲線、貝塞爾曲線
代碼:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}%畫箭頭用的包
\begin{document}
\begin{tikzpicture}
\draw[->] (0,0)--(7,0);
\draw[->] (0,0)--(0,7); %箭頭線
\draw[red] (3,2) arc (0:120:1);%圓弧:起始點,開始角度:結束角度:半徑
\draw[blue] (3,2) arc (0:120:1 and 2);%橢圓弧
\draw[green] (0,0) sin (3,1) cos (5,0);%畫pi/2的正弦、余弦
\draw[orange] (3,3)..controls (4,4)and(5,5) .. (4,3); %貝塞爾曲線:起點..controls 控制點 and 控制點..終點
\end{tikzpicture}
\end{document}
效果圖:
輔助線、網格、填充、自定義函數
代碼:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}%畫箭頭用的包
\begin{document}
\begin{tikzpicture}
\draw[help lines,step = 0.5] (-3,-3) grid (3,3); %輔助線格子
\draw[-latex] (-4,0) -- (4,0);
\draw[-latex] (0,-4) -- (0,4);%實心箭頭
\draw[domain = -2:360][samples = 200] plot({cos(\x)}, {0.5*sin(\x)});%函數圖像,參數方程,內有小括號,外面必須用花括號括起來,samples是畫函數圖時列出的點
\filldraw[fill = yellow,draw = blue][ultra thick] (2,2) circle (1);%填色,還可以設置線寬
\end{tikzpicture}
\end{document}
效果圖:
圖形旋轉、平移、縮放、變形、圓角多邊形、箭頭設置
代碼:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}%畫箭頭用的包
\begin{document}
\begin{tikzpicture}[>=Stealth]%設置箭頭,環境中所有箭頭都用這個庫
\draw[->] (-4,0) -- (4,0);
\draw[->>] (0,-4) -- (0,4);
\draw(-3,2)--(-3,3)[rounded corners = 0.3cm]
-- (-2,3)--(-1.5,2)[sharp corners]--(-2.5,1)--cycle;%圓角多邊形
\draw[help lines](0,0) rectangle (1,1);%輔助線
\draw[scale=1.5] (0,0) rectangle (1,1);%縮放
\draw[rotate=30] (0,0) rectangle (1,1);%擾起點旋轉
\draw[help lines](2,0) rectangle (3,1);
\draw[shift={(0.5,0.5)}](2,0) rectangle (3,1);%平移
\draw[help lines](4,0) rectangle (5,1);
\draw[xslant=0.4](4,0) rectangle (5,1);%傾斜
\end{tikzpicture}
\end{document}
效果圖:
錨點、自定義屬性
代碼:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{arrows.meta}%畫箭頭用的包 \begin{document} \begin{tikzpicture} \draw[->] (-4,0) -- (4,0); \draw[->>] (0,-4) -- (0,4); \node (A) at (2,2) {$A_1$}; \node[draw] (B) at (3,2) {B};%draw屬性添加自適應大小的方框 \node[draw] (C) at (2.5,3) {}; \draw (A) -- (B.center) -- (C.north) -- (A);%連線可以連到字母方框的某個位置:center/north/south/east/west \node[draw,anchor=east] (a) at (A) {$a_0$};%定義A的錨點在A位置的東部,然后定義a,並將其位置定義為A位置的中心 \node[draw,below right=4pt] (b) at (B) {b};%在相對於B的位置右下方4pt的地方定義b \node[circle,fill=blue,text=white,font={\bfseries}] (A) at (0,0) {A node};%填充、各種可自定義的參數 \node[rectangle,rounded corners,draw=gray,font={\sffamily\slshape}] (B) at (2,0) {B node}; \node[draw] (P) at (-3,3){center}; \draw[dotted] (0,0)--(P.south);%畫某點到某點的南邊的線 \end{tikzpicture} \end{document}
效果圖:
線段與點的標注
代碼:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{arrows.meta}%畫箭頭用的包 \begin{document} \begin{tikzpicture} \draw (2,1.5)coordinate (A)node[above] {$A$}%定義點A並標注A --node[above left,sloped] {$c$}%給線標注c (0,0)coordinate (B)node[left] {$B$} --node[below]{$a$} (2.5,0)coordinate (C)node[right]{$C$} --node[right]{$b$} cycle; \end{tikzpicture} \end{document}
效果圖:
例子
代碼:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{arrows.meta}%畫箭頭用的包 \begin{document} \begin{tikzpicture}[>=Stealth] \draw[->,line width=0.2pt](-0.5,0)--(4.5,0); \draw[->,line width=0.2pt](0,-0.5)--(0,2.5); \coordinate (a) at (0.5,1.9); \coordinate (b) at (4,1.2); \coordinate (a0) at (a |- 0,0); \coordinate (b0) at (b |- 0,0); \node[below] at (a0) {$a$}; \node[below] at (b0) {$b$}; \filldraw[fill=gray!50,draw,thick] (a0)--(a)..controls(1,2.8)and(2.7,0.4)..(b)--(b0)--cycle; \node[above right,outer sep=0.2cm,rounded corners,fill = green!20,draw = black,text = blue!60!red,scale = 0.6] %blue60,red40 at (b) {$\displaystyle\int_a^bf(x)dx = F(b)-F(a)$};%寫標注,draw邊框,fill填充,scale字體大小 \end{tikzpicture} \end{document}
效果圖:
用循環、判斷語句繪圖
代碼:
\documentclass{article} \usepackage{tikz} \usepackage{pgffor}%可以使用foreach的包 \usepackage{ifthen}%可以使用ifthenelse的包,還能使用whiledo \begin{document} \begin{tikzpicture} \foreach \i in {0,...,5}{ \foreach \j in {0,...,\i}{ \ifthenelse{\i > 3}{%if成立 \node[fill = green!20,rounded corners]at (\i,\j) {(\i,\j)}; }{%if不成立 \node[fill = red!20,rounded corners]at (\i,\j) {(\i,\j)}; } } } \end{tikzpicture} \end{document}
效果圖:
定義變量、使用函數
這個庫可以讓你使用過程語言結合LaTeX的畫圖腳本的來畫圖,十分方便。主要就是導入math庫:
\usetikzlibrary{math}%使用數學程序
\usetikzlibrary{fpu}%修理不能使用ifthenelse的錯誤
代碼:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{fpu}%修理不能使用ifthenelse的錯誤!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \usetikzlibrary{math}%使用數學程序 \begin{document} \begin{tikzpicture} \tikzmath{ %數學程序編寫,定義的變量可以在其外部使用,里面所有語句都要加分號; 並且不能有多余的回車 function paint_loop(\x,\y){ %函數中只能使用局部變量,外部變量不能用 for \i in {0,1,...,\x}{ for \j in {0,...,\y}{ int \ii,\jj; \ii = \i*1.5; \jj = \j; if \ii > 2 then {%條件分支語句 {%繪圖代碼在數學程序內部使用,要用畫括號括起來!!!! \node[draw,fill = green!20,rounded corners] at (\ii,\jj) {(\ii,\jj)}; }; }else{ { \node[draw,fill = red!20,rounded corners] at (\ii,\jj) {(\ii,\jj)}; }; }; }; }; }; \a = 5; %和python一樣,加個.就是實數,不加點就是小數 \b = 3; paint_loop(\a,\b); coordinate \co;%能自定義的變量有int、real、coordinate(坐標),不能在定義的時候賦值 \co = (3,4.5); } \draw[-latex] (-1,0) -- (5,0); \draw[-latex] (0,-1) -- (0,5); \node at (\co) {COCOCO}; %在tikzmath中定義的變量在能外部使用 \end{tikzpicture} \end{document}
效果圖:
繪制神經網絡圖
代碼:
\documentclass{article} \usepackage{tikz} \usetikzlibrary{fpu}%修理不能使用ifthenelse的錯誤!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \usetikzlibrary{math}%使用數學程序 \begin{document} \begin{tikzpicture} \tikzmath{ function paint_nodes(\radius,\gapy,\posx,\num){ \gapy = \gapy+\radius*2; \starty = \gapy*(\num-1)/2; for \i in {0,...,\num-1}{ \drawy = \starty - \i*\gapy; { \filldraw[line width = 0.5pt,fill = white] (\posx,\drawy) circle (\radius); }; }; }; function paint_lines(\radius,\gapy,\posx,\num,\nextposx,\nextnum){ \gapy = \gapy+\radius*2; \starty = \gapy*(\num-1)/2; \startyy = \gapy*(\nextnum-1)/2; for \i in {0,...,\num-1}{ \drawy = \starty - \i*\gapy; for \j in {0,...,\nextnum-1}{ \drawyy = \startyy - \j*\gapy; { \draw (\posx,\drawy) -- (\nextposx,\drawyy); }; }; }; }; function paint_x_lines(\radius,\gapy,\posx,\num,\ifright,\len){ \gapy = \gapy+\radius*2; \starty = \gapy*(\num-1)/2; for \i in {0,...,\num-1}{ \drawy = \starty - \i*\gapy; if \ifright == 1 then{ { \draw[-latex] (\posx,\drawy) -- (\posx+\len,\drawy); }; }else{ { \draw[-latex] (\posx,\drawy)--(\posx-\len,\drawy); }; }; }; }; function paint_net(\x0,\x1,\x2,\x3){ \gapx = 2; \radius = 0.3; \gapy = 0.2; paint_lines(\radius,\gapy,0*\gapx,\x0,1*\gapx,\x1); paint_lines(\radius,\gapy,1*\gapx,\x1,2*\gapx,\x2); paint_lines(\radius,\gapy,2*\gapx,\x2,3*\gapx,\x3); paint_x_lines(\radius,\gapy,3*\gapx,\x3,1,1.8); paint_x_lines(\radius,\gapy,0*\gapx-1,\x0,1,1); paint_nodes(\radius,\gapy,1*\gapx,\x1); paint_nodes(\radius,\gapy,2*\gapx,\x2); paint_nodes(\radius,\gapy,3*\gapx,\x3); }; paint_net(7,9,10,5); } \node[scale = 0.9] at (0,-4.2) {Feature}; \node[scale = 0.9] at (2,-4.2) {Input layer}; \node[scale = 0.9] at (4,-4.2) {Hide layer}; \node[scale = 0.9] at (6,-4.2) {Output layer}; \end{tikzpicture} \end{document}
效果圖: