7

I'm trying to get the hang of TikZ as I need images I try to create them. I found this nice example from TeXample (you can find the code for the diagram there) which looks like nearly what I would like to create. I'm trying to remove some of the nodes but as I'm very new to TikZ I get errors that I don't understand yet. What I'm trying to accomplish is to keep the blue boxes of the transmitter and receiver but have them side by side and the transmitter containing Laser and SOA only and the receiver containing SOA and Rx only. I would like to undestand how I can add those blue dashed boxes and add more nodes so I can create my own diagrams. I have tried to get rid of Polarisation controller node but this gets me as far as having another empty box in place. I'm guessing that the problem is that I'm still drawing the nodes somehow. I would like to know how it actaully works.

original example

1
  • 2
    Instead of deleting the nodes leave the & characters and you shouldn't get any errors. Think of it as an array and ampersands are the column seperators. The chained ones are as the name implies chained together so you need to recreate them if you want to modify. See the manual for the chains library.
    – percusse
    Jun 20, 2012 at 22:44

1 Answer 1

11

I've simplified the code only to produce the elements you requested; I also left the original comments (whith some minor changes) which should give you an idea of what is doing each part of the code. I also didn't load the libraries that were not used in the simplified code.

The idea is to use a \matrix with one row (and four columns) to place the four elements; the entries of the matrix are joined using a chain; the blue dotted boxes are \nodes that use the fit library to enclose the specified elements of the matrix:

% Modification of the code
% BER measurement on fibre optical system
% Author: Jose Luis Diaz (taken from TeXample.net)
\documentclass{article}
\usepackage[a4paper, landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,scopes,fit}

\begin{document}
\sffamily\begin{tikzpicture}

  % Place all element in a matrix of nodes, called m
  % By default all nodes are rectangles with round corners
  % but some special sytles are defined also
  \matrix (m) [matrix of nodes, 
    column sep=5mm,
    row sep=1cm,
    nodes={draw, % General options for all nodes
      line width=1pt,
      anchor=center, 
      text centered,
      rounded corners,
      minimum width=1.5cm, minimum height=8mm
    },
    nodes in empty cells, 
    ]
  {
  % Row of symbols
  % m-1-1
    Laser 
  & % m-1-2
    SOA
  & % m-1-4
    [1cm]SOA 
  & % m-1-5
    Rx    
  \\
  };  % End of matrix

  % Now, connect all nodes in a chain.
  % The names of the nodes are automatically generated in the previous matrix. Since the
  % matrix was named ``m'', all nodes have the name m-row-column
  { [start chain,every on chain/.style={join}, every join/.style={line width=1pt}]
    \chainin (m-1-1);
    \chainin (m-1-2);
    \chainin (m-1-3);
    \chainin (m-1-4);
    };

%  Define the style for the blue dotted boxes
  \tikzset{blue dotted/.style={draw=blue!50!white, line width=1pt,
                               dash pattern=on 1pt off 4pt on 6pt off 4pt,
                                inner sep=4mm, rectangle, rounded corners}};
%
%  % Finally the blue dotted boxes are drawn as nodes fitted to other nodes
  \node (first dotted box) [blue dotted, 
                            fit = (m-1-1) (m-1-2)] {};
  \node (second dotted box) [blue dotted,
                            fit = (m-1-3) (m-1-4)] {};
%
%  % Since these boxes are nodes, it is easy to put text above or below them                          
  \node at (first dotted box.north) [above, inner sep=3mm] {\textbf{Transmitter}};
  \node at (second dotted box.north) [above, inner sep=3mm] {\textbf{Receiver}};
\end{tikzpicture}

\end{document}

enter image description here

And here's a simple addition showing, on a very basic level how you can add additional nodes to the matrix (now it's a matrix with two rows and five columns) and how to connect them, again, using a chain. Of course, the original example it's a little more elaborated since different node styles are defined, but these simple reductions can perhaps serve you as a starting point:

\documentclass{article}
\usepackage[a4paper, landscape]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,scopes,fit}

\begin{document}
\sffamily\begin{tikzpicture}

  % Place all element in a matrix of nodes, called m
  % By default all nodes are rectangles with round corners
  % but some special sytles are defined also
  \matrix (m) [matrix of nodes, 
    column sep=5mm,
    row sep=1cm,
    nodes={draw, % General options for all nodes
      line width=1pt,
      anchor=center, 
      text centered,
      rounded corners,
      minimum width=1.5cm, minimum height=8mm
    },
    nodes in empty cells, 
    ]
  {
  % First row of symbols
  % m-1-1
    Laser 
  & % m-1-2
    SOA
  & % m-1-4
    [1cm]SOA 
  & % m-1-5
    Rx
  &
  Node    
  \\
  % Second row of symbols
  Node
  &
  Node
  &
  Node
  &
  Node 
  &
  Node 
   \\
  };  % End of matrix

  % Now, connect all nodes in a chain.
  % The names of the nodes are automatically generated in the previous matrix. Since the
  % matrix was named ``m'', all nodes have the name m-row-column
  { [start chain,every on chain/.style={join}, every join/.style={line width=1pt}]
    \chainin (m-1-1);
    \chainin (m-1-2);
    \chainin (m-1-3);
    \chainin (m-1-4);
    \chainin (m-1-5);
    \chainin (m-2-5);
    \chainin (m-2-4);
    \chainin (m-2-3);
    \chainin (m-2-2);
    \chainin (m-2-1);
    };

%  Define the style for the blue dotted boxes
  \tikzset{blue dotted/.style={draw=blue!50!white, line width=1pt,
                               dash pattern=on 1pt off 4pt on 6pt off 4pt,
                                inner sep=4mm, rectangle, rounded corners}};
%
%  % Finally the blue dotted boxes are drawn as nodes fitted to other nodes
  \node (first dotted box) [blue dotted, 
                            fit = (m-1-1) (m-1-2)] {};
  \node (second dotted box) [blue dotted,
                            fit = (m-1-3) (m-1-4)] {};
%
%  % Since these boxes are nodes, it is easy to put text above or below them                          
  \node at (first dotted box.north) [above, inner sep=3mm] {\textbf{Transmitter}};
  \node at (second dotted box.north) [above, inner sep=3mm] {\textbf{Receiver}};
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .