diff options
Diffstat (limited to 'Master/texmf-dist/doc/latex/lshort-chinese/src/chap/chap.07.graphic.tex')
-rw-r--r-- | Master/texmf-dist/doc/latex/lshort-chinese/src/chap/chap.07.graphic.tex | 262 |
1 files changed, 169 insertions, 93 deletions
diff --git a/Master/texmf-dist/doc/latex/lshort-chinese/src/chap/chap.07.graphic.tex b/Master/texmf-dist/doc/latex/lshort-chinese/src/chap/chap.07.graphic.tex index 9692245611e..13a07dc1e4d 100644 --- a/Master/texmf-dist/doc/latex/lshort-chinese/src/chap/chap.07.graphic.tex +++ b/Master/texmf-dist/doc/latex/lshort-chinese/src/chap/chap.07.graphic.tex @@ -6,7 +6,7 @@ 本章将带你了解一些基本的绘图功能。 一些特殊的绘图,如交换图、树状图甚至分子式和电路图也能够通过代码绘制, -不过其复杂程度已经超出本手册范围,有兴趣的读者可以查阅一些帮助手册,或者在互联网寻求帮助。 +不过其复杂程度已经超出本手册范围,有兴趣的读者可以查阅一些帮助文档,或者在互联网寻求帮助。 \end{intro} \section{绘图语言简介}\label{sec:pict-lang} @@ -14,14 +14,14 @@ \LaTeX\ 提供了原始的 \env{picture} 环境,能够绘制一些基本的图形如点、线、矩形、圆、B\'ezier 曲线等等, 不过受制于 \LaTeX\ 本身,它的绘图功能极为有限,效果也不够美观。 -现在流行的绘图代码有以下几种: +当前较为流行的、用于 \LaTeX\ 的绘图宏包 / 程序主要有: \begin{itemize} \item PSTricks \par - 以 PostSciprt 语言的功能为基础的绘图宏包,具有优秀的绘图能力。它对老式的 \texttt{latex + dvips} 编译命令支持最好, + 以 PostSciprt 语法为基础的绘图宏包,具有优秀的绘图能力。它对老式的 \texttt{latex + dvips} 编译命令支持最好, 而现在的几种编译命令下使用起来都不够方便。 \item \TikZ\ \& \pkg{pgf} \par - 德国的 Till Tantau 在开发著名的 \LaTeX\ 幻灯片文档类 \cls{beamer} 时一并开发了绘图宏包 \pkg{pgf}, + 德国的 Till Tantau 教授在开发著名的 \LaTeX\ 幻灯片文档类 \cls{beamer} 时一并开发了绘图宏包 \pkg{pgf}, 目的是令其能够在 \texttt{pdflatex} 或 \texttt{xelatex} 等不同的编译命令下都能使用。 \TikZ\ 是在 \pkg{pgf} 基础上封装的一个宏包,采用了类似 \hologo{METAPOST} 的语法,提供了方便的绘图命令,绘图能力不输 PSTricks。 @@ -29,12 +29,12 @@ \hologo{METAPOST} 脱胎于高德纳为 \TeX\ 配套开发的字体生成程序 \hologo{METAFONT}, 具有优秀的绘图能力,并能够调用 \TeX\ 引擎向图片中插入文字和公式。 Asymptote 在 \hologo{METAPOST} 的基础上更进一步,具有一定的类似 C 语言的编程能力,支持三维图形的绘制。\par - 它们往往需要把代码写在单独的文件里,用特定的工具去编译,也可以借助特殊的宏包在 \LaTeX\ 代码里直接使用。 + 它们作为独立的程序,通常的用法是将代码写在单独的文件里,编译生成图片供 \LaTeX\ 引用,也可以借助特殊的宏包在 \LaTeX\ 代码里直接使用。 \end{itemize} -本手册将介绍 \TikZ\ 绘图语言里最基本的部分。\TikZ\ 还支持各种自定义的扩展,基于 \TikZ\ 的专门用途的绘图宏包也不胜枚举, +本手册将介绍 \TikZ\ 绘图宏包里最基本的部分。\TikZ\ 还支持各种自定义的扩展,基于 \TikZ\ 的专门用途的绘图宏包也不胜枚举, 其复杂程度已远远超出入门手册的范围(\TikZ\ 的帮助文档有上千页之厚)。 -对此感兴趣的读者需要自行查阅帮助手册,或者到互联网上参考现成的范例。 +对此感兴趣的读者需要自行查阅帮助文档,或者到互联网上参考现成的范例。 \section{\TikZ\ 绘图语言}\label{sec:tikz} @@ -49,7 +49,7 @@ \cmd{tikz}\oarg*{\ldots} \marg*{\Arg{tikz code 1}\texttt{;} \Arg{tikz code 2}\texttt{;} \ldots} \\[1ex] \cmd{begin}\marg*{tikzpicture}\oarg*{\ldots} \\ \Arg{tikz code 1}\texttt{;} \\ -\Arg{tikz code 2}\texttt{;}\ldots \\ +\Arg{tikz code 2}\texttt{;} \\ \ldots \\ \cmd{end}\marg*{tikzpicture} \end{command} @@ -95,8 +95,15 @@ 连续使用连线时,可以使用 \texttt{cycle} 令路径回到起点,生成闭合的路径。 \begin{example} \begin{tikzpicture} -\draw (0,0) -- (1,1) - -- (2,0) -- cycle; +\draw (0,0) -- (1,1) -- (2,0) -- cycle; +\end{tikzpicture} +\end{example} + +多条路径可用于同一条画图命令中,以空格分隔: +\begin{example} +\begin{tikzpicture} +\draw (0,0) -- (0,1) + (1,0) -- (1,1) -- (2,0) -- cycle; \end{tikzpicture} \end{example} @@ -126,14 +133,14 @@ \end{example} \begin{itemize} - \item 正弦、余弦曲线(四分之一个周期): + \item 正弦、余弦曲线(1/4 周期): \end{itemize} \begin{example} \begin{tikzpicture} -\draw (0,0) sin (1.57,1); -\draw (0,1) sin (1.57,0); -\draw (2,1) cos (3.57,0); -\draw (2,0) cos (3.57,1); +\draw (0,0) sin (1,1); +\draw (0,1) sin (1,0); +\draw (2,1) cos (3,0); +\draw (2,0) cos (3,1); \end{tikzpicture} \end{example} @@ -171,11 +178,11 @@ \begin{example} \begin{tikzpicture} \draw[help lines,step=0.5] - (-2,-2) grid (2,2); -\draw[->] (-2.5,0) -- (2.5,0); -\draw[->] (0,-2.5) -- (0,2.5); -\draw[domain=-2:2] - plot(\x,{\x*\x -2}); + (-1,-1) grid (1,1); +\draw[->] (-1.5,0) -- (1.5,0); +\draw[->] (0,-1.5) -- (0,1.5); +\draw[domain=-1:1] + plot(\x,{\x*\x*2 -1}); \end{tikzpicture} \end{example} @@ -185,7 +192,7 @@ 除了 \cmd{draw} 命令之外,\TikZ\ 还提供了 \cmd{fill} 命令用来填充图形,\cmd{filldraw} 命令则同时填充和描边。 除了矩形、圆等现成的闭合图形外,\cmd{fill} 和 \cmd{filldraw} 命令也能够填充人为构造的闭合路径。 \begin{command} -\cmd{draw}\oarg*{\ldots} \Arg{path}; \quad +\cmd{draw}\oarg*{\ldots} \Arg{path}; \\ \cmd{fill}\oarg*{\ldots} \Arg{path}; \\ \cmd{filldraw}\oarg*{\ldots} \Arg{path}; \end{command} @@ -193,65 +200,122 @@ 绘图参数可作为可选参数用在 \env{tikzpiture} 环境或 \cmd{tikz} 命令时,参数会影响到所有具体的绘图命令; 用在单个绘图命令 \cmd{draw}、\cmd{filldraw} 等时,只对这个命令起效。 -\envindex[tikz]{scope} -\TikZ\ 还提供了 \env{scope} 环境,令一些绘图参数在局部起效: +\TikZ\ 有数不清的绘图参数,这些参数令 \TikZ\ 能够绘制丰富多彩的图像,同时也令 \TikZ\ 难以精通。 +以下示例常用的一些绘图参数。 + +\begin{itemize} + \item \texttt{color/draw/fill=\Arg{color}} 为 \cmd{draw} 或 \cmd{fill} 等命令指定颜色。 + \texttt{draw} 和 \texttt{fill} 分别指定填充和描边的颜色,而 \texttt{color} 同时指定, + 可以省略 \texttt{color=} 直接写颜色名称。 +\end{itemize} +\begin{example} +\begin{tikzpicture}[thick] +\draw[blue] (0,0) rectangle (1,1); +\filldraw[fill=yellow,draw=red] + (2,0.5) circle [radius=0.5]; +\end{tikzpicture} +\end{example} + +\begin{itemize} + \item \texttt{thick=\Arg{length}/thin/semithick/\ldots} 指定线条的粗细。 +\end{itemize} \begin{example} \begin{tikzpicture} -\draw (0,0) rectangle (2.5, 2.5); -\begin{scope}[fill=gray,scale=0.5] -\filldraw (0,0) rectangle (2.5, 2.5); -\end{scope} +\draw[ultra thin] (0,0)--(0,2); +\draw[very thin] (0.5,0)--(0.5,2); +\draw[thin] (1,0)--(1,2); +\draw[semithick] (1.5,0)--(1.5,2); +\draw[thick] (2,0)--(2,2); +\draw[very thick] (2.5,0)--(2.5,2); +\draw[ultra thick] (3,0)--(3,2); \end{tikzpicture} \end{example} -\TikZ\ 有数不清的绘图参数,这些参数令 \TikZ\ 能够绘制丰富多彩的图像,同时也令 \TikZ\ 难以精通。 -本手册仅总结常用的一些绘图参数,见表 \ref{tbl:tikz-options}。 - -\begin{table}[htp] -\caption{\TikZ\ 常用的一些绘图参数。}\label{tbl:tikz-options} -\small -\hrule \begin{itemize} - \item \texttt{color=\Arg{color}} \par - 为线条(\cmd{draw})或填充(\cmd{fill})指定颜色,\Arg{color} 使用颜色名或是 \pkg{xcolor} 的混合颜色语法。 - 往往可以不写 \texttt{color=} 直接写颜色名称。 - \item \texttt{fill=\Arg{color} / draw=\Arg{color}} \par - 分别给 \cmd{filldraw} 指定填充和描边的颜色。也可给 \cmd{fill} 和 \cmd{draw} 命令使用。 - 不带参数直接使用 \texttt{fill} 和 \texttt{draw},相当于用默认颜色。 + \item \texttt{solid/dashed/dotted/dash dot/dash dot dot} 指定线条类型(实线、虚线、点划线等)。 + 与 \texttt{dashed} 对应地有 \texttt{densely dashed} 和 \texttt{loosely dashed},后三种类型同理。 \end{itemize} -\hrule +\begin{example} +\begin{tikzpicture} +\draw[dashed] (0,0) -- (0,2); +\draw[dotted] (0.5,0) -- (0.5,2); +\draw[dash dot] (1,0) -- (1,2); +\draw[dash dot dot] (1.5,0) -- (1.5,2); +\draw[densely dotted] + (2,0) -- (3,2) -- (4,0) -- cycle; +\end{tikzpicture} +\end{example} + \begin{itemize} - \item \texttt{line width=\Arg{length}} \par - 指定线条粗细为 \Arg{width}。默认为普通线宽 0.4pt。 - \item \texttt{thin / semithick / thick / \ldots} \par - 指定线条粗细为预定义的某个类型,默认为 \texttt{thin}。总共有七种预定义的类型: - \texttt{ultra thin},\texttt{very thin},\texttt{thin},\texttt{semithick},\texttt{thick}, - \texttt{very thick},\texttt{ultra thick}。 - \item \texttt{help lines} \par - 指定线条为辅助线,相当于 \texttt{line width=0.2pt,gray}。 - \item \texttt{solid / dashed / dotted / dash dot / dash dot dot / \dots} \par - 指定线条类型(实线、虚线等)。 - \item \texttt{rounded corners} \par - 将路径转向处绘制成圆角。可写成 \texttt{rounded corners=\Arg{radius}} 使用给定的半径。 + \item \texttt{\Arg{arrow}-\Arg{arrow}} 指定线条首尾的箭头形式。 + 较为复杂的箭头形式需要使用 \cmd{use\-tikz\-library}\marg*{arrows.meta}。 \end{itemize} -\hrule +\begin{example} +\begin{tikzpicture}[thick] +\draw[->] (0,4) -- (3,4); +\draw[->>] (0,3.5) -- (3,3.5); +\draw[->|] (0,3) -- (3,3); +\draw[<-] (0,2.5) -- (3,2.5); +\draw[<->] (0,2) -- (3,2); +\draw[>->|] (0,1.5) -- (3,1.5); +\draw[-stealth] (0,1) -- (3,1); +\draw[-latex] (0,0.5) -- (3,0.5); +\draw[-to] (0,0) -- (3,0); +\end{tikzpicture} +\end{example} + \begin{itemize} - \item \texttt{-> / -< / -to / -latex / -stealth / \ldots} \par - 指定路径终点的箭头种类。 - \item \texttt{<- / >- / to- / latex- / -stealth / \ldots} \par - 指定路径起点的箭头种类。起点和终点的箭头可以搭配,如 \texttt{<->} 或者 \texttt{latex-to} 等。 + \item \texttt{rounded corners\oarg*{=\Arg{radius}}/sharp corners} 将路径转向处绘制成圆角/直角。可选参数 \Arg{radius} 控制圆角的半径。 + 可以对某一段路径直接使用。 \end{itemize} -\hrule +\begin{example} +\begin{tikzpicture} +\draw[rounded corners] + (0,0) rectangle (1,1); +\draw (2,0) -- (2,1) + [rounded corners=.3cm] + -- (3,1) -- (3.5,0) + [sharp corners] -- cycle; +\end{tikzpicture} +\end{example} + \begin{itemize} - \item \texttt{scale=\Arg{scale}} \par - 指定整个图像或某个路径的缩放比例。 - \item \texttt{xshift=\Arg{length} / yshift=\Arg{length}} \par - 指定整个图像或某个路径相对于原位置的水平/垂直位移。 - \item \texttt{rotate=\Arg{angle}} \par - 指定整个图像或某个路径旋转一定角度。 + \item \texttt{scale/xshift/yshift/xslant/yslant/rotate} 设定图形的缩放、位移和旋转。 \end{itemize} -\hrule -\end{table} +\begin{example} +\begin{tikzpicture} +\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[yshift=4pt](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{example} + +为了重复利用绘图参数,减少代码冗余,\TikZ\ 引入了“样式”的概念,可以定义一个样式包含绘图参数, +然后将样式作为一个参数用于绘图: +\begin{example} +\begin{tikzpicture} + [myarrow/.style={blue,thick,->}] +\draw (0,0)--(0,1)--(2,1); +\draw[myarrow] (0,0)--(2,1); +\draw[myarrow,dotted] + (0,0)--(2,0)--(2,1); +\end{tikzpicture} +\end{example} + +\envindex[tikz]{scope} +\TikZ\ 还提供了 \env{scope} 环境,令绘图参数或样式在局部生效: +\begin{example} +\begin{tikzpicture} +\draw (0,0) rectangle (2.5, 2.5); +\begin{scope}[thick,scale=0.5] +\draw (0,0) rectangle (2.5, 2.5); +\end{scope} +\end{tikzpicture} +\end{example} \subsection{\TikZ\ 文字结点}\label{subsec:tikz-node} @@ -271,37 +335,50 @@ \end{tikzpicture} \end{example} -表 \ref{tbl:tikz-options} 中的参数可用于 \cmd{node} 命令的配置。除此之外,\cmd{node} 还有一些特定的参数,见表 \ref{tbl:tikz-node-options}。 +\ref{subsec:tikz-draw} 小节中的参数可用于 \cmd{node} 命令的配置。除此之外,\cmd{node} 还有一些特定的参数: +\begin{itemize} + \item \texttt{anchor=\Arg{position}} 令结点的某个角落 \Arg{position} 与 \Arg{coordinate} 对应。 + \item \texttt{centered / above / below / left / right / above left / \ldots \oarg*{=\Arg{length}}} \\ + 与 \texttt{anchor} 等效的选项。可选的 \Arg{length} 为节点相对于 \Arg{coordinate} 的距离。 +\end{itemize} +\begin{example} +\begin{tikzpicture} +\coordinate (A) at (1,1); +\fill (A) circle[radius=2pt]; +\node[draw,anchor=south] at (A) {a}; +\node[draw,below right=4pt] at (A) {b}; +\end{tikzpicture} +\end{example} -\begin{table}[htp] -\caption{\TikZ\ 结点使用的一些绘图参数。}\label{tbl:tikz-node-options} -\small -\hrule \begin{itemize} - \item \texttt{anchor=\Arg{position}} \par - 指定结点的某个角落 \Arg{position} 位于给定的位置 \Arg{coordinate}。 - 参数 \Arg{position} 用 \texttt{center}、\texttt{north}、\texttt{north west} 等形式表示。 - \item \texttt{centered / above / below / left / right / above left / \ldots} \par - 指定结点相对于 \Arg{coordinate} 的位置,\texttt{anchor=\Arg{position}} 的等效写法。\texttt{above} 相当于 \texttt{anchor=south},以此类推。 - 带参数的形式 \texttt{above=\Arg{length}} 指定节点相对于 \Arg{coordinate} 的距离。 - \item \texttt{shape=\Arg{shape}} \par + \item \texttt{shape=\Arg{shape}} 结点的形状,默认可用 \texttt{rectangle} 和 \texttt{circle},可省略 \texttt{shape=} 直接写。在导言区使用命令 \cmd{use\-tikz\-library}\marg*{shapes.geometric} 可用更多的形状。 - \item \texttt{inner sep=\Arg{length} / outer sep=\Arg{length}} \par - 结点边界向外和向内的额外距离。 - \item \texttt{minimum size=\Arg{length} / minimum height=\Arg{length} / minimum width=\Arg{length}} \par - 结点的最小大小或最小高度/宽度。 - \item \texttt{text=\Arg{color}} \par + \item \texttt{text=\Arg{color}} 结点文字的颜色。 - \item \texttt{node font=\Arg{font command}} \par + \item \texttt{node font=\Arg{font command}} 结点文字的字体,形如 \cmd{bfseries} 或 \cmd{itshape} 等。 \end{itemize} -\hrule -\end{table} +\begin{example} +\begin{tikzpicture} +\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}; +\end{tikzpicture} +\end{example} + +\begin{itemize} + \item \texttt{inner sep=\Arg{length} / outer sep=\Arg{length}} + 结点边界向外和向内的额外距离。 + \item \texttt{minimum size=\Arg{length} / minimum height=\Arg{length} / minimum width=\Arg{length}} \\ + 结点的最小大小或最小高度/宽度。 +\end{itemize} \cmd{node} 命令不仅为文字结点的位置命名,在 \cmd{draw} 等命令中还可以使用某个结点的相对位置, 以“东南西北”的方式命名: - \begin{example} \begin{tikzpicture} \draw (0,0) circle[radius=1]; @@ -311,7 +388,7 @@ \end{tikzpicture} \end{example} -另一种用法是在 \cmd{draw} 等命令的路径中使用 \texttt{node},不仅可以对某个位置标记节点,还能够对线标记: +\cmd{node} 命令的一种等效用法是在 \cmd{draw} 等命令的路径中使用 \texttt{node},不仅可以对某个位置标记节点,还能够对线标记: \begin{example} \begin{tikzpicture} \draw (2,1.5) node[above] {$A$} @@ -324,8 +401,7 @@ \end{tikzpicture} \end{example} -除了 \cmd{node} 命令之外,\cmd{coordinate} 也可以通过参数为某个位置添加文字(label)。 -在此举一个较为复杂的例子,综合前面介绍过的各种路径、形状、文字结点和参数设置。 +在此举一个较为复杂的例子,综合前面介绍过的各种路径、形状、文字结点和参数设置,见源代码 \ref{code:tikz-example}。 \begin{sourcecode}[htp] \begin{Verbatim} @@ -334,8 +410,8 @@ \draw[-stealth,line width=0.2pt] (0,-0.5) -- (0,2.5); \coordinate (a) at (0.5,1.9); \coordinate (b) at (4,1.2); -\coordinate[label=below:$a$] (a0) at (a |- 0,0); -\coordinate[label=below:$b$] (b0) at (b |- 0,0); +\node[below] (a0) at (a |- 0,0) {$a$}; +\node[below] (b0) at (b |- 0,0) {$b$}; \filldraw[fill=gray!20,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, |