6

Cascading Blocks

How can i create this kind of connected and cascading block diagram in Latex?

5 Answers5

15

enter image description here

\documentclass{article}



\begin{document}
\lineskip0pt

\framebox[3cm]{\strut Text1}

\makebox[3cm]{\strut\vrule}

\framebox[3cm]{\strut Text2}

\makebox[3cm]{\strut\vrule}

\framebox[3cm]{\strut Text3}




\end{document}
David Carlisle
  • 757,742
  • I thought about the picture environment, but the simplicity of this is astonishing;). – mbork May 07 '13 at 20:42
  • 2
    tsk-tsk-tsk... after your first TikZ answer, I thought you had renounced to the bad habit of making thing simple ;-) – Gonzalo Medina May 07 '13 at 20:45
  • 1
    @david How to reduce the space between the two blocks? – Pratap Dheeraj May 07 '13 at 20:45
  • Haha, now things get less simple;). I'd try deleting \strut and play with \vrule height 6pt etc. - though this is not the LaTeX way;). – mbork May 07 '13 at 20:47
  • 1
    @PratapDheeraj as mbork said (or use \rule{1pt}{6pt} as a latex alternative) If you make it less that baselineskip you may need to set \baselineskip to 0pt as well as \lineskip. (Both settings should be inside a group if it is not the whole document) – David Carlisle May 07 '13 at 20:53
  • Also, I'd change \lineskip=0pt into \offinterlineskip and enclose everything in a group. Though correcting David is definitely outside my competence zone... – mbork May 07 '13 at 20:53
  • 1
    @mbork yes I omitted the group to keep the answer free of clutter at the expense of usability in a real document:-) – David Carlisle May 07 '13 at 20:54
7

Here you can specify the width of the boxes (it will be adjusted to fit if too short) and the separation between the boxes.

\documentclass{article}
\usepackage{keyval}

\makeatletter
\define@key{cascading}{width}{\cascading@wd=#1}
\define@key{cascading}{sep}{\def\cascading@sep{#1}}
\newdimen\cascading@wd

\newcommand{\cascadingblocks}[2][]{%
  \setkeys{cascading}{sep=2ex,#1}%
  \leavevmode\vbox{\offinterlineskip
    \@for\next:=#2\do{%
      \settowidth{\dimen@}{\next}%
      \ifdim\dimen@>\cascading@wd
        \cascading@wd=\dimen@
      \fi
    }%
    \@for\next:=#2\do{%
      \cascading@rule
      \hbox{\fbox{\hb@xt@\cascading@wd{\hss\next\hss}}}%
    }
  }
}
\def\cascading@rule{%
  \def\cascading@rule{%
    \hb@xt@\dimexpr\cascading@wd+2\fboxsep+2\fboxrule\relax
      {\hss\vrule\@height\cascading@sep\hss}%
  }%
}
\makeatother

\begin{document}
% natural width, default sep
\cascadingblocks{foo,bar,foobar,foobazzzz}
% 3cm width, 3pt sep
\cascadingblocks[width=3cm,sep=3pt]{foo,bar,foobar,foobazzzz}
% 1cm width (automatically increased), default sep
\cascadingblocks[width=1cm]{foo,bar,foobar,foobazzzz}

\end{document}

enter image description here

egreg
  • 1,121,712
6

Another option, using TikZ and chains:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains}

\begin{document}

\begin{tikzpicture}[
start chain=going below,
node distance=3mm,
every node/.style={on chain,join},
every join/.style={-},
block/.style={draw, text width=3cm,align=center}
]
\foreach \i in {1,...,5}
  \node[block] {Text \i};
\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
4

The Code:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{arrows}  
\begin{document}
\begin{tikzpicture}
[auto, 
block/.style = {rectangle, draw, text width = 6em, align =center},
line/.style = {draw, thick, -}]

\matrix[every node/.style = block, row sep=6mm]{
\node (t1) {Text 1}; \\
\node (t2) {Text 2}; \\
\node (t3) {Text 3}; \\
\node (t4) {Text 4}; \\
};
\path[line] (t1) -- (t2) -- (t3) -- (t4); 
\end{tikzpicture} 
\end{document}

The Output:

somefun.png

kan
  • 4,545
1

With PSTricks.

enter image description here

\documentclass[preview]{standalone}
\usepackage{pst-node,multido}
\def\mybox#1{\psframebox{\makebox[3cm]{#1}}}

\begin{document}
\offinterlineskip
\psmatrix[rowsep=.5]
\mybox{A}\\
\mybox{B}\\
\mybox{C}
\endpsmatrix
\multido{\ia=1+1,\ib=2+1}{3}{\ncline{\ia,1}{\ib,1}}
\end{document}
Moriambar
  • 11,466