3

I tried to draw a block diagram similar to the one shown in diagram but I am unable to do it Please anyone suggest me a way how draw such block diagram. Thank you

I tried so far,

\usepackage{forest}
\useforestlibrary{edges}

\begin{forest}
for tree={draw, minimum width=2cm, minimum height=1cm, rounded corners},
forked edges,

[IDS for Automotive CAN Bus System [Deployment Strategy[ECU][CAN][Gateway]][Detection Approach[Specification Based][Anomaly Based[Machine Learning][Hybrid Based][Frequency Based]][Signature Based]][Attacking Technique[DoS][Replay]]]
\end{forest}

I am getting this kind of output in pdf.wrong output]1

This output I Want

limlian
  • 151

1 Answers1

4

This is to a large extent adapted from here. You need to inject for tree={folder, grow'=0} whenever you want to start a folder. Your tree is a bit too wide, so I reduced the text width for all but the root nodes.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{forest}
\useforestlibrary{edges}
\makeatletter
% remove the stray space https://tex.stackexchange.com/a/513549
\patchcmd{\pgfutilsolvetwotwoleqfloat}
  { \noexpand\pgfmathfloatdivide@}
  {\noexpand\pgfmathfloatdivide@}
  {}{}
\makeatother
\begin{document}
\noindent\begin{forest}
before typesetting nodes={
   if={isodd(n_children("!r"))}{
     for nodewalk/.wrap pgfmath arg={{fake=r,n=#1}{calign with current edge}}{int((n_children("!r")+1)/2)},
   }{},
 },
forked edges,
for tree={draw, minimum width=2cm, minimum height=1cm, rounded corners,
if level=0{}{text width=2cm}},
[IDS for Automotive CAN Bus System 
 [Deployment Strategy,for tree={folder, grow'=0}
  [ECU]
  [CAN]
  [Gateway]
 ]
 [Detection Approach
  [Specification Based]
  [Anomaly Based,for tree={folder, grow'=0}
   [Machine Learning]
   [Hybrid Based]
   [Frequency Based]
  ]
  [Signature Based]
 ]
 [Attacking Technique,for tree={folder, grow'=0}
  [DoS]
  [Replay]
 ]
]
\end{forest}
\end{document}

enter image description here

showframe is only to show that this tree fits (yet just loading the geometry package makes the page a bit wider).

EDIT: Removed tempcounta/.max={level}{tree}, which was not used, big thanks to @cfr.

2nd EDIT: Added fix from https://tex.stackexchange.com/a/513549, following a suggestion by @cfr.

  • Thank you for your suggestion. Its really useful for creating a block diagram like this. – limlian Dec 01 '19 at 07:32
  • I have added arrow using this command edge path'={(!u.parent anchor) -- ++(0,-15pt) -| (.child anchor)}, and added edge=-Latex in for tree = {edge=-Latex,....} – limlian Dec 01 '19 at 07:32
  • Is there any command to change the size of the blocks? – limlian Dec 01 '19 at 07:33
  • 1
    @limlian The sizes of the blocks is influenced by minimum width=2cm, minimum height=1cm and if level=0{}{text width=2cm}}. If you increase these dimensions, the nodes will get larger, but at a given point the tree might become too large. –  Dec 01 '19 at 07:35
  • 1
    @cfr Very good point! I was not careful enough to only adapt what I really need. –  Dec 02 '19 at 18:20
  • Can't upvote again ;). – cfr Dec 03 '19 at 01:41
  • 1
    @cfr Thanks! Do you by chance now why adding \noindent before \begin{forest} moves the tree to the right? –  Dec 03 '19 at 05:32
  • Weird. You still have the tempcounta there, by the way. – cfr Dec 04 '19 at 02:45
  • 1
    @cfr Thanks! Will fix it when I am at my computer. And thanks for confirming that it is weird. ;-) –  Dec 04 '19 at 02:48
  • @Schrödinger'scat I think it is the TikZ/PGF bug which inserts stray spaces at the start of tikzpictures. Maybe they disappear at the start of the line (get swallowed by the paragraph indent) but not if the paragraph indent is nixed? Guessing ... (The space isn't in the tree itself, it seems ....) – cfr Dec 04 '19 at 02:51
  • 1
    @cfr You are right, of course. –  Dec 04 '19 at 05:35