tikz原创
# tikz 代码结构体
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage{tikz } %导言区调用tikz宏包
\usetikzlibrary{graphs, arrows}
\begin{document}
\begin{tikzpicture}[> = stealth] %[] 中括号内定义的内容对整个 tikzpicture 有效。 > = stealth 该表箭头的样式,有 stealth、latex等样式
% [] scale= 是对图形进行缩放,不会缩放文字!
……
tikz画图的代码
……
\end{tikzpicture}
\end{document}
# draw
画图
\draw(0,0)--(4,0); % 从0,0 到 4,0 的直线
\draw[->](0,0)--(4,0); % 从0,0 到 4,0 的带箭头直线
\draw[<-](0,1)--(4,1); % 从4,1到 0,1 的带箭头直线
\draw[<->](0,2)--(4,2); % 从0,2 到 4,2 的带双箭头直线
\draw[|<->|](0,3)--(4,3); % 从0,3 到 4,3 的带双箭头终止符线段
\draw[|<->|](0,4) to (4,4); % 从0,4 到 4,4 的带双箭头终止符线段,可以用 to 代替 --
\draw[dashed](0,5) to (4,5); % 从0,5 到 4,5 的虚线
\draw[dotted](0,5) to (4,5); % 从0,5 到 4,5 的点线
\draw[dashdotted](0,6) to (4,6); % 从0,6 到 4,6 的点虚线
\draw[|<->|,dashed](0,7) to (4,7); % 从0,7 到 4,7 的带箭头虚线
\draw[->](0,0)to[bend left=30] (4,0); % 使用 bend 能是直线弯曲, 30为度数
% 加粗线条 在中括号中添加 thick、very thick、ultra think等;线条变细使用 thin
# node
\draw[|<->|](0,3)--node{5cm}(4,3); % {}的内容为显示的文本,默认显示在线条上。特别注意:不能使用汉字文本
\draw[|<->|](0,3)--node[fill=white]{5cm}(4,3); % 文字填充白色背景
\draw[|<->|](0,3)--node[fill=red!20!white]{5cm}(4,3); % 20%的红色与白色调配后的颜色 调用xcolor 宏包
\draw[|<->|](0,3)--node[above]{up}(4,3); % []控制结点显示的位置,above 上方;below 下方;left 左侧; right 右侧
\draw[|<->|](0,3)--node[below]{dwon}(4,3);
\draw[|<->|](0,3)--node[left]{left}(4,3);
\draw[|<->|](0,3)--node[left=3mm]{right}(4,3); % 控制显示位置的距离
\draw[|<->|](0,5)node{one}--(4,5); % node 位置不同显示也不同
\draw[|<->|](0,6)--node{two}(4,6); % node 位置不同显示也不同
\draw[|<->|](0,7)--(4,7)node{three}; % node 位置不同显示也不同
\node at (-.2,-.2){$O$}; %标记一个单独的点
# 坐标轴
# 平面坐标轴
\draw[->] (-1,0)--(4,0)node[right]{$x$};
\draw[->] (0,-1)--(0,4)node[right]{$y$};
\node at (-.2,-.2){$O$};
# 极坐标
tikz
中使用(θ:0)
来表示。但是这里的 θ(西塔) 表示的是角度,而不是弧度,取值为0~360。
# foreach
例如画带刻度的坐标系,建立箭头使用 latex 样式
\draw[->] (-1,0)--(4,0)node[right]{$x$};
\draw[->] (0,-1)--(0,4)node[right]{$y$};
\node at (-.2,-.2){$O$};
\foreach \x in {1,2,3} % {1,2,...,6} 1到6;这里其实是以集合的形式来表示的,如{2,6,8}等
{
\draw (0,\x)node[left]{$\x$}--(.1,\x);
\draw (0,\x*0.5)node[left]{$\x$}--(.1,\x*0.5); % 可以对 \x 进行 数学二运算
\draw (\x,0)node[below]{$\x$}--(\x,.1);
}
# 菱形
\draw (-4,0)node[above]{$C$}--(0,3)node[right]{$B$}--(4,0)node[above]{$A$}--(0,-3)node[right]{$D$}--(-4,0); % 注意 node放置的位置与文字文字的控制
\draw [fill=blue](-4,0)node[above]{$C$}--(0,3)node[right]{$B$}--(4,0)node[above]{$A$}--(0,-3)node[right]{$D$}--(-4,0); % 填充颜色
# 坐标轴中的菱形
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage{tikz } %导言区调用tikz宏包
\begin{document}
\begin{tikzpicture}[> = latex, scale = .5]
\draw[->] (-6,0)--(6,0)node[right]{$x$};
\draw[->] (0,-6)--(0,6)node[right]{$y$};
\node at (-.2,-.2){$O$};
\foreach \x in {1,2,...,5}
{
% \draw (0,\x)node[left]{$\x$}--(.1,\x);
\draw (0,\x)node[left]{$\x$}--(.1,\x); % 可以对 \x 进行 数学二运算
\draw (\x,0)node[below]{$\x$}--(\x,.1);
}
\draw [fill=blue](-4,0)node[above]{$C$}--(0,3)node[right]{$B$}--(4,0)node[above]{$A$}--(0,-3)node[right]{$D$}--(-4,0); % 注意 node放置的位置与文字文字的控制
\end{tikzpicture}
\end{document}
调整画图顺序,调整图层顺序。
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage{tikz } %导言区调用tikz宏包
\begin{document}
\begin{tikzpicture}[> = latex, scale = .5]
\draw [fill=blue](-4,0)node[above]{$C$}--(0,3)node[right]{$B$}--(4,0)node[above]{$A$}--(0,-3)node[right]{$D$}--(-4,0); % 注意 node放置的位置与文字文字的控制
\draw[->] (-6,0)--(6,0)node[right]{$x$};
\draw[->] (0,-6)--(0,6)node[right]{$y$};
\node at (-.4,-.4){$O$};
\foreach \x in {1,2,...,5}
{
% \draw (0,\x)node[left]{$\x$}--(.1,\x);
\draw (0,\x)node[left]{$\x$}--(.1,\x); % 可以对 \x 进行 数学二运算
\draw (\x,0)node[below]{$\x$}--(\x,.1);
}
\end{tikzpicture}
\end{document}
# 矩形
\draw [fill=red] (0,0) rectangle (4,3); % 由 2个点作为对角顶点所组成的矩形,但是填充颜色时,会有边框
\fill [cyan] (0,0) rectangle (4,3); % 这样的矩形无边框
\fill [cyan,draw=black] (0,0) rectangle (4,3); % 添加黑色边框
\draw[red] (2,1) -| (1,2);%直角1
\draw[blue] (2,1)|-(1,2);%直角2
# 圆形圆弧椭圆
\draw[fill=green] (0,0) circle (1.5); % 圆心坐标 0,0 ,半径 1.5
%\draw (0,0)--(45:4); % 直线 (45:4) tikz 中极坐标的标识方法,45位度数,4为长度。
\draw (0,0) arc (0:45:2); % arc 绘画圆弧, 起点角度为0度,终点角度为45度,圆弧半径为2.
\draw [->] (0,0) arc (0:45:2);
\draw [gray] (6,0) arc [radius=1, start angle=45, end angle= 120];
\draw[black] (4,4) ellipse (1 and 3);%椭圆:圆心 4,4 短、长半轴
# 图形阴影样式
需要在导言区引入库patterns
。
\usetikzlibrary{patterns}
\draw [pattern= north east lines] (0,0) rectangle (4,3); % pattern 从北到西的线
# 函数
导言区调用amsmath
宏包用于数学符号。
# 抛物线
\usepackage{amsmath} %导言区调用amsmath宏包 用于数学符号
\draw[->] (-6,0)--(6,0)node[right]{$x$};
\draw[->] (0,-2)--(0,10)node[right]{$y$};
\node at (-.4,-.4){$O$};
\draw [domain=-4:4, samples=1000, thick] plot(\x,{0.5*\x*\x}); % domain 定义域;samples 点的数量;plot 点的函数表达式,第一个为x取值,第二个为函数式即y取值
\node at (4.5,6){$y=\dfrac{1}{2}x^2$}; % dfrac 需要导言区调用amsmath宏包
# 三角函数
特别注意的是,tikz
中都是用角度来进行绘图。在tikz
中若需将角度转化为弧度,需要使用(\x r)
,添加一个r
即可。
点击查看
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage{amsmath} %导言区调用amsmath宏包 用于数学符号
\usepackage{tikz} %导言区调用tikz宏包
% \usepackage{pgfplots}
% \pgfplotsset{compat=1.8}
\begin{document}\begin{tikzpicture}[help lines/.style={black!50,very thin}]
% x轴
\draw[<->,thick] (-4.25,0)--(4.25,0) node[right] {$x$};
% x轴数据
\foreach \x / \r in {-4/-2\pi,-3/-1\frac{1}{2}\pi,-2/-\pi,-1/-\frac{1}{2}\pi,+1/+\frac{1}{2}\pi,+2/+\pi,+3/+1\frac{1}{2}\pi,+4/+2\pi} \draw (\x,-0.15) -- (\x,+0.15) node[below=7] {$\r$};
% y轴
\draw[<->,thick] (0,-4.25)--(0,4.25) node[above] {$y$};
% y轴数据
\foreach \y in {-4,-3,-2,-1,+1,+2,+3,+4} \draw (-0.15,\y) -- (+0.15,\y) node[left=7] {$\y$};
% 辅助线
% \draw[help lines] (-4.25,-4.25) grid (4.25,4.25);
\draw[very thick,color=red ] plot [domain={(-2*pi)*(2/pi))}:{(+2*pi)*(2/pi)},smooth] (\x,{sin(\x*pi/2 r)});
\draw[very thick,color=blue] plot [domain={(-2*pi)*(2/pi))}:{(+2*pi)*(2/pi)},smooth] (\x,{cos(\x*pi/2 r)});
\foreach \i in {0,...,4} \draw[very thick,color=green] plot [domain={((\i-2)*pi-rad(atan(4))*ceil(\i/4))*(2/pi)}:{((\i-2)*pi+rad(atan(4))*ceil((4-\i)/4))*(2/pi)},smooth] (\x,{tan(\x*pi/2 r)});
\node[red ] at (5,3) {$y = \sin x$};
\node[blue] at (5,2) {$y = \cos x$};
\node[green] at (5,1) {$y = \tan x$};
\foreach \i in {0,...,3} \draw[very thick,color=purple ] plot [domain={((\i-2)*pi+rad(asin(1/4)))*(2/pi)}:{((\i-1)*pi-rad(asin(1/4)))*(2/pi)},smooth] (\x,{cosec(\x*pi/2 r)});
\foreach \i in {0,...,4} \draw[very thick,color=olive] plot [domain={((\i-2)*pi-rad(acos(1/4))*ceil(\i/4))*(2/pi)}:{((\i-2)*pi+rad(acos(1/4))*ceil((4-\i)/4))*(2/pi)},smooth] (\x,{sec(\x*pi/2 r)});
\foreach \i in {0,...,3} \draw[very thick,color=cyan] plot [domain={((\i-2)*pi+rad(atan(1/4)))*(2/pi)}:{((\i-1)*pi-rad(atan(1/4)))*(2/pi)},smooth] (\x,{cot(\x*pi/2 r)});
\node[purple ] at (5,-1) {$y = \csc x$};
\node[olive] at (5,-2) {$y = \sec x$};
\node[cyan] at (5,-3) {$y = \cot x$};
\end{tikzpicture}
\end{document}
# 取整函数
点击查看
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage[utf8]{inputenc}
\usepackage{amsmath} %导言区调用amsmath宏包 用于数学符号
\usepackage{tikz} %导言区调用tikz宏包
\usepackage{tkz-base, tkz-euclide} %导言区调用tikz宏包
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[> = latex, scale = 1.2]
\draw[->] (-4,0)--(4,0)node[right]{$x$};
\draw[->] (0,-4)--(0,4)node[right]{$y$};
\node at (-.2,-.2){$O$};
\foreach \x in {1,2,3} % {1,2,...,6} 1到6;这里其实是以集合的形式来表示的,如{2,6,8}等
{
\draw (0,\x)node[left]{$\x$}--(.1,\x);
\draw (0,-\x)node[left]{$\x$}--(.1,-\x);
\draw (\x,0)node[below]{$\x$}--(\x,.1);
\draw (-\x,0)node[below]{$\x$}--(-\x,.1);
}
\draw[dashed] (-2,0)to(-2,-2);
\draw[dashed] (-1,0)to(-1,-1.9);
\draw[dashed] (1,0)to(1,.9);
\draw[dashed] (2,0)to(2,1.9);
\draw[dashed] (3,0)to(3,1.9);
\draw (-2,-2)--(-1,-2);
\draw[fill=white] (-1,-2) circle (2pt);
\draw (-1,-1)--(0,-1);
\draw[fill=white] (0,-1) circle (2pt);
\draw[fill=white] (1,0) circle (2pt);
\draw (1,1)--(2,1);
\draw[fill=white] (2,1) circle (2pt);
\draw (2,2)--(3,2);
\draw[fill=white] (3,2) circle (2pt);
\end{tikzpicture}
\end{document}
# 指数函数
点击查看
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage{amsmath} %导言区调用amsmath宏包 用于数学符号
\usepackage{tikz} %导言区调用tikz宏包
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[> = latex, scale = 1.2]
\draw[->] (-4,0)--(3,0)node[right]{$x$};
\draw[->] (0,-1)--(0,4)node[right]{$y$};
\draw(1,0)--(1,.1)node[below=5pt]{$1$};
\node at (-.2,-.2){$O$};
\draw [color=red, domain=-3.5:2, smooth, very thick] plot(\x,{2^(\x)}) node[right] {$f(x)=2^{\: x}$}; %注意 指数函数的值域不得小于0!! smooth 平滑曲线
\draw [color=pink, domain=-3.5:1.35, smooth, very thick] plot(\x,{3^(\x)}) node[right] {$f(x)=3^{\: x}$};
\draw [color=blue, domain=-2:2.5, smooth, very thick] plot(\x,{(1/2)^(\x)}) node[above] {$f(x)=\frac{1}{2}^{\: x}$};
\draw [color=purple, domain=-1.3:2.5, smooth, very thick] plot(\x,{(1/3)^(\x)}) node[below=5pt] {$f(x)=\frac{1}{3}^{\: x}$};
\end{tikzpicture}
\end{document}
# 对数函数
笔记
tikz
中使用 log10(x)/log10(3)
类似同低相除进行绘图。
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage{amsmath} %导言区调用amsmath宏包 用于数学符号
\usepackage{tikz} %导言区调用tikz宏包
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[> = latex, scale = 1.2]
\draw[->] (-1,0)--(6,0)node[right]{$x$};
\draw[->] (0,-3)--(0,4)node[right]{$y$};
\draw(1,0)--(1,.1)node[below=5pt]{$1$};
\node at (-.2,-.2){$O$};
\draw [color=red, domain=0.15:3, samples=1000, very thick] plot(\x,{log2(\x)}) node[right] {$f(x)=log_{ \:2}x$}; %注意 对数函数的定义域不得小于0!!
\draw [color=blue, domain=.15:3, samples=1000, very thick] plot(\x,{ln(\x)/ln(3)}) node[right] {$f(x)=log_{ \:3}x$}; % log_3x = log10(x)/log10(3) 换底公式
\draw [color=pink, domain=.15:3, samples=1000, very thick] plot(\x,{log10(\x)/log10(1/2)}) node[right] {$f(x)=log_{ \:\frac{1}{2}}x$};
\draw [color=purple, domain=.15:3, samples=1000, very thick] plot(\x,{log10(\x)/log10(1/3)}) node[right] {$f(x)=log_{ \:\frac{1}{3}}x$};
\end{tikzpicture}
\end{document}
# 幂函数
点击查看
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage{amsmath} %导言区调用amsmath宏包 用于数学符号
\usepackage{tikz} %导言区调用tikz宏包
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[> = latex, scale = 1.2]
\draw[->] (-4,0)--(4,0)node[right]{$x$};
\draw[->] (0,-4)--(0,4)node[right]{$y$};
\node at (-.2,-.2){$O$};
\draw [color=red, domain=-2:2, smooth, very thick] plot(\x,{(\x)^2}) node[right] {$f(x)={\: x}^2$}; % smooth 平滑曲线
\draw [color=blue, domain=-1.6:1.6, smooth, very thick] plot(\x,{(\x)^3}) node[above] {$f(x)={\: x}^3$};
\draw [color=orange, domain=-3.5:3.5, smooth, very thick] plot(\x,{(\x)^0}) node[below] {$f(x)={\: x}^0$};
\draw [color=olive, domain=-3.5:3.5, smooth, very thick] plot(\x,{(\x)^1}) node[right] {$f(x)={\: x}^1$};
\draw [color=purple, domain=-3.5:3.5, smooth, very thick] plot(\x,{\x^(1/2)})node[right]{$f(x)={\: x}^{\frac{1}{2}}$};
\draw [color=magenta, domain=0.25:3.5, smooth, very thick] plot(\x,{\x^(-1)})node[right]{$f(x)={\: x}^{-1}$};
\draw [color=magenta, domain=-3.5:-0.25, smooth, very thick] plot(\x,{\x^(-1)});
\end{tikzpicture}
\end{document}
# 梯形
需要引入额外的红包tkz-base
,tkz-euclide
,这2个也是基于tikz
的。
点击查看
\documentclass[dvisvgm]{standalone} %生成使用dvisvgm生成SVG,需要配合standalone(文档类使用)
\usepackage[utf8]{inputenc}
\usepackage{amsmath} %导言区调用amsmath宏包 用于数学符号
\usepackage{tikz} %导言区调用tikz宏包
\usepackage{tkz-base, tkz-euclide} %导言区调用tikz宏包
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[> = latex, scale = 1.2]
\tkzDefPoints{-2.5/0/B, 2.5/0/C, -1.5/3/A, 1.5/3/D, 0/1.5/O} % 定义点 变量 语句结尾不用加分号
\tkzDefMidPoint(A,B) \tkzGetPoint{E} % 定义AB中点 变量E
\tkzDefMidPoint(C,D) \tkzGetPoint{F} % 定义CD中点 变量F
\tkzDrawPolygon(A,B,C,D) % DrawPolygon 绘制多边形
\draw(B)to(F)node[right]{$F$};
\draw(C)to(E)node[left]{$E$};
% \tkzAutoLabelPoints[center=O]
\tkzAutoLabelPoints[center=O](A,B,C,D) % tkz 根据中心点自动标记点名称
\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}
# 蓝色矩形、红色圆、弧
\begin{tikzpicture}
\draw [blue] (0,0) rectangle (1.5,1);
\draw [red, ultra thick] (3,0.5) circle [radius=0.5];
\draw [gray] (6,0) arc [radius=1, start angle=45, end angle= 120];
\end{tikzpicture}
\draw[orange] (0,0) -- (2,1-|1,2);%找到垂点并与(0,0)连线
\draw[purple] (0,1)--(1,1.5)--(0,2)--cycle (0,2)--(1,3);%封闭的线段不加分号的连写
上次更新: 2022/08/23, 18:12:45