Tikz 框图

Tikz 框图 首页 / Latex入门教程 / Tikz 框图

Tikz 被定义为用于从代数或几何描述生成矢量图形的一对语言。流行的Tikz环境也用于Latex宏程序包。 Tikz解释器在后端支持多个Tex输出。以下是用于创建块的一些命令:

  • \node 命令用于绘制块并提及块的位置。
  • \draw 命令用于绘制线条的不同样式。
  • \tikzset 命令用于设置块的规格。
  • \usetikzlibrary 是用于在程序中实现定位系统的库。

Tikz 环境的编写方式为:

\begin{tikzpicture}
.....
\end{tikzpicture}

让我们以模拟通信系统的框图为例。下面给出了使用Tikz创建简单块结构的代码:

\documentclass[tikz,border=4mm]{standalone} % 指定边界 
\usetikzlibrary{positioning,fit,calc}
\tikzset{block/.style={draw, thick, text width=3cm, minimum height=1.5cm, align=center},   
% align 命令用于在中心对齐框图
% height 命令调整框图的高度
% 这里的块图是指整个图,而不是单个块
% 此处的粗命令表示块图内使用的所有块的边界。如果您想要块的细边缘,您可以将其更改为细命令
line/.style={-latex}   % 宽度越小,图表窗口越大
}  
\begin{document}  
    
    \begin{tikzpicture}  
      \node[block] (a) {Source};  
      \node[block,right=of a] (b) {Transmitter}; % 这些是块的名称
      % 您可以通过指定名称和字母来使用尽可能多的块  
      \node[block,right=of b] (c) {LearnFK};  
      \node[block,right=of c] (d) {Reciever};  
      \node[block,right=of d] (e) {Destination};  
      \node[block] (f) at ([yshift=-3cm]$(b)!1.0!(c)$) {Noise};   
      % yshift 此处指定点在 y 轴上的位置的偏移。您可以根据要求更改位置。
      % 提到的值是从 (b) 到 (c) 的距离。如果该值为 0.5,则块将位于 (b) 和 (c) 的中心。  
      \node[draw,inner xsep=4mm,inner ysep=7mm,fit=(a)(b),label={100:P}](g){}; % 它用于在块组合之外绘制盒子。这里将在块 (a) 和 (b) 之外绘制块。 xsep 和 ysep 是盒子的尺寸。此处的 label 命令用于设置外部 A 和 B 块名称的位置。 
      \node[draw,inner xsep=4mm,inner ysep=7mm,fit=(d)(e),label={80:Q}]{};  
        
      \draw[line] (a)-- (b);  
      \draw[line] (b)-- (c);  
      \draw[line] (f) -- (c); % 您可以根据您的要求将线路的位置更改为北、西等。
      \draw[line] (c)-- (d); % -- 表示两个块 (c) 和 (d) 之间的线。
     \draw[line] (d)-- (e);  
    \end{tikzpicture}  
\end{document}  

%之后写的文字只是为了使您更好地理解。 Latex会忽略%字符之后的文字。

输出还将以默认的灰色背景色显示。

输出:

Block Diagram using TikZ

其他复杂的示例使用与上述相同的命令。

此类示例的代码如下:

\documentclass[tikz,border=4mm]{standalone} % 指定边界 
\usetikzlibrary{positioning,fit,calc}
\tikzset{block/.style={draw, thick, text width=3cm, minimum height=1.5cm, align=center},   
% align 命令用于在中心对齐框图
% height 命令调整框图的高度
% 这里的块图是指整个图,而不是单个块
% 此处的粗命令表示块图内使用的所有块的边界。如果您想要块的细边缘,您可以将其更改为细命令
line/.style={-latex}   % 宽度越小,图表窗口越大
}  
\begin{document}  
    
    \begin{tikzpicture}  
    \node[block] (a) {LearnFK};  
    \node[block,right=of a] (b) {B};   
    \node[block,right=of b] (c) {C};  
    \node[block,right=of c] (d) {D}; % 用于不同方块不同位置的命令
      \node[block] (e) at ([yshift=-2cm]$(b)!0.5!(c)$) {E};   
      \node[block] (f) at ([yshift=-2cm]$(c)!0.5!(d)$) {F};   
      \node[block] (g) at ([yshift=2cm]$(c)!0.5!(d)$) {G};   
      \node[block] (h) at ([yshift=2cm]$(b)!1.0!(b)$) {H};   
      \draw[line] (a)-- (b);  
      \draw[line] (b)-- (c);  
      \draw[line] (c)-- (d);  
      \draw[line] (e)-- (f);  
      \draw[line] (g)-- (h);  
      \draw[line] (f)-- (g);  
      \draw[line] (a.east) -- (e.west);  
      \draw[line] (h)-- (b);  
      \draw[line] (f.east) -- (d.south);  
    
\end{tikzpicture}  
\end{document}  

使用的命令简单易懂,易于实现。

输出:

Block Diagram using TikZ

您可以根据要求绘制任何形式的框图。

颜色填充

您也可以在特定框图的块中填充颜色。我们来看一个例子:

\documentclass[tikz, border=5mm]{standalone}   
\usetikzlibrary{positioning, fit, calc}   
\usepackage{xcolor}  
\tikzset{block/.style={draw, thick, text width=2cm , minimum height=1.3cm, align=center},   
line/.style={-latex}     
}  
\begin{document}  
  
    \begin{tikzpicture}  
    \node[block, fill=olive] (a) {A};  
    \node[block,right=of a, fill=yellow] (b) {LEARNFK};   
    \node[block,right=of b, fill= gray] (c) {C};  
    \node[block, fill=purple] (d) at ([yshift=-2cm]$(a)!0.5!(b)$) {D};  
    \node[block, fill= orange] (e) at ([yshift=-2cm]$(b)!0.5!(c)$) {E};  
    \node[draw, fill=pink, fill opacity=0.5,inner xsep=5mm,inner  
         ysep=6mm,fit=(d)(e),label={130:A}](f){}; % 这里的 label 命令用于标记块,它涵盖了块 D 和 E。 
    \draw[line] (a)-- (b);  
    \draw[line] (b)-- (c);  
    \draw[line] (d)-- (e);  
    \draw[line] (e)-- ($(b)!0.5!(c)$);  
    \draw[line] (d)-- ($(a)!0.5!(b)$);  
  
    \end{tikzpicture}  
\end{document}  

在这里,我们为所有块着色。您可以根据需要修改颜色。您还可以根据要求为全部或部分块着色。

输出:

Block Diagram using TikZ

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

iOS开发高手课 -〔戴铭〕

黄勇的OKR实战笔记 -〔黄勇〕

系统性能调优必知必会 -〔陶辉〕

Vim 实用技巧必知必会 -〔吴咏炜〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

Tony Bai · Go语言第一课 -〔Tony Bai〕

编程高手必学的内存知识 -〔海纳〕

eBPF核心技术与实战 -〔倪朋飞〕

B端体验设计入门课 -〔林远宏(汤圆)〕

好记忆不如烂笔头。留下您的足迹吧 :)