标签归档:TikZ

TikZ一例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
\begin{tikzpicture}
\draw [help lines] (0, 0) grid (6, 5);
\draw [thick] (5, 0) -- (0, 4);
\draw [thick] (1.2, 4.5) to [out=-85, in=141.34] (2.5,2);
\draw [thick] (2.5, 2) to [out=-38.66, in=160] (5.2, 0.5);

\draw [thick] (2, 0) -- (0, 4);
\draw [thick] (0.3, 4.1) to [out=-85, in=141.34] (1.7,1.5);
\draw [thick] (1.7, 1.5) to [out=-38.66, in=160] (3.8, 0.3);

\node [left] (A) at (0, 4) {$A$};
\node [below] (B) at (5, 0) {$B$};
\node [below] (BB) at (2, 0) {$B^*$};

\draw [fill] (2.5, 2.0) circle [radius=0.05];
\node [above right] (C) at (2.5, 2.0) {$C$};
\draw [fill] (0.65, 2.74) circle [radius=0.05];
\node [right] (CC) at (0.65, 2.74) {$C^*$};

\draw [thick] (0, 3.26) -- (4.075, 0);
\node [left] (A2) at (0, 3.26) {$A'$};
\node [below] (B2) at (4.075, 0) {$B'$};

\draw [dashed, ultra thick] (0, 2.74) -- (0.65, 2.74) -- (0.65, 0) node [below] {$x_1^*$};
\end{tikzpicture
}

TikZ一例——万有引力示意图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\def\R{4}
\tikzstyle{blueball}=[circle, thick, minimum size=1.5cm, draw=blue!80,fill=blue!20]
\tikzstyle{greenball}=[circle, thick, minimum size=1.2cm, draw=green!80,fill=green!20]
\begin{tikzpicture}[>=latex]
\node (m_1) at (0, 0) [blueball]{$m_1$};
\node (m_2) at (\R, 0) [greenball]{$m_2$};

\draw[dashed] (0, -0.8) -- (0, -1.5);
\draw[dashed] (\R, -0.6) -- (\R, -1.5);
\draw[< -, dashed] (0, -1.5) -- (0.45*\R, -1.5);
\draw[->, dashed] (0.55*\R, -1.5) -- (\R, -1.5);
\node (l) at (0.5*\R, -1.5) {$r$};

\draw[->] (0.8, 0) -- (0.4*\R, 0);
\draw[<-] (0.6*\R+0.2, 0) -- (\R-0.6, 0);
\node (F1) at (1.1, 0.3) {$F_1$};
\node (F2) at (\R-1, 0.3) {$F_2$};

\node (G) at (0.5*\R, -2) {$F_1=F_2=G\displaystyle\frac{m_1\times m_2}{r^2}$};
\end{tikzpicture
}

KTikZ中文设置

KTikZ是一个编写TikZ脚本的辅助软件。主要功能有关键字提示、高亮、自动补全,作图结果实时预览等。初学TikZ的时候用一下,还是很方便的。

但是还有几个问题,都跟中文有关:
1. 不支持中文,输入、显示都没有问题,但是一旦保存退出再打开,就乱码了。不知道是什么编码,肯定用的不是utf8。如果图片中有中文,千万要小心。(更新:升级到svn版就没有这个问题了)
2. 默认用的是pdflatex进行编译,直接生成pdf后再预览,如果想用xelatex,可以在配置中直接设置。但是这样做并不能得到正确的结果,因为KTikZ默认的模板中,用到了preview这个包(这个包使得导出的图片没有多余空白),并且指定编译器是pdftex系列,所以会有问题。这个问题和下面的问题一起通过修改默认模板的方式解决。
3. 图片不支持中文标注。TikZ对中文的支持非常好,一般在文章中作图时,中文都没有问题,主要原因还是KTikZ用的模板没考虑中文。

KTikZ可以直接指定模板,默认的模板放在/usr/share/apps/ktikz/templates下(archlinux环境,其他发行版类似)。为了保险,复制一份到自己的home目录里再修改。
以template_example.pgs为例,改成下面这样,就支持中文标注和xelatex了,有变动的行加了注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
\documentclass{article}
\usepackage[adobefonts]{ctex
} %支持中文就靠这一行了
%\usepackage{mathptmx} %这一行是指定公式所用的字体,我看着不习惯,就去掉了
\usepackage{tikz}
%\usepackage{color}
\usepackage[active,xetex,tightpage]{preview} %这一行有改动,将pdftex换成xetex
\PreviewEnvironment[]{tikzpicture}
\PreviewEnvironment[
]{pgfpicture}
\DeclareSymbolFont{symbolsb}{OMS}{cmsy}{m}{n}
\SetSymbolFont{symbolsb}{bold}{OMS}{cmsy}{b}{n}
\DeclareSymbolFontAlphabet{\mathcal}{symbolsb}
\begin{document}
<>
\end{document
}

顺便贴上今天下午作的图和源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\usetikzlibrary{decorations.markings}
\begin{tikzpicture
}[x=4cm,y=4cm, decoration={
markings,%
mark=at position 0.5 with{\arrowreversed[black]{latex};}}] %stealth

\draw(0, 0) node[below left] {$O$};
\draw[-latex] (0,0) --node[below] {$\sqrt{-k/b}$} (1.125,0) node[right=3, below] {$x(t)$};
\draw[-latex] (0,0) --node[left] {$\sqrt{k/a}$} (0,1.125) node[left=-1] {$y(t)$};

\draw[postaction={decorate}, thick] (0, 0) -- (1, 1) node[above right] {$k=0$,平局};

\draw[postaction={decorate}, thick] (0, 0.3) parabola (0.4,0.5);
\draw[postaction={decorate}, thick] (0, 0.5) parabola (0.42,0.75);
\draw[postaction={decorate}, thick] (0, 0.7) parabola (0.41,0.95) node[above] {$k&gt;0$,乙方胜};
\draw[postaction={decorate}, thick, rotate=-90] (0, 0.25) parabola (-0.4,0.5);
\draw[postaction={decorate}, thick, rotate=-90] (0, 0.45) parabola (-0.42,0.75) node[above right] {$k \draw[postaction={decorate}, thick, rotate=-90] (0, 0.65) parabola (-0.41,0.95);
\end{tikzpicture
}

TikZ一例–简单流程图


下面那个大括号找了半天。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
\usetikzlibrary{arrows}
\usetikzlibrary{decorations.pathreplacing
}

% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt
]
\tikzstyle{block} = [rectangle, draw, fill=blue!20,
text width=2em, text centered, rounded corners, minimum height=4em
]
\tikzstyle{line
} = [draw, -latex']
%\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
%    minimum height=2em]

\begin{tikzpicture}[scale=0.7, node distance = 2.0cm, auto]
\node [block] (problem) {\\\\\\};

\node [block, right of=problem] (model) {\\\\\\};
\path [line] (problem) -- (model);

\node [block, right of=model] (method) {数值计算方法};
\path [line] (model) -- (method);

\node [block, right of=method] (prog) {\\\\\\};
\path [line] (method) -- (prog);

\node [block, right of=prog] (comp) {\\\\\\};
\path [line] (prog) -- (comp);

\draw[decorate, decoration={brace, mirror}, very thick, blue] (0,-2) -- (3,-2);
\draw (1.5,-2.8) node {应用数学};

\draw[decorate, decoration={brace, mirror}, very thick, blue] (5.5,-2) -- (11.5,-2);
\draw (8.5,-2.8) node {计算数学};
\end{tikzpicture
}

TikZ一例–内公切线

一下午就画这么个东西:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
\usetikzlibrary{calc}
\begin{tikzpicture}
\def\L{8}
\coordinate (m) at (0.5*\L,0);
\draw (m) node[below] {$m$};
\draw[->] (0,0) -- (1.2*\L,0);
\node [circle,draw] (i) at (0,0) [minimum size=3cm] {};
\draw (i) node[below left] {$i$};
\node [circle,draw] (j) at (\L,0) [minimum size=3cm] {};
\draw (j) node[below right] {$j$};
\coordinate (a) at (tangent cs:node=i,point={(m)},solution=1);
\coordinate (c) at (tangent cs:node=i,point={(m)},solution=2);
\coordinate (b) at (tangent cs:node=j,point={(m)},solution=1);
\coordinate (d) at (tangent cs:node=j,point={(m)},solution=2);
\draw (a) node[above] {$A$} -- (i.center)-- (c) node[below] {$C$} --
(d) node[above] {$D$} -- (j.center)-- (b) node[below] {$B$} -- cycle;

\draw[dashed] (0,0) -- (0,0.5*\L);
\draw[dashed] (\L,0) -- (\L,0.5*\L);
\node (l) at (0.5*\L,0.45*\L) {$l_{ij}$};
\draw[< -] (0,0.45*\L) -- (l);
\draw[->] (l) -- (\L,0.45*\L);

\draw (m) +(0:8mm) arc (0:22:8mm);
\path (m) +(8:1.2cm) node{$\alpha_{ij}$};

\draw[dashed] (m) +($(0,0)-(a)$) -- (0,0);

\draw[->] (0,0) +(0:18mm) arc (0:-22:18mm);
\path (0,0) +(-8:22mm) node{$\alpha_{ij}$};

\draw[->] (0,0) -- (3,-2) node[below right] {$\overrightarrow{v}_{ij}$};
\draw[->] (0,0) +(0:8mm) arc (0:-32:8mm);
\path (0,0) +(-18:12mm) node{$\beta_{ij}$};

\draw[dashed] (m) +($(0,0)-(c)$) -- (0,0);
\end{tikzpicture
}

CaR,GCLC,TikZ

C.a.R.和GCLC都可以算做类似几何画板的所谓动态几何软件,这里是维基百科上关于这类软件的一个比较。那张表并不完整,至少缺了是否跨平台这一栏。而这两个软件都具备以下特点:

  1. 免费
  2. 跨平台
  3. 支持LaTeX

另外,C.a.R.可以将动态结果输出至网页形式,只要装了Java的机器,可以直接在浏览器中操作。GCLC则有机器证明的功能,并可以将做图脚本输出为TikZ格式。

而TikZ是LaTeX上的一个宏包,可与Beamer很好的配合。

缺点是这三个软件都非常小众,无论国内还是国外,用户似乎都不多。

8月15日补充:

关于TikZ/PGF的好站(强力推荐):http://www.texample.net