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}
效果圖:
定義變量、使用函數
代碼:
繪製神經網絡圖
代碼: