Search code examples
latexdiagramtikzxelatexoverleaf

Weird diagrams using chains within tikz


I am trying to replicate some diagrams that I made using Microsoft Visio, using the tikz package in LaTeX. The basic diagrams are relatively easy to build, but when it comes to add more than two nodes next to each other, it gets quite tricky... I can't seem to find any documentation that justifies my needs to center and align the nodes like I've done in Visio.

These are the MS Visio diagrams: enter image description here enter image description here

But this is what I have got right now, using Tikz: enter image description here enter image description here

Here is a minimal reproducible example:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usepackage{svg}
\usepackage{amsmath}

\usetikzlibrary{shapes.geometric, arrows, chains}

%%
%%TikZ Settings
\tikzstyle{main} = [rectangle, minimum width=3cm, minimum height=0.6cm,text centered, text width=3cm, draw=black, fill=black!30]
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}

%The "Remediação" diagram

\begin{tikzpicture}[node distance=1.5cm]

    \node (principal) [main] {Remediação};
    \coordinate[below of=principal] (a);
    \begin{scope}[start chain=going right]
    \node (rem-vuln-c-patch) [main, on chain, below of=principal, left of=a] {Remediação de Vulnerabilidade Normal com \textit{patch}};
    \node (rem-vuln-c-c-patch) [main, on chain] {Remediação de Vulnerabilidade Crítica com \textit{patch}};
    \node (rem-vuln-config) [main, on chain] {Remediação de Vulnerabilidade por má configuração};
    \node (gest-vuln-s-patch) [main, on chain] {Gestão de Vulnerabilidade sem \textit{patch}};
    \end{scope}
    
    \node(gest-risco) [main, align=center, below of=rem-vuln-c-c-patch] {Gestão de Risco};
    
    \draw [arrow] (principal) -| (rem-vuln-c-patch);
    \draw [arrow] (principal) -| (rem-vuln-c-c-patch);
    \draw [arrow] (principal) -| (rem-vuln-config);
    \draw [arrow] (principal) -| (gest-vuln-s-patch);
    \draw [arrow] (gest-vuln-s-patch) |- (gest-risco);
    \draw [arrow] (gest-risco) -| (rem-vuln-c-patch);
    \draw [arrow] (gest-risco) -- (rem-vuln-c-c-patch);
    
\end{tikzpicture}

% The "Balanço" diagram

\begin{tikzpicture}[node distance=1.5cm]

    \node (principal) [main] {Balanço};
    \coordinate[below of=principal] (a);
    \begin{scope}[start chain=going right]
    \node (lic-apr) [main, on chain, below of=principal, left of=a] {Lições aprendidas};
    \node (reu-retro) [main, on chain] {Reunião de Retrospetiva};
    \node (cri-rel-exec) [main, on chain] {Criação do relatório executivo};
    \end{scope}
    
    \draw [arrow] (principal) -| (lic-apr);
    \draw [arrow] (principal) -| (reu-retro);
    \draw [arrow] (principal) -| (cri-rel-exec);
    
\end{tikzpicture}

\end{document}

Solution

  • Using a tree might be easier than a chain:

    \documentclass{article}
    \usepackage[utf8]{inputenc}
    
    \usepackage{tikz}
    \usepackage{svg}
    \usepackage{amsmath}
    
    \usetikzlibrary{shapes.geometric, arrows, chains,positioning}
    
    %%
    %%TikZ Settings
    \tikzstyle{main} = [rectangle, minimum width=3cm, minimum height=2cm,text centered, text width=3cm, draw=black, fill=black!30]
    \tikzstyle{arrow} = [thick,->,>=stealth]
    
    \begin{document}
    
    %The "Remediação" diagram
    
    \begin{tikzpicture}[
      level distance=3cm,
      sibling distance=4cm,
      edge from parent path={[thick,->](\tikzparentnode.south) -- ++(0,-1cm) -| (\tikzchildnode.north)}
    ]
    
      \node[main,minimum height=0.6cm] (principal) {Remediação}
        child { node[main] (rem-vuln-c-c-patch)  {Remediação de Vulnerabilidade Crítica com \textit{patch}} }
        child { node[main] (rem-vuln-c-patch)  {Remediação de Vulnerabilidade Normal com \textit{patch}} }
        child { node[main] (rem-vuln-config)  {Remediação de Vulnerabilidade por má configuração} }
        child { node[main] (gest-vuln-s-patch) {Gestão de Vulnerabilidade sem \textit{patch}} };
        
      \node(gest-risco) [main, align=center, below=4.5cm of principal,minimum height=0.6cm] {Gestão de Risco};
        
      \draw[->,thick] (gest-risco) -| (rem-vuln-c-c-patch);
      \draw[->,thick] (gest-risco) -| (gest-vuln-s-patch);
      \draw[->,thick] ([xshift=-1.0cm]gest-risco.north) -| ([xshift=1.8cm]rem-vuln-c-patch);
    
    \end{tikzpicture}
    
    
    
    \end{document}
    

    enter image description here