Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I define two commands, block and connector in TikZ so that I could use them the same way as umlpackage and umlimport in uml-tikz.

E.g. I want to be able to say something like:

\block[x=0,y=0]{UI}
\block[x=2]{Services}
\block[x=4]{Business Layer}
\block{x=6]{Database Layer}

\connector{UI}{Services}
\connector{Services}{Business Layer}
\connector{Business Layer}{Database Layer}

and have a diagram that looks like:

enter image description here

If there is an easy way to do this in TikZ (without doing all the node and draw commands - I want to do this declaratively) or if there is a library for making component diagrams like these, please let me know.

P.S.: Ultimately, I would like to be a able to draw model diagrams like this:

enter image description here

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\newcommand{\block}[2][0,0]{%
\node[draw,minimum height=1cm,minimum width=3cm] (#2) at (#1) {#2};
}

\newcommand{\connector}[2]{%
\draw (#1) -- (#2);
}

\begin{document}

\begin{tikzpicture}
    \block{First Block}
    \block[4,-1]{Second Block}
    \block[3,2]{Third Block}
    \block[1,4]{Fourth Block}
    \connector{First Block}{Second Block}
    \connector{Third Block}{Second Block}
    \connector{First Block}{Fourth Block}
\end{tikzpicture}

\end{document}

enter image description here

P.S. For fitting nodes in Boxes, have a look at the fitting library (chapter 34 in the manual)


Edit 1: A little more advanced, now with options for the connectors, folder like nodes and using the fitting library for the big folder nodes:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows,fit}
\usepackage{xifthen}
\usepackage{xparse}

\newcommand{\block}[2][0,0]{%
\node[draw,minimum height=1cm,minimum width=3cm] (#2) at (#1) {#2};
}

%\newcommandx{\connector}[4][1=s,2=black]{% (s,hv,vh,bl,br), arrows and line options, start, end
\DeclareDocumentCommand\connector{ O{s} m O{->} m }{
\ifthenelse{\equal{#1}{s}}{\draw[#3] (#2) -- (#4);}{}
\ifthenelse{\equal{#1}{hv}}{\draw[#3] (#2) -| (#4);}{}
\ifthenelse{\equal{#1}{vh}}{\draw[#3] (#2) |- (#4);}{}
\ifthenelse{\equal{#1}{bl}}{\draw[#3] (#2) to[bend left=45] (#4);}{}
\ifthenelse{\equal{#1}{br}}{\draw[#3] (#2) to[bend right=45] (#4);}{}
}

\newcommand{\smallblock}[2][0,0]{
\node[draw,minimum height=1.5cm,minimum width=3.5cm] (#2) at (#1) {#2};
\node[above right,minimum height=0.5cm,minimum width=1cm] (#2_1) at (#2.north west) {};
\draw (#2_1.south west) -- (#2_1.north west) -- (#2_1.north east) -- (#2_1.south east);
}

\newcommand{\bigblock}[2]{
\node[draw,minimum height=2.5cm,minimum width=4.5cm,fit=#1,inner sep=1cm] (#2) {};
\node[above right,minimum height=0.5cm,minimum width=1cm] (#2_1) at (#2.north west) {#2};
\draw (#2_1.south west) -- (#2_1.north west) -- (#2_1.north east) -- (#2_1.south east);
}

\begin{document}

\begin{tikzpicture}
    \smallblock{First Block}
    \smallblock[5,0]{Second Block}
    \smallblock[5,-3]{Third Block}
    \smallblock[0,9]{Fourth Block}
    \smallblock[5,9]{Fifth Block}
    \smallblock[5,6]{Sixth Block}
    \smallblock[5,12]{Seventh Block}
    \connector{First Block}[->]{Second Block}
    \connector[hv]{Third Block}[latex-]{First Block}
    \connector{Fourth Block}{Fifth Block}
    \connector[br]{Fourth Block}[<->,dotted,thick]{Sixth Block}
    \connector[bl]{Fourth Block}[latex'-stealth,red,ultra thick]{Seventh Block}
    \bigblock{(First Block)(Second Block)(Third Block)}{First Group}
    \bigblock{(Fourth Block)(Fifth Block)(Sixth Block)(Seventh Block)}{Second Group}
    \connector{First Group}[latex-latex,blue,thick]{Second Group}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.