4

I am having an issue with TIKZ, or matrices ... I don't know which :)

All I want to do is to move/align my Blackbox in the center of this one (multi-)row while having the arrows spread evenly on the sides (see below). Somehow it does not work. I tried with tabular and array, but still no luck.

Any ideas? Thanks!!

(I'm no hero with latex, please be kind!)

\documentclass[10pt]{article} %
\usepackage{tikz} %
\usepackage{multirow} %
\usetikzlibrary{automata,arrows} %
\usepackage[active,tightpage]{preview}%
\PreviewEnvironment{tikzpicture}%
\setlength\PreviewBorder{1em}%
\begin{document}

    \begin{tikzpicture}
    [auto,
    block/.style ={rectangle, draw=black, thick, fill=black!85, text width=8em, text centered, text=white, rounded corners, minimum height=10em},
    line/.style ={draw, thick, -latex}]

    \matrix [column sep=15mm,row sep=6mm]
    {
    \node[] (in1) {Eingang 1} ; & \multirow{6}{*}{\node[block] (bb) {Blackbox};} & \node[] (out1) {Ausgang 1} ; \\
    \node[] (in2) {Eingang 2} ; & & \node[] (out2) {Ausgang 2} ; \\
    \node[] (in3) {Eingang 3} ; & & \node[] (out3) {Ausgang 3} ; \\
    \node[] (in4) {Eingang 4} ; & & \node[] (out4) {Ausgang 4} ; \\
    \node[] (in5) {Eingang 5} ; & & \node[] (out5) {Ausgang 5} ; \\
    \node[] (in6) {Eingang 6} ; & & \node[] (out6) {Ausgang 6} ; \\
    };

    \begin{scope}[every path/.style=line]
    \path (in1.east) -- ([yshift=3em]bb.west);
    \path (in2.east) -- ([yshift=2em]bb.west);
    \path (in3.east) -- ([yshift=1em]bb.west);
    \path (in4.east) -- ([yshift=-1em]bb.west);
    \path (in5.east) -- ([yshift=-2em]bb.west);
    \path (in6.east) -- ([yshift=-3em]bb.west);

    \path ([yshift=3em]bb.east) -- (out1.west);
    \path ([yshift=2em]bb.east) -- (out2.west);
    \path ([yshift=1em]bb.east) -- (out3.west);
    \path ([yshift=-1em]bb.east) -- (out4.west);
    \path ([yshift=-2em]bb.east) -- (out5.west);
    \path ([yshift=-3em]bb.east) -- (out6.west);
    \end{scope}

    \end{tikzpicture}

\end{document}

align blackbox

1

3 Answers 3

5

Is this what you want?

The following method first places the two sets of nodes on both sides, which creates a bounding box. Then, it puts the black box at the center of the bounding box. You need to load the positioning library. And seems that the automata library is not needed.

Code

\documentclass[10pt]{article} %
\usepackage{tikz} %
\usepackage{multirow} %
\usetikzlibrary{arrows,positioning} %
\usepackage[active,tightpage]{preview}%
\PreviewEnvironment{tikzpicture}%
\setlength\PreviewBorder{1em}%
\begin{document}

    \begin{tikzpicture}
    [auto,
    block/.style ={rectangle, draw=black, thick, fill=black!85, text width=8em, text centered, text=white, rounded corners, minimum height=10em},
    line/.style ={draw, thick, -latex}]

    \node(in1){Eingang 1};
    \foreach\innum[count=\inlag] in {2,...,6}
        \node(in\innum)[below=of in\inlag]{Eingang \innum};

    \node(out1)[right=50mm of in1]{Ausgang 1};
    \foreach\outnum[count=\outlag] in {2,...,6}
        \node(out\outnum)[below=of out\outlag]{Ausgang \outnum};

    \node[block] (bb) at(current bounding box.center) {Blackbox};

    \begin{scope}[every path/.style=line]
    \path (in1.east) -- ([yshift=3em]bb.west);
    \path (in2.east) -- ([yshift=2em]bb.west);
    \path (in3.east) -- ([yshift=1em]bb.west);
    \path (in4.east) -- ([yshift=-1em]bb.west);
    \path (in5.east) -- ([yshift=-2em]bb.west);
    \path (in6.east) -- ([yshift=-3em]bb.west);

    \path ([yshift=3em]bb.east) -- (out1.west);
    \path ([yshift=2em]bb.east) -- (out2.west);
    \path ([yshift=1em]bb.east) -- (out3.west);
    \path ([yshift=-1em]bb.east) -- (out4.west);
    \path ([yshift=-2em]bb.east) -- (out5.west);
    \path ([yshift=-3em]bb.east) -- (out6.west);
    \end{scope}

    \end{tikzpicture}

\end{document}

Output

enter image description here

3
  • This looks really good! Only thing is, I get an Emergency Stop on \usetikzlibrary{ arrows, positioning} % Can't find file `tikzlibraryauto.code.tex'. What do I need to use this library?
    – gilu
    Nov 15, 2013 at 5:22
  • @gilu: Sorry. I think I mistakenly left the auto in the \usetikzlibrary command (it was meant to be automata but I guess I didn't delete it fully). Error corrected now.
    – Herr K.
    Nov 15, 2013 at 5:30
  • No problem! This works really well, too! It's most elegant, since I could also loop the arrows and make it more adaptive...
    – gilu
    Nov 15, 2013 at 5:37
5

What you have is a matrix of nodes in which \multirow won't work. You have to a new row at the center for the black box. To change the excess vertical space, you can use \\[<dimen>] as in

\node[] (in3) {Eingang 3} ; & & \node[] (out3) {Ausgang 3} ; \\[-2.3cm]
                  &    \node[block] (bb) {Blackbox};   &          \\[-2.3cm]

Full code

\documentclass[10pt]{article} %
\usepackage{tikz} %
\usepackage{multirow} %
\usetikzlibrary{automata,arrows} %
\usepackage[active,tightpage]{preview}%
\PreviewEnvironment{tikzpicture}%
\setlength\PreviewBorder{1em}%
\begin{document}

    \begin{tikzpicture}
    [auto,
    block/.style ={rectangle, draw=black, thick, fill=black!85, text width=8em, text centered, text=white, rounded corners, minimum height=10em},
    line/.style ={draw, thick, -latex}]

    \matrix [column sep=15mm,row sep=6mm]
    {
    \node[] (in1) {Eingang 1} ; &  & \node[] (out1) {Ausgang 1} ; \\
    \node[] (in2) {Eingang 2} ; & & \node[] (out2) {Ausgang 2} ; \\
    \node[] (in3) {Eingang 3} ; & & \node[] (out3) {Ausgang 3} ; \\[-2.3cm]
                  &    \node[block] (bb) {Blackbox};   &          \\[-2.3cm]
    \node[] (in4) {Eingang 4} ; & & \node[] (out4) {Ausgang 4} ; \\
    \node[] (in5) {Eingang 5} ; & & \node[] (out5) {Ausgang 5} ; \\
    \node[] (in6) {Eingang 6} ; & & \node[] (out6) {Ausgang 6} ; \\
    };

    \begin{scope}[every path/.style=line]
    \path (in1.east) -- ([yshift=3em]bb.west);
    \path (in2.east) -- ([yshift=2em]bb.west);
    \path (in3.east) -- ([yshift=1em]bb.west);
    \path (in4.east) -- ([yshift=-1em]bb.west);
    \path (in5.east) -- ([yshift=-2em]bb.west);
    \path (in6.east) -- ([yshift=-3em]bb.west);

    \path ([yshift=3em]bb.east) -- (out1.west);
    \path ([yshift=2em]bb.east) -- (out2.west);
    \path ([yshift=1em]bb.east) -- (out3.west);
    \path ([yshift=-1em]bb.east) -- (out4.west);
    \path ([yshift=-2em]bb.east) -- (out5.west);
    \path ([yshift=-3em]bb.east) -- (out6.west);
    \end{scope}

    \end{tikzpicture}

\end{document}

enter image description here

1
  • That will work!! So ... you changed the margin to a negativ number on two rows?! This I don't understand how that works .. :\
    – gilu
    Nov 15, 2013 at 5:29
1

This is what I do now, thanks to all of you who answered!

\documentclass[10pt]{article} %
\usepackage{tikz} %
\usepackage{multirow} %
\usetikzlibrary{arrows,positioning} %
\usepackage[active,tightpage]{preview}%
\PreviewEnvironment{tikzpicture}%
\setlength\PreviewBorder{1em}%
\begin{document}

    \begin{tikzpicture}
    [auto,
    block/.style ={rectangle, draw=black, thick, fill=black!85, text width=8em, text centered, text=white, rounded corners, minimum height=10em},
    line/.style ={draw, thick, -latex},
    nodel/.style={text width=9em, align=right, anchor=east}, % change width for long text
    noder/.style={text width=9em, align=left, anchor=west}    
    ]

    \node(in1){}; %Eingang 1
    \foreach\innum[count=\inlag] in {2,...,6}
        \node(in\innum)[below=of in\inlag]{}; %Eingang \innum

    \node(out1)[right=60mm of in1]{}; %Ausgang 1
    \foreach\outnum[count=\outlag] in {2,...,6}
        \node(out\outnum)[below=of out\outlag]{}; %Ausgang \outnum

    \node[block] (bb) at(current bounding box.center) {Blackbox};

    \begin{scope}[every node/.style=nodel]
        \node[] at (in1) {This is really long text for only one node};
        \node[] at (in2) {Very short!};
        \node[] at (in3) {Here's some more text that needs to fit my node};
        \node[] at (in4) {Here's some more text};
        \node[] at (in5) {Nothing new here};
        \node[] at (in6) {Nothing new here};
    \end{scope}

    \begin{scope}[every node/.style=noder]
        \node[] at (out1) {Not as much};
        \node[] at (out2) {Long text, see above};
        \node[] at (out3) {Short! Or maybe not as short as I thought. Maybe too much};
        \node[] at (out4) {Short};
        \node[] at (out5) {Something else over here};
        \node[] at (out6) {Something else over here};
    \end{scope}

    \begin{scope}[every path/.style=line]
        \path (in1.east) -- ([yshift=3em]bb.west);
        \path (in2.east) -- ([yshift=2em]bb.west);
        \path (in3.east) -- ([yshift=1em]bb.west);
        \path (in4.east) -- ([yshift=-1em]bb.west);
        \path (in5.east) -- ([yshift=-2em]bb.west);
        \path (in6.east) -- ([yshift=-3em]bb.west);

        \path ([yshift=3em]bb.east) -- (out1.west);
        \path ([yshift=2em]bb.east) -- (out2.west);
        \path ([yshift=1em]bb.east) -- (out3.west);
        \path ([yshift=-1em]bb.east) -- (out4.west);
        \path ([yshift=-2em]bb.east) -- (out5.west);
        \path ([yshift=-3em]bb.east) -- (out6.west);
    \end{scope}

    \end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .