0%

LaTex绘图指南-1-向量和范数

使用LaTeX绘制各种科研图,示意图等。

代码和效果

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
\documentclass[border = 0.2cm, 12pt]{standalone}
\usepackage{amsmath, amssymb, amsfonts}
\usepackage{color}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{shapes,snakes}
\tikzset{>=latex}
\usetikzlibrary{angles,quotes}
\usepackage{tkz-euclide}

\begin{document}

\begin{tikzpicture}[scale=2.5]

\draw[->,line width=0.7] (-1.4,0)--(1.4,0) node[above]{$x$};
\draw[->,line width=0.7] (0,-1.4)--(0,1.4) node[right]{$y$};

\draw[arrows=->, line width=1.2] (0,0)--(0.6,1);
\draw[dashed, color=gray, thin] (0.6,0)--(0.6,1);
\draw[color=blue, line width=1.2] (1,0)--(1,1)--(-1,1)--(-1,-1)--(1,-1)--(1,0);

\node at (0.6,-0.15) {$x_1$};
\node at (0.15,1.12) {$x_2$};
\node at (0.7,1.15) {$\boldsymbol{x}$};

\node at (1.1,-0.2) {$1$};
\node at (-1.2,-0.2) {$-1$};
\node at (-0.2,1.12) {$1$};
\node at (-0.25,-1.15) {$-1$};

\end{tikzpicture}

\end{document}

解释

基础部分

文档设置部分: standalone 是一个特殊的LaTeX文档类,他会自动把输出的PDF建材到刚好包含内容的大小,十分适合绘图。
设置border表示内容周围添加的边距,12pt表示字体大小。

绘图部分

绘图环境使用tikzpicture

scale表示绘图比例,基本单位是1cm,scale=2.5表示绘图比例为2.5,也就是说绘图中的1cm的输出实际是2.5cm。

>=latex表示箭头为latex风格。常见的箭头样式:

1
2
3
4
\tikzset{>=latex}    % LaTeX 风格箭头(最常用)
\tikzset{>=stealth} % 匕首型箭头
\tikzset{>=to} % 简单直线箭头
\tikzset{>=triangle} % 三角形箭头

绘制坐标轴

1
2
\draw[->,line width=0.7] (-1.4,0)--(1.4,0) node[above]{$x$};
\draw[->,line width=0.7] (0,-1.4)--(0,1.4) node[right]{$y$};

以绘制$x$轴为例,(-1.4,0)表示起点,(1.4,0)表示终点,line width=0.7表示线宽,node[above]{$x$}表示在终点上方添加$x$标签。

这里关键的就在于node命令,如果要在下方添加标签,可以这样写:

1
draw[->,line width=0.7] (-1.4,0)--(1.4,0) node[below]{$x$};

TikZ 中的位置关键字可以组合使用,常见的位置选项包括:

  • below - 下方
  • left - 左边
  • right - 右边
  • above - 上方
  • below left - 左下
  • below right - 右下
  • above left - 左上
  • above right - 右上

也可以使用精确数值控制:

1
\draw[->,line width=0.7] (-1.4,0)--(1.4,0) node[below=0.2cm]{$x$};

或者像后文一样,使用node at命令:

1
2
3
\node at (0.6,-0.15) {$x_1$};
\node at (0.15,1.12) {$x_2$};
\node at (0.7,1.15) {$\boldsymbol{x}$};

绘制项链和辅助线:

1
2
\draw[arrows=->, line width=1.2] (0,0)--(0.6,1);
\draw[dashed, color=gray, thin] (0.6,0)--(0.6,1);

3D坐标

使用tikz-3dplot包,可以绘制3D坐标系。

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
\tdplotsetmaincoords{60}{120}
\begin{tikzpicture}
[scale=3,tdplot_main_coords, axis/.style={->,black,line width=0.7}, vector/.style={-stealth,black,line width=1.2}, vector guide/.style={dashed,gray,thin}]

%standard tikz coordinate definition using x, y, z coords
\coordinate (O) at (0,0,0);

%tikz-3dplot coordinate definition using r, theta, phi coords
\tdplotsetcoord{P}{.8}{55}{60}

%draw axes
\draw[axis] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};
\draw[axis] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};
\draw[axis] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};

%draw a vector from O to P
\draw[vector] (O) -- (P);

%draw guide lines to components
\draw[vector guide] (O) -- (Pxy);
\draw[vector guide] (Pxy) -- (P);
\draw[vector guide] (0,0,0.45) -- (P);
\draw[vector guide] (Pxy) -- (0.25,0,0);
\draw[vector guide] (Pxy) -- (0,0.5,0);

\node[text width=3.5cm] at (0,0.95,0.55) {\footnotesize $\|\boldsymbol{x}\|_2=\sqrt{x_1^2+x_2^2+x_3^2}$};
\node at (0.2,-0.1,0) {$x_1$};
\node at (-0.1,0.5,0) {$x_2$};
\node at (-0.05,-0.12,0.4) {$x_3$};

\end{tikzpicture}

其中,tdplot_main_coords定义了3D坐标系的主视角,axis定义了坐标轴的样式,vector定义了向量的样式,vector guide定义了向量辅助线的样式,具体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[scale=3,                    % 将整个图形放大3倍
tdplot_main_coords, % 使用3D坐标系统
axis/.style={ % 定义坐标轴样式
->, % 带箭头
black, % 黑色
line width=0.7 % 线宽0.7
},
vector/.style={ % 定义向量样式
-stealth, % 使用匕首型箭头
black, % 黑色
line width=1.2 % 线宽1.2
},
vector guide/.style={ % 定义辅助线样式
dashed, % 虚线
gray, % 灰色
thin % 细线
}]

tdplotsetcoord使用的是球坐标系,P表示点,.8表示半径,55表示俯仰角,60表示偏航角。

其他的内容和2D坐标系类似。

效果: