TransWikia.com

Drawing a diagram with tikz

TeX - LaTeX Asked by Qni on May 11, 2021

I am trying to draw a diagram like this:

enter image description here

So far what I got is this:

enter image description here

No idea where that little box in the top left corner is coming from…

This is my code:

documentclass[a4paper,10pt]{article}
usepackage{tikz}
usetikzlibrary{matrix,shapes,arrows,positioning,chains}

begin{document}
    tikzset{
    block/.style={
        rectangle,
        draw,
        text width=10em,
        text centered,
        rounded corners
    },
}

begin{tikzpicture}
matrix (m)[matrix of nodes, column  sep=1cm,row  sep=8mm, align=center, nodes={rectangle,draw, anchor=center} ]{
    
    & |[block]| {1}
    &  
     & & 
    |[block]| {1}
    & |[block]| {2}               
    & |[block]| {3}    
};
draw [>=latex,-] (m-1-2) |- (m-3-1);
end{tikzpicture}

end{document}

When I try to draw from the top box to the matrix element in the middle I get the error no shape named m-2-2 is known.

What should I do? Thanks

2 Answers

  • You have empty lines in matrix code, it is the cause of undesired square in your image (in matrix you had not have empty lines)
  • for naming of all cells you need to add option nodes in empty cells, to the matrix styles options
  • that in empty cells are not drawn small squares, you had to change nodes style to coordinate:
documentclass[a4paper,10pt]{article}
usepackage{tikz}
usetikzlibrary{arrows,
                chains, 
                matrix,
                positioning}

begin{document}
    tikzset{
    block/.style={
        rectangle,
        draw,
        text width=10em,
        text centered,
        rounded corners
    },
}

    begin{tikzpicture}
matrix (m) [matrix of nodes, 
             nodes in empty cells,
             column  sep=1cm,
             row  sep=8mm, 
             align=center, 
             nodes={coordinate}
             ]
{
                & |[block]| {1} &               
                &               &               
|[block]| {1}   & |[block]| {2} & |[block]| {3} 
};
draw       (m-1-2) -- (m-2-2) -| (m-3-1)
                              (m-2-2) -| (m-3-2)
                              (m-2-2) -| (m-3-3);
% or 
%   draw   (m-1-2) -- (m-3-2)
%           (m-3-1) |- (m-2-2) -| (m-3-3);
    end{tikzpicture}

end{document}

enter image description here

(In nodes in your images is higher, but in MWE you not define their heights. I follow node style definition in your MWE.)

However, your diagram is tree, which can be simpler drawn as tree, for example with forest package:

%documentclass[a4paper,10pt]{article}
documentclass[margin=3.141592]{standalone}
usepackage[edges]{forest}

begin{document}
    begin{forest}
    for tree={              
% style of nodes
      draw, semithick, rounded corners,
       text width = 4em, text badly centered,% changed 
   minimum height = 4ex,                     % added
% style of tree 
             edge = {draw=blue},             % changed
           anchor = north,
             grow = south,
    forked edge,            
            s sep =  4mm,   % horizontal ("sibling") distance between nodes
            l sep = 12mm,   % vertical ("level") distance between n odes
         fork sep =  6mm,   % distance from parent to branching point
               }
[1
    [1]
    [2]
    [3]
]
    end{forest}
end{document}

which drawn the following tree:

enter image description here

Correct answer by Zarko on May 11, 2021

enter image description here

    documentclass{article}
usepackage{tikz} 
usetikzlibrary{arrows.meta, positioning, shapes} 
begin{document}
begin{tikzpicture}[
    node distance = 1.5cm and 1cm,
    arr/.style = {draw=blue!50, ultra thick},
    base/.style = {draw, font=small,
        minimum height=3em, text width=5em, align=center},
    block/.style = {base, fill=#1},
    block/.default = white,
    every edge/.append style = {arr}
    ]
    node [block] (A) {};
    node [block, below =of A] (B) {};
    node [block, right = of B] (C) {};
    node [block, left = of B] (D) {};
    coordinate[below=1cm of A] (aux);
    path[arr] (A) to (B);
    path[arr](aux) -| (C);
    path[arr](aux) -| (D);
end{tikzpicture}
end{document}

Answered by js bibra on May 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP