个性化阅读
专注于IT技术分析

Latex使用TikZ框图

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

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

Tikz环境写为:

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

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

\documentclass[tikz, border=4mm]{standalone} % specifies the border
\usetikzlibrary{positioning, fit, calc} % used for the efficient working of the positioning system
\tikzset{block/.style={draw, thick, text width=3cm, minimum height=1.5cm, align=center}, % the align command is used to align the block diagram at the center
% the height command adjust the height of the block diagram
% here block diagram refers to the whole diagram, not the single block
% the thick command here signifies the border of all the blocks used inside the block diagram. You can change it to thin command if you want the thin edge of the blocks
line/.style={-latex}   % the lesser the width the greater will be the diagram window
}
\begin{document}

\begin{tikzpicture}
  \node[block] (a) {Source};
  \node[block, right=of a] (b) {Transmitter}; % these are the name of the blocks
  % you can use as many blocks by specifying the name and alphabets
  \node[block, right=of b] (c) {Channel};
  \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}; 
  % the yshift here specifies the shift in the position of the point on the y-axis. You can change the location according to the requirements.
  % the value mentioned is the distance from (b) to (c). If the value is 0.5, then the block will be at the center of (b) and (c).
  \node[draw, inner xsep=4mm, inner ysep=7mm, fit=(a)(b), label={100:P}](g){}; % it is used to draw the box outside the combination of blocks. Here a block will be drawn outside the block (a) and (b). The xsep and ysep are the dimensions for the box. The label command here is used to set the location of the name of the outer A and B blocks.
  \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); % you can change the location of the line to north, west, etc. depending on your requirements.
  \draw[line] (c)-- (d); % -- signifies the line between the two blocks (c) and (d).
 \draw[line] (d)-- (e);
\end{tikzpicture}
\end{document}

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

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

输出:

Latex使用TikZ框图

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

此类示例的代码如下:

\documentclass[tikz, border=5mm]{standalone} 
\usetikzlibrary{positioning, fit, calc} 
\tikzset{block/.style={draw, thick, text width=2cm , minimum height=1.3cm, align=center}, line/.style={-latex}   
}
\begin{document}

\begin{tikzpicture}
\node[block] (a) {A};
\node[block, right=of a] (b) {B}; 
\node[block, right=of b] (c) {C};
\node[block, right=of c] (d) {D}; % the commands used for the different location of different blocks
  \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}

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

输出:

Latex使用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) {B}; 
\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){}; % here label command is used to label the block, which covers block D and 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}

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

输出:

Latex使用TikZ框图

赞(0)
未经允许不得转载:srcmini » Latex使用TikZ框图

相关推荐

评论 抢沙发

评论前必须登录!