优化文本节点的创建

优化文本节点的创建

我创建了一个包含多个带文本节点的框图,我需要优化节点创建,并希望使用构造自动创建每个节点及其相关文本。此外,按照我在以下代码中建议的那样将节点与节点\foreach连接起来是否是个好主意?我很欣赏你的想法。(ant)(ant2)(ch)

这是我的代码:

\documentclass[10pt,a4paper]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usetikzlibrary{arrows,positioning,shapes}
\begin{document}

\def\sp{4mm}
\begin{tikzpicture}
[line join=round, >=latex', blk/.style={draw,minimum height=1.4cm,align=center}, node distance=\sp, scale=0.8,line width=0.7pt]

\node  [blk] (a) at (-8,5)  {Binary \\sources};
\node  [blk, right=of a](b) {Source \\coding};
\node  [blk, right=of b](c) {Channel \\coding};
\node  [blk, right=of c](d) {Pulse\\ modulation};
\node  [blk, right=of d](e) {Bandpass\\ modulation};
\node  [blk, right=of e](f) {Digital-\\to-analog\\ converting};
\node  [blk, right=of f](g) {RF\\ front end};

\node  [blk, below left= 10*\sp and 0.0*\sp of a] (a2){Binary \\sink};
\node  [blk, right=of a2]       (b2){Source \\decoding};
\node  [blk, right=of b2]       (c2){Channel \\decoding};
\node  [blk, right=of c2]       (d2){Detection};
\node  [blk, right=of d2]       (e2){Demod.\\and\\sampling};
\node  [blk, right=of e2]       (f2){Timing,\\synch, and\\equalization};
\node  [blk, right=of f2]       (g2){Analog-\\to-digital\\ converting};
\node  [blk, right=of g2]       (h2){RF\\ front end};

\foreach \x/\y in {a/b,b/c,c/d,d/e,e/f,f/g}
\draw [->](\x) -- (\y);

\foreach \x/\y in {a2/b2,b2/c2,c2/d2,d2/e2,e2/f2,f2/g2,g2/h2}
\draw [<-](\x) -- (\y);

% antennas, ant1:
\node [circle,minimum height=\sp,above right=of g](ant){};
\draw (ant.west)--(ant.east)-- (ant.south)--(ant.west)--cycle;
\draw (g) -| (ant.center);
% ant2:
\node [circle,minimum height=\sp,above right=of h2](ant2){};
\draw (ant2.west)--(ant2.east)--(ant2.south) -- (ant2.west)--cycle;
\draw (h2) -| (ant2.center);

%channel
\node [draw,minimum height=1cm,align=center,above=4*\sp of ant2](ch){Wireless\\channel};

%Transmissions, Upper:
\node [circle,minimum height=.7\sp](tr1)at($(ant)!.55!(ch.north)$){};
\draw [->](ant)--(tr1.south west)--(tr1.north east)--(ch.north);
%Lower:
\node [circle,minimum height=.7\sp](tr2)at($(ch.south)!.45!(ant2)$){};
\draw [->](ch.south)--(tr2.south west)--(tr2.north east)--(ant2);

\end{tikzpicture}

\end{document}

@LaRiFaRi——我无法在注释中写长代码。您提供了一个很好的代码,辐射样式看起来不错。但从技术上讲(对于这个系统而言),Wi-Fi 装饰是不可接受的,我们应该坚持我建议的波传播形状。那么现在,我们如何才能改进以下代码?另外,对于两个带文本的虚线框,您有什么建议吗?感谢您的帮助。

我的改进代码:

\documentclass[10pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{times}
\usetikzlibrary{arrows,positioning,calc,decorations.pathreplacing}
\begin{document}

\def\sp{4mm}
\tikzset{
blk/.style={draw,minimum height=1.4cm,align=center}
}
\tikzset{
sq/.style={shape=rectangle,sloped,minimum height=0.7\sp, minimum width=0.7\sp}
}
\tikzset{
dashedBox/.style={shape=rectangle,draw,dashed,minimum height=6*\sp,align=center}
}
\begin{tikzpicture}[line join=miter,line cap=round, >=latex', node distance=\sp, scale=0.8,line width=0.7pt]

\node  [blk] (a) at (-8,5)  {Binary\\sources};
\node  [blk, right=of a](b) {Source\\coding};
\node  [blk, right=of b](c) {Channel\\coding};
\node  [blk, right=of c](d) {Pulse\\modulation};
\node  [blk, right=of d](e) {Bandpass\\modulation};
\node  [blk, right=of e](f) {Digital-\\to-analog\\ converting};
\node  [blk, right=of f](g) {RF\\front end};

% Dashed Box
%\coordinate (L)at([yshift=3*\sp,xshift=-.5*\sp] d.north west);
%\coordinate (R)at([yshift=-.5*\sp,xshift=.5*\sp]e.south east);
%\draw [dashed](L) rectangle (R);
\node [dashedBox,yshift=\sp,minimum width=11.2*\sp](B1)at($(d.west)!0.5!(e.east)$){};
\node at(B1.north)[below,align=center]{Digital Modulation};


\node  [blk, below= 9*\sp of a.west, anchor=west] (a2){Binary\\sink};
\node  [blk, right=of a2]       (b2){Source\\decoding};
\node  [blk, right=of b2]       (c2){Channel\\decoding};
\node  [blk, right=of c2]       (d2){Detection};
\node  [blk, right=of d2]       (e2){Demod.\\and\\sampling};
\node  [blk, right=of e2]       (f2){Timing,\\synch, and\\equalization};
\node  [blk, right=of f2]       (g2){Analog-\\to-digital\\converting};
\node  [blk, right=of g2]       (h2){RF\\front end};

% Dashed Box
\node  [dashedBox,yshift=1*\sp,minimum width=10*\sp](B2)at($(d2.west)!0.5!(e2.east)$) {};
\node at(B2.north)[below,align=center]{Digital Demodulation};

\foreach \x/\y in {a/b,b/c,c/d,d/e,e/f,f/g}
\draw [->](\x) -- (\y);

\foreach \x/\y in {a2/b2,b2/c2,c2/d2,d2/e2,e2/f2,f2/g2,g2/h2}
\draw [<-](\x) -- (\y);

% antennas, ant1:
\node [circle,minimum height=\sp,above right=of g](ant){};
\draw (ant.west)--(ant.east)-- (ant.south)--(ant.west)--cycle;
\draw (g) -| (ant.center);
% ant2:
\node [circle,minimum height=\sp,above right=of h2](ant2){};
\draw (ant2.west)--(ant2.east)--(ant2.south) -- (ant2.west)--cycle;
\draw (h2) -| (ant2.center);

%channel
\path (ant)--node(ch)[draw,align=center]{Wireless\\channel}(ant2.south east);

%Transmission 1
\path(ant)--node(tr1)[sq]{}(ch);
\draw [->,shorten >=2pt](ant)--(tr1.south east)--(tr1.north west)--(ch);
%Transmission 2
\path(ch)--node(tr2)[sq]{}(ant2);
\draw [->,shorten >=2pt](ch)--(tr2.north east)--(tr2.south west)--(ant2.center);
\end{tikzpicture}
\end{document}

输出如下:

在此处输入图片描述

答案1

我不知道如何使用 foreach 绘制注释,我想这不会为您节省太多时间。您应该查看 pgf 手册中的链接。但这里有一个使用 wifi-decoration 的天线提案,取自这个答案

% arara: pdflatex

\documentclass[10pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{times}
\usetikzlibrary{arrows,positioning,calc,decorations.pathreplacing}
\tikzset{blk/.style={draw,minimum height=1.4cm,align=center}}
\tikzset{radiation/.style={{decorate,decoration={expanding waves,angle=90,segment length=4pt}}}}

\begin{document}
\def\sp{4mm}
\begin{tikzpicture}[line join=round, >=latex', node distance=\sp, scale=0.8,line width=0.7pt]

\node  [blk] (a) at (-8,5)  {Binary\\sources};
\node  [blk, right=of a](b) {Source\\coding};
\node  [blk, right=of b](c) {Channel\\coding};
\node  [blk, right=of c](d) {Pulse\\ modulation};
\node  [blk, right=of d](e) {Bandpass\\modulation};
\node  [blk, right=of e](f) {Digital-\\to-analog\\ converting};
\node  [blk, right=of f](g) {RF\\front end};

\node  [blk, below= 9.2*\sp of a.west, anchor=west] (a2){Binary\\sink};
\node  [blk, right=of a2]       (b2){Source\\decoding};
\node  [blk, right=of b2]       (c2){Channel\\decoding};
\node  [blk, right=of c2]       (d2){Detection};
\node  [blk, right=of d2]       (e2){Demod.\\and\\sampling};
\node  [blk, right=of e2]       (f2){Timing,\\synch, and\\equalization};
\node  [blk, right=of f2]       (g2){Analog-\\to-digital\\converting};
\node  [blk, right=of g2]       (h2){RF\\front end};

\foreach \x/\y in {a/b,b/c,c/d,d/e,e/f,f/g}
\draw [->](\x) -- (\y);

\foreach \x/\y in {a2/b2,b2/c2,c2/d2,d2/e2,e2/f2,f2/g2,g2/h2}
\draw [<-](\x) -- (\y);

% antennas, ant1:
\node [circle,minimum height=\sp,above right=of g](ant){};
\draw (ant.west)--(ant.east)-- (ant.south)--(ant.west)--cycle;
\draw (g) -| (ant.center);
% ant2:
\node [circle,minimum height=\sp,above right=of h2](ant2){};
\draw (ant2.west)--(ant2.east)--(ant2.south) -- (ant2.west)--cycle;
\draw (h2) -| (ant2.center);

%channel
\node [draw,minimum height=1cm,align=center,right=6*\sp of g](ch){Wireless\\channel};
\draw[radiation,decoration={angle=30}] ([xshift=\sp]ant.south east) -- ([xshift=-\sp]ch.north west);
\draw[radiation,decoration={angle=30}] ([yshift=-\sp]ch.south) -- ([yshift=\sp]ant2.north);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是一个可能的优化。

在此处输入图片描述

  1. 所有节点名称 (ch)、(ant)、(ant2) 和 (a,b,c,...g;a2,b2,...g2) 均被删除。取而代之的是,matrix of nodes其中blk样式被移至table style并且column/row sep用于节点距离。总共构建了 3 x 10 个单元格。

  2. matrix of nodes' with天线呈三角形的节点。

  3. 所有坐标均通过 构建matrix of nodes

  4. Foreach 与形式 的坐标一起使用(mat-x-y)

代码

\documentclass[10pt,a4paper]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,decorations.pathreplacing}
\usetikzlibrary{arrows,positioning,shapes}
\usetikzlibrary{matrix}
\begin{document}

\def\sp{4mm}
\tikzset{triangle/.style={draw,regular polygon, regular polygon sides=3,scale=0.1,rotate=180},           % antenna 
table/.style={
  matrix of nodes,
  row sep=\sp, column sep=\sp,
  nodes={draw,rectangle, %minimum width=2cm, 
align=center}, anchor=center,text depth=1.5ex,  text height=1.4cm, 
},
radiation/.style={{decorate,decoration={expanding waves,angle=90,segment length=4pt}}}
}
\begin{tikzpicture}
[line join=round, >=latex', 
%blk/.style={draw=none}, 
scale=0.8,line width=0.7pt]
\matrix (mat) [table]{
\node  [] {Binary \\ \\sources}; &
\node  [] {Source \\\\coding}; &
\node  [] {Channel \\\\coding};&
\node  [] {Pulse\\ \\modulation}; &
\node  [] {Bandpass\\ \\modulation};&
\node  [] {Digital-\\to-analog\\converting};&
\node  [] {RF\\ \\ front end}; & 
\node[triangle,anchor=north,yshift=-10cm]{};\\
 & & & & & & & & &\node []{Wireless\\ \\channel};\\
\node  [] {Binary \\ \\sink}; &
\node  [] {Source \\ \\decoding}; &
\node  [] {Channel \\ \\decoding}; &
\node  [] {Detection\\ }; &
\node  [] {Demod.\\and\\sampling};&
\node  [] {Timing,\\synch, and\\equalization}; &
\node  [] {Analog-\\to-digital\\ converting}; & \node  [] {RF\\ \\front end};
& \node  [triangle,anchor=north,yshift=-10cm]{};\\
};
\foreach \x/\y in {1/2,2/3,3/4,4/5,5/6,6/7}{
\draw [->] (mat-1-\x.east) -- (mat-1-\y.west);
}
\foreach \x/\y in {1/2,2/3,3/4,4/5,5/6,6/7,7/8} {
\draw [->] (mat-3-\x.east) -- (mat-3-\y.west);
}
%
\draw[] (mat-1-7.east)  -|($(mat-1-8)+(0,4pt)$);
\draw[] (mat-3-8.east)  -| ($(mat-3-9)+(0,4pt)$);

\draw[radiation,decoration={angle=30}] (mat-1-8.center) -- (mat-2-10.north west);
\draw[radiation,decoration={angle=30}] (mat-2-10.south west) -- (mat-3-9.north east);
\end{tikzpicture}

\end{document}

相关内容