6

I need help with a diagram for my thesis. After hours and hours of reading the TikZ manual and trying to recreate the diagram above I only get a useless result.

This is the diagram I need to draw with Ti*k*Z.

I add my rather embarassing result. However, it's far from what it should look like in the end. I would be really thankful if someone could help me out of my mess.

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
    \begin{tikzpicture}
    [node distance = 1cm, auto,font=\footnotesize,
    every node/.style={node distance=3cm},
    comment/.style={rectangle, inner sep= 5pt, text width=4cm, node distance=0.25cm, font=\scriptsize\sffamily},
    force/.style={rectangle, draw, fill=black!10, inner sep=5pt, text width=4cm, text badly centered, minimum height=1.2cm, font=\bfseries\footnotesize\sffamily}]


    \node [force] (Frauen) {Schweizer Frauen\\ freiwillig};


    \node [force, left=0cm of Frauen] (Maenner) {Schweizer Männer\\
    dienstpflichtig};
    \node [force, right=0cm of Frauen] (Auslaender) {Ausländer \\(niedergelassene Männer mit ausländischer Staatsangehörigkeit)\\ freiwillig, nur Zivilschutz};
    \node [force, right=0cm of Auslaender] (Auslaenderinnen) {Ausländerinnen (niedergelassene Frauen mit ausländischer Staatsangehörigkeit)\\ freiwillig, nur Zivilschutz};

    \node [force, below of=Maenner] (tauglich) {militärdienst\\-tauglich?};
    \node [force, right=0.5cm of tauglich] (schutzdienst) {schutzdienst\\-tauglich\\?};
    \node [force, right=0.5cm of schutzdienst] (IV) {IV bzw.\\Integritäts-\\schädigung\\>40\%\\?};
    \node [force, right=0.5cm of IV] (EG) {keine\\Ersatz-\\abgabe};


    \node [force, below of=tauglich] (Gewissen) {mit\\Gewissen\\vereinbar?};
    \node [force, right=0.5cm of Gewissen] (Zivildienst) {Zivildienst};
    \node [force, right=0.5cm of Zivildienst] (Zivilschutz) {Zivilschutz\\ und Ersatz-\\abgabe};    
    \node [force, right=0.5cm of Zivilschutz] (Ersatzabgabe) {Ersatzabgabe};    


    \node [force, below of=Gewissen] (Armee) {Armee};

    \path[->,thick] 
    (Maenner) edge (tauglich)
    (tauglich) edge (Gewissen)
    (Gewissen) edge (Armee)
    (Gewissen) edge (Zivildienst)
    (tauglich) edge (schutzdienst)
    (Auslaender) edge (schutzdienst)
    (Auslaenderinnen) edge (schutzdienst)
    (Frauen) edge (tauglich)
    (schutzdienst) edge (IV)
    (IV) edge (EG)
    (schutzdienst) edge (Zivilschutz)
    (IV) edge (Ersatzabgabe);

    \node [comment, below=0.25 of tauglich] {JA};
    \node [comment, below=0.25 of Gewissen] {JA};
    \node [comment, below=0.25 of schutzdienst] {JA};
    \node [comment, below=0.25 of IV] {NEIN};
    \node [comment, right=0 of tauglich] {NEIN};
    \node [comment, right=0 of schutzdienst] {NEIN};
    \node [comment, right=0 of IV] {JA};
    \node [comment, right=0 of Gewissen] {NEIN};

    \end{tikzpicture} 
\end{document}
2
  • Maybe a little bit off topic but would you like a finished result or a step for step guide? Sep 2, 2018 at 15:33
  • 1
    Both would be fine for me. But in the end I want to improve my skills with tikz so I would try to understand what the person has done. Should I have further questions about the finished result I would just ask again to fully understand it :)
    – danik
    Sep 2, 2018 at 15:55

2 Answers 2

9

Not quite a complete solution, though most features have been implemented. The shading in one of the circles is missing, as is the "comments" placed in the flow charts. The code has some comments, ask if you want more explanation for specific things.

I can mention that for the -|/|- syntax, some more explanation is found here: TikZ: What EXACTLY does the the |- notation for arrows do?

