PGF/TikZ 快速上手
最后编辑于: 2016-06-05
游戏名目
PGF/TikZ 是用于矢量绘图的一套语言
PGF/TikZ 的一个可行的替代是 PSTricks
基础入门
在开始之前
使用宏包
在序言部分使用 \usepackage{tikz}
来引入宏包
\begin{tikzpicture}
code
\end{tikzpicture}
code
部分为目标图形的几何和代数描述tikzpicture
环境还可以嵌套到 figure
环境中
另外一种简写方式是使用\tikz code
code
绘制直线
TikZ 默认的度量单位为厘米\draw
命令--
连接
\begin{tikzpicture}
\draw (0,0) -- (1,2);
\end{tikzpicture}
也可以顺序地连接多个点绘制折线或闭合多边形
\begin{tikzpicture}
\draw (-1,0) -- (0,1) -- (1,0) -- (0,-1) -- (-1,0);
%\draw (-1,0) -- (0,1) -- (1,0) -- (0,-1) -- cycle;
\end{tikzpicture}
也可以在最后使用 cycle
避免重复输入起点
句末的 ;
表示一条语句的结束tikzpicture
环境中
\begin{tikzpicture}
\draw (-1,0) -- (0,1) -- (1,0) -- (0,-1) -- (-1,0);
\draw (0,0) -- (1,2);
\end{tikzpicture}
网格
使用 grid
放在两个点之间
\begin{tikzpicture}
\draw (-1,0) -- (0,1) -- (1,0) -- (0,-1) -- (-1,0);
\draw [help lines] (-2,-2) grid (2,2);
\end{tikzpicture}
其中 help lines
选项表示绘制的网格为辅助线
缩放图形
在使用 tikzpicture
环境的时候可以加上 scale=
选项来设置图形的缩放
\begin{tikzpicture}[scale=1]
\draw (-1,0) -- (0,1) -- (1,0) -- (0,-1) -- (-1,0);
\end{tikzpicture}
\begin{tikzpicture}[scale=3]
\draw (-1,0) -- (0,1) -- (1,0) -- (0,-1) -- (-1,0);
\end{tikzpicture}
还可以使用 xscale=
和 yscale=
选项分别对
线型
给直线绘制语句添加不同的选项
\begin{tikzpicture}
\draw [->] (0,0) -- (4,0);
\draw [<-] (0, -2) -- (4,-2);
\draw [|->] (0,-4) -- (4,-4);
\draw [<->] (-1,1) -- (-1,-5) -- (5,-5);
\end{tikzpicture}
控制线宽:
\begin{tikzpicture}
\draw [very thin, ->] (0,0) -- (4,0);
\draw [thin, ->] (0,-1) -- (4, -1);
\draw [semithick, ->] (0,-2) -- (4,-2);
\draw [thick, ->] (0,-3) -- (4,-3);
\draw [very thick, ->] (0,-4) -- (4,-4);
\draw [ultra thick, ->] (0,-5) -- (4,-5);
\draw [line width=1cm, ->] (0,-6) -- (4,-6);
\end{tikzpicture}
注意到多个选项之间用 ,
隔开
虚线和点划线
\begin{tikzpicture}
\draw [dashed, ultra thick] (0,1) -- (2,1);
\draw [dashed] (0, 0.5) -- (2,0.5);
\draw [dotted] (0,0) -- (2,0);
\end{tikzpicture}
除了 dashed
和 dotted
densely dotted
loosely dotted
densely dashed
和 loosely dashed
颜色
\Huge red~\begin{tikzpicture}\draw [red, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge green~\begin{tikzpicture}\draw [green, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge blue~\begin{tikzpicture}\draw [blue, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge cyan~\begin{tikzpicture}\draw [cyan, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge magenta~\begin{tikzpicture}\draw [magenta, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge yellow~\begin{tikzpicture}\draw [yellow, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge black~\begin{tikzpicture}\draw [black, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge gray~\begin{tikzpicture}\draw [gray, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge darkgray~\begin{tikzpicture}\draw [darkgray, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge lightgray~\begin{tikzpicture}\draw [lightgray, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge brown~\begin{tikzpicture}\draw [brown, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge lime~\begin{tikzpicture}\draw [lime, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge olive~\begin{tikzpicture}\draw [olive, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge orange~\begin{tikzpicture}\draw [orange, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge pink~\begin{tikzpicture}\draw [pink, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge purple~\begin{tikzpicture}\draw [purple, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge teal~\begin{tikzpicture}\draw [teal, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge violet~\begin{tikzpicture}\draw [violet, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
\Huge white~\begin{tikzpicture}\draw [white, line width=0.5cm] (0,0) -- (1,0);\end{tikzpicture}
圆角:
\begin{tikzpicture}
\draw [rounded corners] (0,0) rectangle (4,4);
\end{tikzpicture}
更多形状
除了直线外
\begin{tikzpicture}
\draw [blue] (0,0) rectangle (4,4);
\draw [red, ultra thick] (2,2) circle [radius=2];
\draw (2,2) ellipse (3 and 5);
\draw [green] (4,4) arc [radius=4, start angle=45, end angle= 120];
\end{tikzpicture}
还可以绘制两点之间的曲线
\begin{tikzpicture}
\draw[very thick] (0,0) to [out=90,in=195] (2,1.5);
\end{tikzpicture}
使用 to
替换 --
out
和 in
分别表示出射角和入射角
绘制函数
除了给定确定的点来绘制线条外
\begin{tikzpicture}
\draw [help lines, <->] (0,5) -- (0,0) -- (5,0);
\draw[ultra thick, domain=0:1.5] plot (\x, {0.025+\x+\x*\x});
\draw[ultra thick, domain=0:2*pi] plot (\x, {sin(\x r)});
\draw[ultra thick, domain=0:5] plot (\x, {floor(\x)});
\end{tikzpicture}
注意到使用了 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)
rnd
e
和 pi
r
表示使用弧度
填充
使用fill
\begin{tikzpicture}
\draw [fill=red,ultra thick] (0,0) rectangle (2,2);
\draw [fill=red,ultra thick,red] (3,0) rectangle (5,2);
\draw [blue, fill=blue] (6,0) -- (6,2) -- (8,0) -- (6,0);
\draw [fill] (2,-2) circle [radius=0.5];
\draw [fill=orange] (9,0) rectangle (11,1);
\draw [fill=white] (9,0.25) rectangle (10,1.5);
\draw [ultra thick, fill=purple] (5,-2) to [out=87,in=150] (7,-1) -- (7,-1.5) -- (5,-2);
\end{tikzpicture}
标注
使用 node
可以在指定点区域加上标注文字below
above
left
right
及其组合来控制相对位置
\begin{tikzpicture}
\draw [thick, <->] (0,4) -- (0,0) -- (4,0);
\draw[fill] (2,3) circle [radius=0.05];
\node [below] at (2,3) {below};
\node [above] at (2,3) {above};
\node [left] at (2,3) {left};
\node [right] at (2,3) {right};
\draw[fill] (2,1) circle [radius=0.05];
\node [below right, red] at (2,1) {below right};
\node [above left, green] at (2,1) {above left};
\node [below left, purple] at (2,1) {below left};
\node [above right, magenta] at (2,1) {above right};
\node [below right] at (4,0) {$\mathrm{x}$};
\node [left] at (0,4) {$\mathrm{y}$};
\end{tikzpicture}
进阶使用
样式定义
TikZ 可以对单个tikzpicture
\begin{tikzpicture}[
myline/.style={line width=2pt},
myblueline/.style={myline, blue}]
\draw [myline] (0,0) -- (4,0);
\draw [myblueline] (0,1) -- (4,1);
\end{tikzpicture}
或者使用tikzset
\tikzset{
myline/.style={line width=2pt},
myblueline/.style={myline, blue}
}
\begin{tikzpicture}
\draw [myline] (0,0) -- (4,0);
\draw [myblueline] (0,1) -- (4,1);
\end{tikzpicture}
几何变换
除了使用 scale
控制尺寸缩放外shift
)slant
)rotate
)rotate around
) 等几何变换
\begin{tikzpicture}
\draw (0,0) rectangle (2,2);
\draw[shift={(3,0)},scale=1.5] (0,0) rectangle (2,2);
\draw[xshift=70pt,xscale=1.5] (0,0) rectangle (2,2);
\draw[xshift=125pt,rotate=45] (0,0) rectangle (2,2);
\draw[xshift=140pt,xslant=1] (0,0) rectangle (2,2);
\draw[xshift=175pt,rotate around={45:(2,2)}] (0,0) rectangle (2,2);
\end{tikzpicture}
一点思考
做了这么多笔记
参考资料
If it is not in these notes, it is most probably somewhere in the manual.
-
“TikZ is not a drawing program” ↩