12

I have a problem with the connection of two blocks. I have the following code

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{shapes,shapes.geometric,arrows,fit,calc,positioning,automata}
\usepackage{amsmath}

\newcommand{\Block}[4]{
    \tikzstyle{input} = [coordinate]
    \tikzstyle{block} = [draw, fill=white!10, rectangle,minimum height=1cm, minimum width=1cm]
    \tikzstyle{container} = [draw, rectangle, dashed, inner sep=1em]
    \begin{tikzpicture}
        \node [block, minimum width=2cm, minimum height=0.5cm] (di)
            {#1};
        \node [block, minimum width=2cm, minimum height=0.5cm,below of  = di, node distance = 0.6cm] (BlockName)
            {#2};
        \node [block, minimum width=2cm, minimum height=0.5cm,below of  = BlockName, node distance = 0.6cm] (xi)
            {#3};
        \node[container, fit=(di)(BlockName)(xi)] (FTCBlockVal) (Blk)
            {};
        \node at (Blk.north west) [above right,node distance=0 and 0] 
            {#4};
    \end{tikzpicture}
}

\begin{document}

\Block{Val1}{Val2}{Val3}{Name}
\Block{Val1}{Val2}{Val3}{Name}


\end{document}

where I have now command to create a block. I will use many the same blocks. The result is

enter image description here

So now I have a question. How connect this two blocks using an arrow?

10

If blocks are used inside a tikzpicture Andrew's pic solution is one option. I propose an alternative with a matrix node which simplifies the block definition. Being container style defined as a matrix of nodes

container/.style={
    matrix of nodes,
    draw, dashed,
    inner sep=1em,
    nodes={block, anchor=center},
    row sep=.3cm,
    label={[anchor=south west]above left:#1},

the block command is just:

\newcommand{\Block}[5][]{
    \matrix[container=#5,#1] (#5) {
        #2\\#3\\#4\\}; 
}

The optional parameter can be used for blocks placement while the four mandatory parameters follow original \Block definition. In this case the fourth mandatory parameter acts as matrix name and label. With this matrix definition, inner nodes are identified as matrixname-1-1|-2-1|-3-1. An example of use:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,matrix}
\usepackage{amsmath}

\tikzset{
    input/.style={coordinate},
    block/.style={
        draw, solid, fill=white!10, 
        minimum width=2cm, minimum height=.5cm,
        inner sep=.3333em},
    container/.style={
        matrix of nodes,
        draw, dashed,
        inner sep=1em,
        nodes={block, anchor=center},
        row sep=.3cm,
        label={[anchor=south west]above left:#1},
    }
}

\newcommand{\Block}[5][]{
    \matrix[container=#5,#1] (#5) {
        #2\\#3\\#4\\}; 
}

\begin{document}

\begin{tikzpicture}
\Block{Val1}{Val2}{Val3}{NameA}
\Block[right=1cm of NameA] {Val1}{Val2}{Val3}{NameB}
\draw[->] (NameA)-- coordinate (aux) (NameB);
\draw[->] (NameA-1-1)--(NameA-1-1-|aux) |-(NameB-3-1);

\Block[below right=1cm and 2cm of NameB] {Val1}{Val2}{Val3}{NameC}
\draw (NameC-3-1)-|(NameB);
\end{tikzpicture}

\end{document}

enter image description here

If blocks are drawn in its own tikzpicture and distributed over the page, remember picture option has to be used inside the tikzpicture parameters in order to allow to draw links (with another tikzpicture) between them

9

I would define your block using \tikzset{pics/block/.style=....}. This way you can still use your \Block macro, so that

\Block{Val1}{Val2}{Val3}{Name}
\Block{Val1}{Val2}{Val3}{Name}

will produce the image in the original question. The advantage of a pics definition is that you can also put two blocks side-by-side in the same tizkpicture environment. Further, via a parameter given to the `pic you can label the nodes in each block differently, which means that you can easily refer to them in code like

\begin{tikzpicture}
  \draw(0,0) pic{block={A,Val1,Val2,Val3,Name1}};% labels Adi, ABlockName...
  \draw(4,0) pic{block={B,Val1,Val2,Val3,Name1}};% labels Bdi, BBlockName...
  \draw[->,blue](Adi.east) -- (Bdi.west);
\end{tikzpicture}

will produce

enter image description here

Here is the full code:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{shapes,shapes.geometric,arrows,fit,calc,positioning,automata}
\usepackage{amsmath}

\tikzset{
  block/.style = {draw, fill=white!10, rectangle, minimum height=5mm, minimum width=2cm},
  container/.style = {draw, rectangle, dashed, inner sep=1em},
  pics/block/.style args={#1,#2,#3,#4,#5}{% label, val1, val2, val3, name
    code = {
        \node[block] (#1di) {#2};
        \node[block, below of=#1di, node distance = 0.6cm] (#1BlockName) {#3};
        \node[block, below of=#1BlockName, node distance = 0.6cm] (#1xi) {#4};
        \node[container, fit=(#1di)(#1BlockName)(#1xi)] (FTCBlockVal) (#1Blk){};
        \node at (#1Blk.north west) [above right,node distance=0 and 0] {#5};
     }
  }
}
%\Block{label}{val1}{val2}{val3}{name}, label empty by default
\newcommand{\Block}[5][]{%
  \begin{tikzpicture}
     \draw (0,0) pic{block={#1,#2,#3,#4,#5}};
  \end{tikzpicture}%
}

\begin{document}

  \Block{Val1}{Val2}{Val3}{Name}
  \Block{Val1}{Val2}{Val3}{Name}

  \bigskip

  \begin{tikzpicture}
    \draw(0,0) pic{block={A,Val1,Val2,Val3,Name1}};
    \draw(4,0) pic{block={B,Val1,Val2,Val3,Name1}};
    \draw[->,blue](Adi.east) -- (Bdi.west);
  \end{tikzpicture}
\end{document}

Note that I have put all of your style definitions together inside \tikzset{...}. I have also reduced the definition of block as you had different definitions for the minimum height and width in the style definitions and when they were actually used in the nodes.

0

My suggestion is to use an application with a GUI, ie., Dia. The application can export to LATEX PGF macro's. "Dia is a GTK+ based diagram creation program for GNU/Linux, MacOS X, Unix, and Windows, and is released under the GPL license." (https://wiki.gnome.org/action/show/Apps/Dia?action=show&redirect=Dia).

You can easily include the diagram as follows in your document, for example:

\usepackage{tikz}

\begin{document}

\vspace{1em}
\begin{figure}[bth]
    \centering
    \input{connect-two-blocks.pgf}
    \caption[Connect two blocks]{Connect two blocks.} 
    \label{fig:connecttwoblocks}
\end{figure}

\end{document}

The pgf file below contains the connecttwoblocks figure drawn with Dia. Note that the size of the text "Valx" and "Name" can be adjusted with the scale property [framed,scale=0.7,every node/.style={scale=0.8}].

\ifx\du\undefined
  \newlength{\du}
\fi
\setlength{\du}{15\unitlength}
\begin{tikzpicture}[framed,scale=0.45,every node/.style={scale=0.8}]
\pgftransformxscale{1.000000}
\pgftransformyscale{-1.000000}
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (7.350000\du,2.350000\du)--(7.350000\du,15.500000\du)--(18.350000\du,15.500000\du)--(18.350000\du,2.350000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{{1.000000\du}{1.000000\du}}{0\du}
\pgfsetdash{{1.000000\du}{1.000000\du}}{0\du}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (7.350000\du,2.350000\du)--(7.350000\du,15.500000\du)--(18.350000\du,15.500000\du)--(18.350000\du,2.350000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (12.850000\du,9.120000\du){};
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (8.950000\du,4.300000\du)--(8.950000\du,6.550000\du)--(16.550000\du,6.550000\du)--(16.550000\du,4.300000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (8.950000\du,4.300000\du)--(8.950000\du,6.550000\du)--(16.550000\du,6.550000\du)--(16.550000\du,4.300000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (12.750000\du,5.620000\du){Val1};
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (9.025000\du,7.350000\du)--(9.025000\du,9.750000\du)--(16.475000\du,9.750000\du)--(16.475000\du,7.350000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (9.025000\du,7.350000\du)--(9.025000\du,9.750000\du)--(16.475000\du,9.750000\du)--(16.475000\du,7.350000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (12.750000\du,8.745000\du){Val2};
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (9.025000\du,10.550000\du)--(9.025000\du,13.100000\du)--(16.475000\du,13.100000\du)--(16.475000\du,10.550000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (9.025000\du,10.550000\du)--(9.025000\du,13.100000\du)--(16.475000\du,13.100000\du)--(16.475000\du,10.550000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (12.750000\du,12.020000\du){Val3};
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node[anchor=west] at (7.700000\du,1.600000\du){Name1};
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (23.905000\du,2.275000\du)--(23.905000\du,15.425000\du)--(34.905000\du,15.425000\du)--(34.905000\du,2.275000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{{1.000000\du}{1.000000\du}}{0\du}
\pgfsetdash{{1.000000\du}{1.000000\du}}{0\du}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (23.905000\du,2.275000\du)--(23.905000\du,15.425000\du)--(34.905000\du,15.425000\du)--(34.905000\du,2.275000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (29.405000\du,9.045000\du){};
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (25.505000\du,4.225000\du)--(25.505000\du,6.475000\du)--(33.105000\du,6.475000\du)--(33.105000\du,4.225000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (25.505000\du,4.225000\du)--(25.505000\du,6.475000\du)--(33.105000\du,6.475000\du)--(33.105000\du,4.225000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (29.305000\du,5.545000\du){Val1};
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (25.580000\du,7.275000\du)--(25.580000\du,9.675000\du)--(33.030000\du,9.675000\du)--(33.030000\du,7.275000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (25.580000\du,7.275000\du)--(25.580000\du,9.675000\du)--(33.030000\du,9.675000\du)--(33.030000\du,7.275000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (29.305000\du,8.670000\du){Val2};
\definecolor{dialinecolor}{rgb}{1.000000, 1.000000, 1.000000}
\pgfsetfillcolor{dialinecolor}
\fill (25.580000\du,10.475000\du)--(25.580000\du,13.025000\du)--(33.030000\du,13.025000\du)--(33.030000\du,10.475000\du)--cycle;
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetmiterjoin
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (25.580000\du,10.475000\du)--(25.580000\du,13.025000\du)--(33.030000\du,13.025000\du)--(33.030000\du,10.475000\du)--cycle;
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node at (29.305000\du,11.945000\du){Val3};
% setfont left to latex
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\node[anchor=west] at (24.650000\du,1.700000\du){Name2};
\pgfsetlinewidth{0.100000\du}
\pgfsetdash{}{0pt}
\pgfsetdash{}{0pt}
\pgfsetbuttcap
{
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetfillcolor{dialinecolor}
% was here!!!
\pgfsetarrowsend{stealth}
\definecolor{dialinecolor}{rgb}{0.000000, 0.000000, 0.000000}
\pgfsetstrokecolor{dialinecolor}
\draw (16.599699\du,5.402364\du)--(25.505000\du,5.350000\du);
}
\end{tikzpicture}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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