Do note that there are multiple ways of doing this, you might get suggestions from other users that you like better.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usetikzlibrary{
  positioning,
  shapes.geometric, % for diamond shape
  chains, % for automatic placement nodes relative to the previous one, in "chains"
  calc, % for ($...$) syntax of coordinate calculation
  fit, % to create a node that fits around other specified nodes
  arrows.meta, % for Triangle arrow tip
  backgrounds, % for "on background layer", to place something behind already existing stuff
  quotes, % for edge["label"] syntax, easy way of placing text along an edge
  matrix % for matrix of nodes
}
\begin{document}
\begin{tikzpicture}[
    font=\footnotesize\sffamily,
    baserect/.style={ % base style for rectangles on top of the diagram
       text width=3cm,
       align=center,
       draw
    },
    diam/.style={ % style for the black diamonds
       diamond,
       draw,
       align=center,
       text width=1.5cm,
       inner sep=1pt,
       fill=black,
       text=white
    },
    basecirc/.style={ % basic circle style, used in the next three styles
       circle,
       draw,
       align=center,
       text width=1.5cm
    },
    persohnlich/.style={
        basecirc,
        fill=black!30,
        text=white
    },
    finanziell/.style={
        basecirc,
        fill=black!15
    },
    keine/.style={
        basecirc,
        fill=white
    },
]


\begin{scope}[
   % in this scope, start a chain called groups. nodes in this chain are named groups-1, groups-2 etc.
   start chain=groups,
   % set distance between nodes in the chain
   node distance=-\pgflinewidth,
   every node/.append style={ % set style for all the nodes in the scope
     baserect,
     on chain, % adds all the nodes to the chain
     font=\footnotesize\sffamily\bfseries,
     minimum height=2cm,
     fill=black!20,
   }   
]
    % now add all the nodes, no additional styling or positioning needed
    \node {Schweizer Männer};
    \node {Schweizer Frauen};
    \node {Ausländer \\(niedergelassene Männer mit ausländischer Staatsangehörigkeit)};
    \node {Ausländerinnen (niedergelassene Frauen mit ausländischer Staatsangehörigkeit)};
\end{scope}

% this scope is basically the same as the previous one. 
% one main difference is that the first node in the chain is
% placed on the lower edge of the first node in the previous chain
\begin{scope}[
   start chain=values,
   node distance=-\pgflinewidth,
   every node/.append style={
     baserect,
     on chain,
     minimum height=1cm
   }   
]
    \node [anchor=north] at (groups-1.south)  {dienstpflichtig};
    \node {freiwillig};
    \node {freiwillig, nur Zivilschutz};
    \node {freiwillig, nur Zivilschutz};
\end{scope}

% the nodes in the first row of the flow chart
\node [diam, below=2cm of values-1] (tauglich) {militärdienst\\-tauglich?};
\node [diam, right=3cm of tauglich] (schutzdienst) {schutzdienst\\-tauglich?};
\node [diam, right=0.75of schutzdienst, inner sep=-2pt] (IV) {IV bzw.\\Integritäts-\\schädigung\\>40\%?};
\node [keine, right=0.75cm of IV] (EG) {keine\\Ersatz-\\abgabe};

% second row of nodes in flow chart
\node [diam, below=of tauglich] (Gewissen) {mit\\Gewissen\\vereinbar?};
% in the following node, at=(schutzdienst|-Gewissen) means
% place this node at the x-coordinate of schutzdienst, and y-coordinate of Gewissen
\node [finanziell, at=(schutzdienst|-Gewissen)] (Zivilschutz) {Zivilschutz\\ und Ersatz-\\abgabe};
\node [finanziell, at=(IV|-Gewissen)] (Ersatzabgabe) {Ersatz\-abgabe};
% in the following, at=($(Gewissen.east)!0.5!(Zivilschutz.west)$) means
% place this node halfway (0.5) between the right (east) side of Gewissen
% and the left (west) side of Zivilschutz
\node [persohnlich, at=($(Gewissen.east)!0.5!(Zivilschutz.west)$)] (Zivildienst) {Zivildienst};

% last node in flow chart
\node [persohnlich, below=of Gewissen] (Armee) {Armee};

% draw thick arrow from the dienstpflichtig node to the first node in the flow chart
% add a coordinate named "mid" halfway along the arrow, that is used to draw the dashed lines
\draw [-Triangle, line width=2mm] (values-1) -- coordinate (mid) (tauglich);

% here (values-2) |- (mid) means draw a vertical, then horizontal,
% line from values-2 to mid
\draw [dashed, ultra thick] (values-2) |- (mid);

