Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have created a block diagram with several nodes with text and I need to optimize the node creation and hopefully use the \foreach construct to automate the creation of each node with its associated text. Moreover, is it a good idea to connect (ant) and (ant2) nodes with the (ch) node as I suggested in the following code? I appreciate your ideas.

Here is my code:

\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-- I couldn't put long code in comments. You have provided a good code with nice looking radiation style. But technically (for this very system) the Wi-Fi decoration is not accepted and we should stick to the shape I suggested for wave propagation. So now, how could we possibly improve the following code? Also, any suggestions for the two dashed boxes with text? I appreciate your help.

My improved code:

\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}

And here is the output:

enter image description here

share|improve this question
    
Please don't edit the content of the answers without getting their consent (for styles and typos it's typically OK to edit though). I've moved your updated solution to your question and pinged @LaRiFaRi –  percusse Jul 26 at 7:14
    
Don't \usepackage{times}: it's an obsolete package. Either \usepackage{mathptmx} or \usepackage{newtxtext,newtxmath}, for getting Times font in your document. –  egreg Jul 26 at 8:07

2 Answers 2

I don't know how to draw notes with foreach and I guess this will not safe much time for you. You should take a look at chaining in the pgf-manual. But here is a proposal for the antennas with the wifi-decoration taken from this answer.

% 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}

enter image description here

share|improve this answer

This is a possible optimization.

enter image description here

  1. All node names (ch), (ant), (ant2), and (a,b,c,...g;a2,b2,...g2) are removed. Instead, matrix of nodes are used, where blk style is moved to table style and column/row sep are use for node distance. A total of 3 by 10 cells are constructed.

  2. Antenna becomes a node in matrix of nodes' withtriangle style`.

  3. All coordinates are constructed via matrix of nodes.

  4. Foreach is used with coordinates of form (mat-x-y).

Code

\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}
share|improve this answer
1  
Please try to run your code. I have error in line 49 in the first \foreach statement. {line 49: Package pgf Error: No shape named mat-1-1 is known } line 49: Package pgf Error: No shape named mat-1-2 is known } line 49: Package pgf Error: No shape named mat-1-2 is known } } –  AboAmmar Jul 25 at 23:09
1  
@AboAmmar -- After checking, pdfTeX, Version 3.1415926-2.3-1.40.12 (MiKTeX 2.9) (preloaded format=pdflatex 2012.3.11) will show the error you mentioned, but later version is fine --- Version 3.1415926-2.4-1.40.13, pdflatex 2013.8.24. –  Jesse Jul 26 at 1:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.