# Alignment of nodes and scopes in tikz matrix?

I am trying to fix the positions of the arrows of the following diagram without much success. I would like the arrows to be centered (both horizontally and vertically) in the matrix cell no matter which way they are pointing.

The arrows are defined by macros of LARROW RARROW and DARROW.

MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,shapes.gates.logic.IEC,shapes.arrows,positioning,decorations.markings}

\begin{document}

\begin{tikzpicture}[use IEC style logic gates, LFSR/.style={
rectangle split, rectangle split horizontal=true, rectangle split parts=#1, draw, anchor=center}, ARROW/.style={draw,thick,single arrow,single arrow head extend=3pt,transform shape}]
\def\Byte(#1,#2,#3,#4){%
\node[LFSR=3,name=#1] {\nodepart{one}#2\nodepart{two}#3\nodepart{three}#4};
}
\def\LFSR(#1,#2,#3,#4){%
\begin{scope}[decoration={
markings,
mark=at position 0.6 with {\arrow{latex}}}]
\Byte(LFSR0,#1,#2,#3)
\node[xor gate, draw, logic gate inputs=nn, logic gate input sep=0.3cm, rotate = 180, below = 1.4cm of LFSR0.one, xshift=2mm, xor gate symbol ={\scalebox{-1}{\small=1}}, logic gate symbol align = {bottom}] (Xor) {\parbox[t][0.75cm]{0.7cm}{\scalebox{-1}{\small XOR}}};
\draw (LFSR0.two south) |- (Xor.input 2);
\draw (LFSR0.three south) |- (Xor.input 1);
\draw (Xor.output) -- ++(left:3mm) node[anchor=north,inner sep=0pt] (hook1) {};
\draw[postaction={decorate}] (hook1) |- node [near start, left] {#4} (hook1|-LFSR0.west);
\draw (hook1|-LFSR0.west) -- (LFSR0.west) {};
\draw[-latex] (LFSR0.one north) ++(up:1mm) node[anchor=center,inner sep=0pt] (hook2) {} -- (LFSR0.three south |-hook2);
\end{scope}
}
\def\RARROW{\node[rectangle,draw] {
\parbox[b][1cm]{0pt}{\begin{tikzpicture}
\node[ARROW] {};
\end{tikzpicture}}
};
}
\def\LARROW{\node[rectangle,draw,rotate=180] {
\parbox[t][1cm]{0pt}{\begin{tikzpicture}
\node[ARROW] {};
\end{tikzpicture}}
};
}
\def\DARROW{\node[rectangle,draw,align=center] {
\begin{tikzpicture}
\node[ARROW,rotate=-90] {};
\end{tikzpicture}
};
}
\matrix[row sep=0, column sep=2.5mm]{
\LFSR(1,1,1,0) &\RARROW& \LFSR(0,1,1,0) & \RARROW & \LFSR(0,0,1,1) & \RARROW & \LFSR(1,0,0,0)\\
\node[anchor=east,minimum height=9mm]{$t=0$};   & &
\node[anchor=east,minimum height=9mm]{$t=1$};   & &
\node[anchor=east,minimum height=9mm]{$t=2$};   & &
\node[anchor=east,minimum height=9mm]{$t=3$};   \\
& & & & & & \DARROW\\
\LFSR(1,1,1,0) &\LARROW& \LFSR(1,1,0,1) &\LARROW& \LFSR(1,0,1,1) &\LARROW& \LFSR(0,1,0,1)\\
\node[anchor=east,minimum height=9mm]{$t=7$};   & &
\node[anchor=east,minimum height=9mm]{$t=6$};   & &
\node[anchor=east,minimum height=9mm]{$t=5$};   & &
\node[anchor=east,minimum height=9mm]{$t=4$};   \\
};
\end{tikzpicture}

\end{document}

-
Your code does not compile here. Anyway, could you provide a minimal example? – tecepe Oct 5 '12 at 7:08
@tecepe Sorry I missed a unit in the parbox command. It should now compile. I am thinking whether I should use minipages to fix height of all rows in each cell or to ditch matrix altogether and go for may be chains – Mobius Pizza Oct 5 '12 at 8:17
Are there any disadvantages of nesting tikz scopes inside the text area of nodes, such that I transform the matrix into a matrix of nodes? That'd solve the problem I believe – Mobius Pizza Oct 5 '12 at 8:37
Nesting resources has to be done very carefully because tikz inherits the attributes when one tikzpicture is inside another. It can bring many unexpected behavior. – tecepe Oct 5 '12 at 17:14

Below I put my solution to your problem (err... a little bit modified =)

I rewrote the code, hope it has not been too difficult to understand.

\documentclass [tikz] {standalone}

\usetikzlibrary{calc}
\usetikzlibrary{matrix}
\usetikzlibrary{fit}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{shapes.gates.logic.IEC}

\tikzset{
bit/.style = {draw, thick, minimum size = 1 cm, anchor = west}
, port/.style = {draw, thick, minimum size = 2 cm}
, space/.style = {minimum size = 1 cm}
, flow/.style = {->, thick}
, label/.style = {anchor = east}
, inside/.style = {minimum size = 1 cm}
, next/.style = {single arrow, draw, minimum size = 8mm}
}

\newcounter{cellcounter}

\newcommand{\myarrow}[1]{
\begin{tikzpicture}
\node [next, rotate = #1]{};
\end{tikzpicture}
}

\pgfmathsetmacro{\inputonedistance}{1 / 3}
\pgfmathsetmacro{\inputtwodistance}{2 / 3}

\newcommand{\mycell}[5]{
\begin{tikzpicture} [use IEC style logic gates]
\node [port]  (port) {};
\node [inside, anchor = north]  (port symbol) at (port.north){\texttt{=1}};
\node [inside, anchor = south]  (port name) at (port.south){\LARGE \texttt{#2}};

\coordinate (port right 1) at ($(port.north east)!\inputonedistance!(port.south east)$);
\coordinate (port right 2) at ($(port.north east)!\inputtwodistance!(port.south east)$);

\node [space, anchor = south east] (space 0) at (port.north east) {};

\node [bit, anchor = south] (bit 0) at (space 0.north east) {\texttt{#3}};
\node [bit] (bit 1) at (bit 0.east) {\texttt{#4}};
\node [bit] (bit 2) at (bit 1.east) {\texttt{#5}};

\node [space, anchor = south] (space 1) at (bit 0.north) {};
\node [space, anchor = south] (space 2) at (bit 2.north) {};

\draw [flow] (bit 1) |- (port right 1);
\draw [flow] (bit 2) |- (port right 2);
\draw [flow] (space 1.center) |- (space 2.center);

\node [space, anchor = south west] (space 3) at (port.north west) {};
\node [space, anchor = south east] (space 4) at (space 3.north west) {};
\node [space, anchor = east] (space 5) at (port.west) {};

\node [label] (label 1) at ($(space 4)!0.5!(space 5)$) {\texttt{#1}};

\draw [flow] (port) -| (label 1.east) |- (bit 0);
\draw [flow] (port) -| (label 1.east) |- (bit 0);

\node [fit = (port) (bit 2) (label 1) (space 2)] (base) {};
\node [anchor = north] at (base.south) {$$t = \thecellcounter$$ \stepcounter {cellcounter}};
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells, nodes = {minimum size = 2cm}]{
& & & & & & & & & & & & & & \\
& & & & & & & & & & & & & & \\
& & & & & & & & & & & & & & \\
& & & & & & & & & & & & & & \\
& & & & & & & & & & & & & & \\
& & & & & & & & & & & & & & \\
& & & & & & & & & & & & & & \\
};

\node at (m-4-2) {\mycell{0}{XOR}{1}{1}{1}};

\node at (m-2-6) {\mycell{0}{XOR}{0}{1}{1}};
\node at (m-2-10) {\mycell{1}{XOR}{0}{0}{1}};
\node at (m-2-14) {\mycell{0}{XOR}{1}{0}{0}};

\node at (m-6-14) {\mycell{1}{XOR}{0}{1}{0}};
\node at (m-6-10) {\mycell{1}{XOR}{1}{0}{1}};
\node at (m-6-6) {\mycell{1}{XOR}{1}{1}{0}};

\node at (m-3-4)  {\myarrow{45} };
\node at (m-2-8)  {\myarrow{0}  };
\node at (m-2-12) {\myarrow{0}  };
\node at (m-4-14) {\myarrow{270}};
\node at (m-6-8)  {\myarrow{180}};
\node at (m-6-12) {\myarrow{180}};
\node at (m-5-4)  {\myarrow{135}};

\end{tikzpicture}
\end{document}

-
That's great thanks. I was able to use scopes on each matrix cell and achieve similar results, your arrangement is more appealing. I did not know about the "fit" library, that's very useful. Tikz/pgf manual is such a mammoth document I am finding it really hard to learn from it :P – Mobius Pizza Oct 6 '12 at 13:18