% here the -|/|- syntax is used both for coordinate specifications and path specifications
\draw [dashed, thick] (values-3) -- (values-3 |- mid) -| (schutzdienst);
\draw [dashed, thick] (values-4) |- (values-3 |- mid);

% draw the coloured background for the top right part of the flow chart, using a fit node
\begin{scope}[on background layer]
  \node [fill=black!10, fit=(Zivilschutz)(EG), inner sep=3mm] {};
\end{scope}


% draw the connections in the flow chart
% the ".." syntax comes from the quotes library
\path[near start,thick] 
     (tauglich) edge["JA"] (Gewissen)
     (Gewissen) edge["JA"] (Armee)
     (Gewissen) edge["NEIN"] (Zivildienst)
     (tauglich) edge["NEIN"] (schutzdienst)
     (schutzdienst) edge["NEIN"] (IV)
     (IV) edge["JA"] (EG)
     (schutzdienst) edge["JA"] (Zivilschutz)
     (IV) edge["NEIN"] (Ersatzabgabe);


% use fit again, to draw the surrounding frame
\node [draw,fit=(Armee)(EG), inner sep=5mm] (frame) {};

% use a matrix to make the legend
\matrix [
  above left,
  matrix of nodes,
  nodes={anchor=west}
  ] (m) at (frame.south east) {
&&[1cm] |[persohnlich,text width=0cm]| & persöhnliche Dienstleistung \\
|[draw,minimum size=8pt]| & Militärdienstpflicht & |[finanziell,text width=0cm]| &finanzielle Ersatzgabe \\
|[fill=black!10,minimum size=8pt]| & Schutzdienstpflicht & |[keine,text width=0cm]| & keine Leistung \\
};
\end{tikzpicture} 
\end{document}
2
  • Thank you very much for your help! The comments are extremely helpful to understand better how the whole thing works. Thank you!
    – danik
    Sep 2, 2018 at 20:07
  • +1: The commenting is great! Sep 27, 2021 at 2:29
6

Just a proposal:

