TikZ使用笔记

zxl19 2022-07-20

我的TikZ使用笔记。

TikZ Hello World

TikZ是PGF(Portable Graphic Format)的前端,其名称来源于德语TikZ ist kein Zeichenprogramm,意为TikZ is not a drawing program,是一个GNU’s Not Unix形式的递归缩写。

绘图模板

\documentclass[border=2pt]{standalone}

% Drawing
\usepackage{tikz}
\usepackage{tikz-3dplot}

% Styles
\tikzset{>=latex}

\begin{document}
    \tdplotsetmaincoords{70}{110}
    \begin{tikzpicture}[tdplot_main_coords]
        % Plot Here
    \end{tikzpicture}
\end{document}

宏定义

变量

%% Parameter Setup
% Translation World->A
\pgfmathsetmacro{\ax}{-0.5}
\pgfmathsetmacro{\ay}{1}
\pgfmathsetmacro{\az}{2.5}
% Translation World->B
\pgfmathsetmacro{\bx}{-1.5}
\pgfmathsetmacro{\by}{3.5}
\pgfmathsetmacro{\bz}{1}
% Euler Angle World->A
\pgfmathsetmacro{\rollWA}{0}
\pgfmathsetmacro{\pitchWA}{0}
\pgfmathsetmacro{\yawWA}{10}
% Euler Angle World->B
\pgfmathsetmacro{\rollWB}{0}
\pgfmathsetmacro{\pitchWB}{0}
\pgfmathsetmacro{\yawWB}{30}
% P_{W}
\pgfmathsetmacro{\px}{2}
\pgfmathsetmacro{\py}{4}
\pgfmathsetmacro{\pz}{5}
% Control Points
\pgfmathsetmacro{\mx}{(\ax + \bx) / 2}
\pgfmathsetmacro{\my}{(\ay + \by) / 2}
\pgfmathsetmacro{\mz}{(\az + \bz) / 2}

坐标系原点

%% Define Points
\coordinate (World) at (0, 0, 0);
\coordinate (A) at (\ax, \ay, \az);
\coordinate (B) at (\bx, \by, \bz);
\coordinate (P) at (\px, \py, \pz);

绘图命令

点、线段和曲线

%% Plot Point P
\fill[black] (P) circle[radius = 1pt];
\draw[thin, color = black, dashed] (A) -- (P) node[anchor = south east]{$\mathbf{P}_{\mathcal{A}}$};
\draw[thin, color = black, dashed] (B) -- (P) node[anchor = north west]{$\mathbf{P}_{\mathcal{B}}$};

%% Plot Poses
\draw[thin, color = black, ->] (World) sin (A);
\draw[thin, color = black, ->] (World) sin (B);
\draw[thin, color = black, ->] (A) parabola (B);

坐标系

%% Plot World Coordinates
\draw (World) node[anchor = south east]{$\left\{\mathcal{W}\right\}$};
\draw[thick, color = black, ->] (World) -- (1, 0, 0) node[anchor = east]{$x_{\mathcal{W}}$};
\draw[thick, color = black, ->] (World) -- (0, 1, 0) node[anchor = west]{$y_{\mathcal{W}}$};
\draw[thick, color = black, ->] (World) -- (0, 0, 1) node[anchor = south]{$z_{\mathcal{W}}$};

%% Plot A Coordinates
\tdplotsetrotatedcoords{\rollWA}{\pitchWA}{\yawWA}
\tdplotsetrotatedcoordsorigin{(A)}
\draw (A) node[anchor = south east]{$\left\{\mathcal{A}\right\}$};
\draw[thick, color = red, tdplot_rotated_coords, ->] (0, 0, 0) -- (1, 0, 0) node[anchor = north]{$x_{\mathcal{A}}$};
\draw[thick, color = green, tdplot_rotated_coords, ->] (0, 0, 0) -- (0, 1, 0) node[anchor = west]{$y_{\mathcal{A}}$};
\draw[thick, color = blue, tdplot_rotated_coords, ->] (0, 0, 0) -- (0, 0, 1) node[anchor = south]{$z_{\mathcal{A}}$};

%% Plot B Coordinates
\tdplotsetrotatedcoords{\rollWB}{\pitchWB}{\yawWB}
\tdplotsetrotatedcoordsorigin{(B)}
\draw (B) node[anchor = south west]{$\left\{\mathcal{B}\right\}$};
\draw[thick, color = red, tdplot_rotated_coords, ->] (0, 0, 0) -- (1, 0, 0) node[anchor = north]{$x_{\mathcal{B}}$};
\draw[thick, color = green, tdplot_rotated_coords, ->] (0, 0, 0) -- (0, 1, 0) node[anchor = west]{$y_{\mathcal{B}}$};
\draw[thick, color = blue, tdplot_rotated_coords, ->] (0, 0, 0) -- (0, 0, 1) node[anchor = south]{$z_{\mathcal{B}}$};

文字说明

%% Add Description
\draw (\ax / 2, \ay / 2, \az / 2) node[anchor = west] {$\mathbf{T}^{\mathcal{W}}_{\mathcal{A}}$};
\draw (\bx / 2, \by / 2, \bz / 2) node[anchor = west] {$\mathbf{T}^{\mathcal{W}}_{\mathcal{B}}$};
\draw (\mx, \my, \mz) node[anchor = east] {$\mathbf{T}^{\mathcal{A}}_{\mathcal{B}}$};

模板和学习资源查询

网站

  1. TikZ
  2. TeXample

宏包文档

  1. standalone-CTAN
  2. pgf-CTAN
  3. tikz-3dplot-CTAN
  4. tkz-euclide-CTAN
  5. VisualTikz-CTAN
  6. tikzmark-CTAN
  7. pgf-tikz/pgf
  8. Hansimov/pgfmanual-zh

教程

  1. xiaohanyu/awesome-tikz
  2. xinychen/awesome-latex-drawing
  3. PetarV-/TikZ
  4. MLNLP-World/Paper-Picture-Writing-Code
  5. hackl/tikz-network
  6. LIAMF-USP/TikzTemplates
  7. hoganbin/TikZ-Learning-Notes
  8. AlexTsagas/Tikz-Graphics

转换工具

  1. Mathcha
  2. nschloe/tikzplotlib
  3. matlab2tikz/matlab2tikz
  4. xyz2tex/svg2tikz

高质量模板

  1. HarisIqbal88/PlotNeuralNet
  2. synercys/annotated_latex_equations

参考

  1. [LaTeX绘图指南-001]TikZ的简介、资源以及学习方法-Hansimov的文章-知乎
  2. Main Coordinate Systems-TikZ.net
  3. Example: The 3dplot package-TeXample.net
  4. 用LaTeX做笔记是怎样的体验?-Rabbittac的回答-知乎
  5. 神经网络的宏包丝滑排版令人流连忘返-我是科研小秘书的文章-知乎
  6. 工具丨Latex公式中添加漂亮的注释,写论文的同学们用起来!-Realcat的文章-知乎