I'd like to create the following two figures with TikZ
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:
- 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. - How can I create the arrows look like in the sample picture? Actually they seem badly arranged.
- How can I create the black boxes like in the sample picture?
- 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:
- The arrows above the circles are to short, how can I adjust them?
- The text above the arrows is not vertically centered, what can I do here?
- 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
andtext depth
). I am using mynode-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 mypaths.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 thebackgrounds
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
fit
ted with a node that is only used for thedraw
n 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
andyshift
in theTitle
style. If you want to place it to the left, you need to use a negativeyshift
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 positiveyshift
for correction of the black border) and then theTitle
above it (but then without any shifting values).My
positioning-plus
library adds another set of…=of …
keys to thepositioning
library (and also loadsfit
). This introduces thenorth right=of <ref-node>
key that basically is the same asright=of <ref-node>.north east, anchor=north west
.This library also allows us to use
x_node_dist
andy_node_dist
as a reference to the node distances that is set with thenode distance
key.Code
Output