\documentclass[border=5pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usetikzlibrary{matrix,arrows,shapes,positioning,fit,backgrounds}
\tikzset{
    io/.style={
        append after command={
            \pgfextra{
                \fill (\tikzlastnode.south) -- (\tikzlastnode.east) -- (\tikzlastnode.north) -- (\tikzlastnode.west) -- cycle;
            }
        }
    }
}
\begin{document}
    \begin{tikzpicture}[>=triangle 60]
        \matrix (m) [text width=4cm,
                     minimum width=4cm,
                     matrix of nodes,
                     row 1/.style={nodes={fill=gray!40,
                                          font=\bf,
                                          align=center,
                                          minimum width=5cm,
                                          minimum height=2.3cm,
                                          anchor=north,
                                          draw
                     }},
                     row 2/.style={nodes={
                            align=center,
                            minimum width=5cm,
                            minimum height=\pgflinewidth,
                            anchor=north,
                            draw
                     }}]{
            Schweizer Männer &  Schweizer Frauen &  Ausländer (niedergelassene Männer mit ausländischer Staatsangehörigkeit) & Ausländerinnen (niedergelassene Frauen mit ausländischer Staatsangehörigkeit) \\
            dienstpflichtig & freiwillig & freiwillig, nur Zivilschutz & freiwillig, nur Zivilschutz\\
        };
        \draw[->,line width=5pt] (m-2-1) --+ (0,-4.3);
            \draw[line width=5pt,densely dashed] (m-2-2) --+ (0,-2.5) --+ (-5,-2.5);

        \node[align=center,
              white,
              text width=2cm,
              minimum width=3cm,
              minimum height=3cm,
              io,
              below=4 of m-2-1] (miliz) {militärdienst-tauglich?};
        \node[align=center,
              white,
              text width=2cm,
              minimum width=3cm,
              minimum height=3cm,
              io,
              xshift=7.5cm,
              below=4 of m-2-1] (schutz) {schutzdienst-tauglich?};
        \node[align=center,
              white,
              text width=2cm,
              minimum width=3cm,
              minimum height=3cm,
              io,
              xshift=12cm,
              below=4 of m-2-1] (iv) {IV bzw. Integritäts-schädigung $>40\,\%$?};
        \node[align=center,
              text width=2cm,
              minimum width=3cm,
              minimum height=3cm,
              xshift=16cm,
              draw,
              circle,
              inner sep=1pt,
              below=4 of m-2-1] (ersatz) {keine Ersatzabgabe};
        \node[align=center,
              white,
              text width=2cm,
              minimum width=3cm,
              minimum height=3cm,
              io,
              yshift=-4.5cm,
              below=4 of m-2-1] (gewissen) {mit gewissen vereinbar?};
        \node[align=center,
              text width=2cm,
              minimum width=3cm,
              minimum height=3cm,
              draw,
              circle,
              fill=gray,
              yshift=-9cm,
              below=4 of m-2-1] (armee) {Armee};
        \node[align=center,
             text width=2cm,
             minimum width=3cm,
             minimum height=3cm,
             draw,
             circle,
             fill=gray,
             yshift=-4.5cm,
             xshift=4cm,
             below=4 of m-2-1] (zivil) {Zivildienst};
        \node[align=center,
             text width=2cm,
             minimum width=3cm,
             minimum height=3cm,
             draw,
             circle,
             fill=gray,
             yshift=-4.5cm,
             xshift=7.5cm,
             below=4 of m-2-1] (zivier) {Zivilschutz und Ersatzabgabe};
        \node[align=center,
             text width=2cm,
             minimum width=3cm,
             minimum height=3cm,
             draw,
             circle,
             fill=gray,
             yshift=-4.5cm,
             xshift=12cm,
             below=4 of m-2-1] (erab) {Ersatzabgabe};
        \draw (armee) -- (gewissen) -- (miliz) -- (schutz) -- (iv) -- (ersatz);
            \draw (gewissen) -- (zivil);
                \draw (schutz) -- (zivier);
                \draw (iv) -- (erab);
            \path (armee) -- (gewissen) node[midway,right] {JA};
            \path (gewissen) -- (miliz) node[midway,right] (ja1) {JA};
                \path (miliz) -- (schutz) node[very near start,above] {NEIN};
                \path (schutz) -- (iv) node[midway,above] {NEIN};
                    \path (iv) -- (ersatz) node[midway,above] {JA};
            \path (gewissen) -- (zivil) node[pos=.45,above] {NEIN};
                \path (schutz) -- (zivier) node[midway,right] (ja2) {JA};
                    \draw (ja1) -- (ja2) node[pos=.45,rounded corners,fill=white,draw,minimum width=1.5cm,minimum height=1cm] {differenzierte Zuteilung};
                \path (iv) -- (erab) node[midway,right] {NEIN};
            \coordinate (a) at ([xshift=4.4cm,yshift=-.4cm]erab.south east);
            \coordinate (b) at ([xshift=16.4cm,yshift=-.4cm]armee.south east);
        \begin{pgfonlayer}{background}
            \node[fill=gray!20,fit=(schutz)(a)] {};
            \node[scale=1.1,draw,fit=(miliz)(b)] {};
        \end{pgfonlayer}
        \node[anchor=west,align=center,xshift=2cm,rounded corners,fill=white,draw,minimum width=2.5cm,minimum height=1cm,text width=4cm] (hier) at (armee) {Teilweise Befreiung für Leistungen für Milizorganisationen, die in Katastrophenschutzdispotiven integriert sind};
            \draw (armee) -- (hier) -- (zivil);
            \draw (hier) -- (zivier);
        \node[anchor=west,align=center,yshift=2.5cm,xshift=.9cm,rounded corners,fill=white,draw,minimum width=2cm,minimum height=1cm,text width=2.5cm] (das) at (zivier) {Durchgehend reduzierte Ersatzabgabe, wenn Dienst geleistet wird};
            \draw (das) -- (zivier);
        \node[above left=-1] at (b) {
            \begin{tabular}{ll}
                \empty & \tikz[baseline=.15cm]{\node[fill=gray,circle,inner sep=.3cm]{}} persönliche Dienstleistung \\[2ex]
                \tikz[baseline=.15cm]{\node[inner sep=.3cm,draw]{}} Militärdienstpflicht & \tikz[baseline=.15cm]{\node[fill=gray!20,circle,inner sep=.3cm]{}} Finanzielle Ersatzabgabe \\[2ex]
                \tikz[baseline=.15cm]{\node[fill=gray!20,inner sep=.3cm]{}} Schutzdienstpflicht & \tikz[baseline=.15cm]{\node[draw,circle,inner sep=.3cm]{}} keine Leistung
            \end{tabular}
        };
    \end{tikzpicture}
\end{document}

Here is the output:

Screenshot

0

You must log in to answer this question.

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