TikZ设计原则

TikZ设计原则

Content #

Specifying Points #

笛卡尔坐标系:(1cm, 2pt) 极坐标系:(30:1cm) 若不提供单位,(2,1)表示向右2cm,向上1cm.

++(1cm, 0pt)的含义是:从上个点开始向右1cm。 (1,0) ++(1,0) ++(0,1) 表示 (1,0), (2,0), (2,1)

+(1cm, 0pt)不改变当前参照点。 (1,0) +(1,0) +(0,1) 表示 (1,0), (2,0), (1,1)

Path Specifications #

创建TikZ图片的主要任务就是描述path的规格。path由多个或直或曲的线条组成,线条之间并不一定连接在一起。画三角形的path可声明如下: (5pt,0pt) – (0pt,0pt) – (0pt,5pt) – cycle

Actions on Paths #

Drawing(stroking)可以理解成用特定粗细的笔沿着path作图。 \draw命令相当于\path[draw]

Filling表示path的内部由某种颜色来填充,它只对closed path起作用。 \fill命令相当于\path[fill] \filldraw命令相当于\path[draw,fill]

Key-Value Syntax for Graphic Parameters #

作图参数(Graphic Parameters)包括:colors, the dashing pattern, the clipping area, the line width, and many others.

作图参数由Key-Value pairs来描述,比如 color=red. 作图参数由可选参数的形式提供给path drawing or filling命令。下图画出一个线宽为2pt的红色的三角形: \tikz \draw[line width=2pt,color=red] (1,0) – (0,0) – (0,1) – cycle;

Special Syntax for Specifying Nodes #

Node用于向图形中添加文本,它会添加在path的当前位置(current position)上。默认情况下,Node会在path画出来后再绘制。 \tikz \draw (1,1) node {text} – (2,2);

Special Syntax for Specifing Trees #

node后可跟任意个children,每个children由关键词child来指定。

\begin{tikzpicture}
  \node {root}
    child {node {left}}
    child {node {right}
      child {node {child}}
      child {node {child}}
    };
\end{tikzpicture}

Special Syntax for Graphs #

graph命令在是node语法之上的,方便添加带有大量节点的树时使用。

\usetikzlibrary {graphs}
\tikz \graph [grow down, branch right] {
  root -> { left, right -> {child, child} }
};

Grouping of Graphic Parameters #

通过{scope} environment可将多个用于drawing和filling的graphic parameters 封装在一起。

\begin{tikzpicture}
  \begin{scope}[color=red]
    \draw (0mm,10mm) -- (10mm,10mm);
    \draw (0mm, 8mm) -- (10mm, 8mm);
    \draw (0mm, 6mm) -- (10mm, 6mm);
  \end{scope}
  \begin{scope}[color=green]
    \draw             (0mm, 4mm) -- (10mm, 4mm);
    \draw             (0mm, 2mm) -- (10mm, 2mm);
    \draw[color=blue] (0mm, 0mm) -- (10mm, 0mm);
  \end{scope}
\end{tikzpicture}

Coordinate Transformation System #

TikZ支持两种坐标转换系统:coordinate transformation和canvas transformation. canvas transformation较为复杂难用,通常应选用 coordinate transformation.