[Tex/LaTex] How to create the following figures in TikZ

diagramstikz-pgf

I'd like to create the following two figures with TikZ

enter image description here

enter image description here

I'm tikz beginner, can anybody give me some advice how to do that?

Now i've created the first figure:

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning, fit}
\begin{document}
\begin{tikzpicture}[
  mymatrix/.style={matrix of nodes, nodes=typetag, row sep=1em, anchor=base},
  mycontainer/.style={draw=black,inner sep=1ex},
  typetag/.style={draw=black, inner sep=1ex, anchor=west},
  title/.style={draw=none, color=black, inner sep=0pt}
  ]
  \matrix[mymatrix] (mx1) {
    |[title]|Text A \\
    Text 1 \\
    Text 2 \\
    Text 3 \\
    Text 4 \\
    Text 5 \\
    Text 6 \\
  };
  \matrix[mymatrix, right=of mx1.north east, matrix anchor=north west] (mx2) {
    |[title]|Text D \\
    Text 13 \\
    Text 14 \\
    Text 15 \\
    Text 16 \\
    Text 17 \\
    Text 18 \\
  };
  \matrix[mymatrix, right=of mx2.north east, matrix anchor=north west] (mx3) {
    |[title]|Text E \\
    Text 19 \\
  };
  \matrix[mymatrix, above=of mx2.north, matrix anchor=south west] (mx4) {
    |[title]|Text C \\
    Text 12 \\
  };
  \matrix[mymatrix, above=of mx2.north, matrix anchor=south east] (mx5) {
    |[title]|Text B \\
    Text 7 \\
    Text 8 \\
    Text 9 \\
    Text 10 \\
    Text 11 \\
  };

  \node[mycontainer, fit=(mx1)] (n1) {};
  \node[mycontainer, fit=(mx2)] (n2) {};
  \node[mycontainer, fit=(mx3)] (n3) {};
  \node[mycontainer, fit=(mx4)] (n4) {};
  \node[mycontainer, fit=(mx5)] (n5) {};

  \path[->,thick] (n1) edge (n2);
  \path[->,thick] (n3) edge (n2);
  \path[->,thick] (n4) edge (n2);
  \path[->,thick] (n5) edge (n2);
\end{tikzpicture}
\end{document}

As already mentioned I'm a TikZ beginner, so there are the following problems:

  1. How can the boxes around "Text 1", "Text 2" and so on have the
    same width and height? I'd like to have them centered, too.
  2. How can I create the arrows look like in the sample picture? Actually they seem badly arranged.
  3. How can I create the black boxes like in the sample picture?
  4. Is it the right way to use matrix to create this figure, could it be done easier?

Here is the LaTeX-code for the second figure:

\documentclass[tikz,border=5pt]{standalone}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[utf8]{inputenc}
%
\usepackage{tikz}
\usetikzlibrary{matrix,positioning-plus}


\begin{document}
\begin{tikzpicture}[
kreis/.style={circle,draw,minimum width=20pt},
every node/.style={align=center}]

\matrix(mat)[row sep=10pt, column sep=60pt] at (0,0) {
\node[kreis](k1) {}; &
\node[kreis](k2) {}; &
\node[kreis](k3) {}; &
\node[kreis](k4) {}; &
\node[kreis](k5) {}; &
\node[kreis](k6) {}; \\
};

 \draw[-latex] (k1) -- (k2);
 \draw[-latex] (k2) -- (k3);
 \draw[-latex] (k3) -- (k4);
 \draw[-latex] (k4) -- (k5);
 \draw[-latex] (k5) -- (k6);

\node [below,text width=2.5cm,font=\small] at (k1.south) {Text 1};
\node [below,text width=2.5cm,font=\small] at (k2.south) {Text 2};
\node [below,text width=2.5cm,font=\small] at (k3.south) {Text 3};
\node [below,text width=2.5cm,font=\small] at (k4.south) {Text 4};
\node [below,text width=2.5cm,font=\small] at (k5.south) {Text 5};
\node [below,text width=2.5cm,font=\small] at (k6.south) {Text 6};

\node (n1) [above=of k1] {};
\node (n2) [above=of k2] {};
\node (n3) [above=of k3] {};
\node (n4) [above=of k4] {};
\node (n5) [above=of k5] {};
\node (n6) [above=of k6] {};


 \draw[latex-latex] (n1) -- (n2) node[midway,above] {Text 7};
 \draw[latex-latex] (n2) -- (n3) node[midway,above] {Text 8};
 \draw[latex-latex] (n3) -- (n4) node[midway,above] {Text 9};
 \draw[latex-latex] (n4) -- (n5) node[midway,above] {Text 10};
 \draw[latex-latex] (n5) -- (n6) node[midway,above] {Text 11};

\node (m1) [above=of n1] {};
\node (m5) [above=of n5] {};
\node (m6) [above=of n6] {};


 \draw[latex-latex] (m1) -- (m5) node[midway,above] {Text 12};
 \draw[latex-latex] (m5) -- (m6) node[midway,above] {Text 13};

\node (k1) [above=of m1] {};
\node (k6) [above=of m6] {};

 \draw[latex-latex] (k1) -- (k6) node[midway,above] {Text 14};

