写在前面
好久没用(
ist kein Zeichenprogramm, pgf的前端, 由Till Tantau等开发)绘图包画图了, 快毕业时候闲来无事, 折腾了一下
画图包, 做了一些简单的绘图并发布在了我, 感兴趣的话可以看一下. 最近想着总结一下基本的画图语法, 以便之后作图使用.
这次我先写一下关于中基本图形的绘制, 包括定点, 线, 圆与椭圆, 交点的描述等. 内容主要参考
pgfmanual
这本书, 使用texdoc pgf
就可以调出这本书(最好更到最新版).
P.S. 才发现自己原来断断续续啃了这本巨型文档(约1300页)的前面几章, 赶紧记录一下, 不然又要忘记了. (放到现在已经直接放弃了)
测试环境:
MacOS 12 Monterey Apple Silicon
MacTeX 2021 Universal
pgf/: 3.1.9a (Sublime Text 4)
一些可能用到的基础知识
学习其实不需要太多的
基础, 只需要掌握基本命令就够了, 下面来介绍这些基本的命令.
文档类
其实在任何文档类中都可以实现, 只要导入了
包, 就可以创建任何你想要的矢量图形(有一些需要使用命令
\usetikzlibrary{}
命令来导入支持的一些子包), 不过, 这里还是推荐用
standalone
文档类, 设置边距为5pt
, 这样能够很好地看到所绘制图形的效果, 生成为PDF之后再插入主文档, 也显得工整简洁一些. 下面这条命令直接省去了\usepackage{tikz}
, 更加方便直接.
\documentclass[tikz,border=5pt]{standalone}
环境
这里主要用到了tikzpicture
这个环境, 事实上使用单行命令\tikz
也能达到绘图的效果, 不过一般的图形肯定不止一条命令的, 还是用环境比较好, 对于一些参数的设置也更为直观.
\begin{tikzpicture}[<这里设定绘图程序的一些参数>]
\end{tikzpicture}
基本绘图命令
第一个要讲的的基本绘图命令是
\path
,这个命令还有很多的缩写(abbreviation), 就是我们最为熟悉的\draw
, \node
等等, 其实本质上都是\path
命令加上一些参数得到的. 下面我用几个示例来介绍一下由\path
衍生出的一些基本绘图命令.
基本图形
这里面的基本图形, 指的是平面几何中常见到的基本图形. 我们先从点开始说起.
点(coordinate)
关于点的绘制, 还是比较简单的, 实现的主要细节在于坐标系的选取, 这里我们先介绍
\coordinate
这个命令,
其基本调用格式如下
\coordinate[<options>] (<name>) at (<coordinate>);
% 下面是两个等价命令
\path ... coordinate[⟨options⟩] (⟨name⟩) at (⟨coordinate⟩) ...;
\node[shape=coordinate][⟨options⟩] (⟨name⟩) at (⟨coordinate⟩){};
我比较喜欢第一种, 简洁明了, 能够以一种自然语言形式直观显示坐标点的创建(标记), 其含义是将<coordinate>处的点标记为<name>
.
下面是一个具体的例子, 用来创建A,B,C 三个点, 并且加上了这三个点的label, 这里需要使用label=
参数,后面加上位置参数:$标记内容$
.
\documentclass[tikz,border=3pt]{standalone}
\begin{document}
\begin{tikzpicture}[]
% 标记点A: 使用三种命令
\coordinate[label=left:{$A$}] (A) at (0,2);
% \path (0,2) coordinate[label=left:{$A$}] (A);
% \node[shape=coordinate][label=left:$A$] (A) at (0,3){};
% 标记点B, C
\coordinate[label=right:{$B$}] (B) at (5,2);
\coordinate[label=below right:{$C$}] (C) at (5,0);
\end{tikzpicture}
\end{document}
示例如下:
线(–)
描好了点, 下面就开始进行连线了, 最简单的一条绘图命令就是\draw
了, 能够绘制很多基本的图形, 下面是一些命令的调用格式.
\draw ... --⟨coordinate or cycle⟩ ...;
\path[draw] ... --⟨coordinate or cycle⟩ ...;
这里先以线段为例, 我们将上面的三个点中的AB,AC连起来,
\documentclass[tikz,border=3pt]{standalone}
\begin{document}
\begin{tikzpicture}[]
% 标记点A, B, C
\coordinate[label=left:{$A$}] (A) at (0,2);
\coordinate[label=right:{$B$}] (B) at (5,2);
\coordinate[label=below right:{$C$}] (C) at (5,0);
% 绘制线段AB, AC
\draw (A) -- (B);
\draw (A) -- (C);
\end{tikzpicture}
\end{document}
由于我们之前已经标记了点ABC,这里连线时只需要直接放上标记的名称即可. 示例图如下:
垂线(-|,|-)
这里再介绍一种十分常用的线, 即垂线. 跟上面连点画线的方法一样, 只需要更改线型即可, 即
% 先水平后垂直
\path ... -|⟨coordinate or cycle⟩ ...;
% 先垂直后水平
\path ... |-⟨coordinate or cycle⟩ ...;
这里我们还以上面的图形为例, 做一个垂线出来:
\documentclass[tikz,border=3pt]{standalone}
\begin{document}
\begin{tikzpicture}[]
% 标记点A, B, C
\coordinate[label=left:{$A$}] (A) at (0,2);
\coordinate[label=right:{$B$}] (B) at (5,2);
\coordinate[label=below right:{$C$}] (C) at (5,0);
% 绘制线段AB, BC
\draw (A) -| (C);
\end{tikzpicture}
\end{document}
示例如下:
圆和椭圆(circle,ellipse)
这里我们介绍几种画圆的方法, 第一种也是最基本的一种就是像连点成线那样, 直接使用\draw
命令, 只需要将线型改成circle
,在第二参数中加上半径即可. 这种方法需要给定点(圆心)和半径(单位默认: cm).
\draw (<coordinate>) circle (<radius>);
% 等价命令
\draw (<coordinate>) circle [radius=<radius>];
绘制命令:
\documentclass[tikz,border=3pt]{standalone}
\begin{document}
\begin{tikzpicture}[]
% 标记点A, B, C
\coordinate[label=left:{$A$}] (A) at (0,2);
\coordinate[label=right:{$B$}] (B) at (5,2);
\coordinate[label=below right:{$C$}] (C) at (5,0);
% 以C点为圆心, 5pt为半径绘制圆
\draw (C) circle (5pt);
\end{tikzpicture}
\end{document}
示例:
椭圆的画法只需要将circle改为ellipse, 然后多加上一个参数, 将半径参数变为x半径和y 半径就可以了.
\documentclass[tikz,border=3pt]{standalone}
\begin{document}
\begin{tikzpicture}[]
% 标记点A, B, C
\coordinate[label=left:{$A$}] (A) at (0,2);
\coordinate[label=right:{$B$}] (B) at (5,2);
\coordinate[label=below right:{$C$}] (C) at (5,0);
% 绘制椭圆
\draw (B) ellipse [x radius=1,y radius=.5];
% 简写的等价命令
% \draw (B) ellipse (1 and .5);
\end{tikzpicture}
\end{document}
弧段(arc)
这里我要介绍介绍最后一种基本图形弧段(还有很多, 不过一般来说用不到了), 之所以没叫圆弧是因为还有椭圆弧, 这里这条命令没有前面几个那样直观, 需要想一下初始位置的角度值然后才能画出自己想要的弧段.
基本命令:
\draw (<start point coordinate>) arc [radius, start angle, end angle or delta angle]
这里最容易混淆的就是这个起始角度了, 这个弧段绘制的命令中, 对于初始角度, 其对应的圆心需要指向起始角度的相反方向, 也就是说, 如果你选择了0
作为起始角度, 其圆心应该是指向左边(x负向)的, 这就是一开始用arc
时候没法正确绘制弧段的一个细节. 之后, 再以这个圆心作直角坐标系, 逆时针旋转delta角度(或者使用给定的end角度), 就能很方便地绘制出arc了.
\documentclass[tikz,border=3pt]{standalone}
\begin{document}
\begin{tikzpicture}[]
% 标记点A, B, C
\coordinate[label=left:{$A$}] (A) at (0,2);
\coordinate[label=right:{$B$}] (B) at (5,2);
\coordinate[label=below right:{$C$}] (C) at (5,0);
% 绘制弧段, 半径为1cm, 开始角度为0, 结束角度为90
\draw (A) arc [radius=1, start angle=0, end angle=90];
% 等价的命令, 采用起始角度和改变角度来实现
\draw (A) arc [radius=1, start angle=0, delta angle=90];
% 等价的简写命令
% \draw (A) arc (0:90:1);
\end{tikzpicture}
\end{document}