\end{tikzpicture}
\end{document}

Actually I have the following problems with it:

  1. The arrows above the circles are to short, how can I adjust them?
  2. The text above the arrows is not vertically centered, what can I do here?
  3. How can I add vertical lines like in the picture above?

Another Question belongs to posting, how can I quickly generate and present a picture of my code?

Best Answer

For the first problem, nodes having the same size, see Dependent node size in TikZ (which solution I am going to use) and the linked questions which comes down to identifying the widest/tallest/deepest element and giving its measurements as options to the nodes (see text width, text height and text depth). I am using my node-families library from the linked question/answer.

The second problem, better connections between the matrices, see for example the very last example of my answer to Converging and diverging nodes in a flowchart and many others that use (angle) anchors and |-/-| as coordinate specifications. I am going to use a specific mix which includes the use of my paths.ortho library.

Your fourth point, regarding the use of matrices, I think that this is a good example to use matrices, even though we only have one column per matrix. For more complicated stuff and maybe even the matrices with the bold black border, one can use a combination of the fit and the backgrounds library. For examples see my answers [1] and [2].

Now, the black borders will require work. I am using here a solution that first places the fully filled node (Mikro- and Makroebene), and then adds a matrix directly below it which both are then fitted with a node that is only used for the drawn line around it.

There are many different approaches possible for this problem. It is our advantage that these problematic matrices are only placed in the lower row of all matrices and only to the right of other matrices. Note the xshift and yshift in the Title style. If you want to place it to the left, you need to use a negative yshift value (well, at least if you want to use the “correct” node distance). If you want to place such matrices above other nodes/matrices, you better first place the matrix part of this structure (then with a positive yshift for correction of the black border) and then the Title above it (but then without any shifting values).

My positioning-plus library adds another set of …=of … keys to the positioning library (and also loads fit). This introduces the north right=of <ref-node> key that basically is the same as right=of <ref-node>.north east, anchor=north west.

This library also allows us to use x_node_dist and y_node_dist as a reference to the node distances that is set with the node distance key.

Code

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning-plus, node-families, paths.ortho}
\tikzset{
  matrix inner xsep/.style={
   /tikz/every node/.append style/.expanded={inner xsep={\pgfkeysvalueof{/pgf/inner xsep}}},
   /pgf/inner xsep={#1}},
  matrix inner ysep/.style={
   /tikz/every node/.append style/.expanded={inner ysep={\pgfkeysvalueof{/pgf/inner ysep}}},
   /pgf/inner ysep={#1}},
  matrix inner sep/.style={matrix inner xsep={#1}, matrix inner ysep={#1}},
}
\begin{document}
\begin{tikzpicture}[
  my matrix*/.style={
    matrix inner sep=+1em,
    matrix of nodes,
    nodes={typetag, Minimum Width=m, text depth=+0pt},
    row sep=+.5em,
    Minimum Width=M},
  my matrix/.style={
    my matrix*, draw,
    row 1/.append style={nodes=title}},
  title/.style={draw=none, font=\bfseries},
  Title/.style={
    title,
    outer sep=+0pt,
    Minimum Width=M,
    fill, text=white,
    text depth=+0pt,
    yshift=+-.25cm,
    xshift=+.25cm,
    inner ysep=.5em},
  fitty/.style={fit=(#1')(#1''), line width=+.25cm, inner sep=+.125cm, draw, name=#1},
  typetag/.style={draw=black, inner sep=1ex, align=center},
  ]

  \matrix[my matrix] (mx1) {
    Text A \\
    Text 1 \\
    Text 2 \\
    Text 3 \\
    Text 4 \\
    Text 5 \\
    Text 6 \\
  };
  \node[Title, north right=of mx1] (mx2') {Text D};
  \matrix[my matrix, below=+0pt of mx2'] (mx2'') {
    \node {Text ABC};\\
    Text 13 \\
    Text 14 \\
    Text 15 \\
    Text 16 \\
    Text 17 \\
    \node{Text 18}; \\
  };
  \node[fitty=mx2]{};
  \node[Title, north right=of mx2] (mx3') {Text E};

  \matrix[my matrix*, below=+0pt of mx3'] (mx3'') {Text 19 \\};
  \node[fitty=mx3] {};

  \matrix[my matrix, above=of mx2.north, matrix anchor=south east, shift=(left:.5*x_node_dist)] (mx5) {
    Text B \\
    \node{Text 7}; \\
    Text 8 \\
    Text 9 \\
    \node{Text 10}; \\
    Text 11 \\
  };
  \matrix[my matrix, north right=of mx5] (mx4) {
    Text C \\
    \node {Text 12}; \\
  };

  \path [thick, ->, >=latex]
    {
      [to path={([shift=(down:y_node_dist)]\tikztostart.north east) -- ([shift=(down:y_node_dist)]\tikztotarget.north west)}] 
      (mx1) edge     (mx2)
      (mx2) edge[<-] (mx3)
    }
    [|*] ([shift=(left:x_node_dist)]  mx5.south east) edge (mx2)
         ([shift=(right:x_node_dist)] mx4.south west) edge (mx2)
  ;
\end{tikzpicture}
\end{document}

Output

enter image description here