# How to add line between two rows of nodes in Tikz and how to align nodes vertically?

I have two questions:

1. How can I add a horizontal line centered between two rows of nodes in Tikz?
2. How can I change the vertical placement of a node to line up better with other nodes

A picture of what I mean will make this clearer:

The blue line indicates the line I'd like to add (it should be straight of course). Note the two red circles. Because of the subscripts the arrows don't "hit" them in the center of the X like it does for X_1, X_2, X_3. So how can I move these down somewhat?

Here's a minimal working example.

\documentclass[a4paper,11pt]{article}

\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc}

\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of nodes, column  sep=1cm,row  sep=8mm, nodes={draw=none, anchor=center} ]{
\node [] {$X_1$}; & \node [] {$X_2$}; & \node [] {$X_3$}; & \node [] {$\ldots$}; & \node [] {$X_{\frac{n}{8}-1}$}; & \node [] {$X_{\frac{n}{8}}$}; \\
\node [] {$Y_1$}; & \node [] {$Y_2$}; & \node [] {$Y_3$}; & \node [] {$\ldots$}; & \node [] {$Y_{\frac{n}{8}-1}$}; & \node [] {$Y_{\frac{n}{8}}$}; \\
};

\path [>=latex,->] (m-1-1) edge (m-2-1);
\path [>=latex,->] (m-1-1) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-2) edge (m-2-2);
\path [>=latex,->] (m-1-2) edge (m-2-3);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-4) edge (m-2-5);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-6);
\path [>=latex,->] (m-1-6) edge (m-2-6);

\end{tikzpicture}

\end{document}

-

You can simplify the syntax by using matrix of math nodes option.

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc}
\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of math nodes, column  sep=1cm,row  sep=8mm, nodes={draw=none, anchor=center,text depth=0pt} ]{
X_1 & X_2 & X_3 &\ldots & X_{\frac{n}{8}-1} & X_{\frac{n}{8}} \\
Y_1 & Y_2 & Y_3 &\ldots & Y_{\frac{n}{8}-1} & Y_{\frac{n}{8}} \\
};

\path [>=latex,->] (m-1-1) edge (m-2-1);
\path [>=latex,->] (m-1-1) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-2) edge (m-2-2);
\path [>=latex,->] (m-1-2) edge (m-2-3);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-2);
\path [>=latex,->] (m-1-3) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-4) edge (m-2-5);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-4);
\path [>=latex,->] (m-1-5) edge node [above] {$\ast$} (m-1-6);
\path [>=latex,->] (m-1-6) edge (m-2-6);
\draw[shorten <=-1cm,shorten >=-1cm] ($(m-1-1)!0.5!(m-2-1)$)--($(m-1-6)!0.5!(m-2-6)$);
\end{tikzpicture}

\end{document}

-
Thanks a lot! That's perfect. – hakoja May 1 '12 at 12:19
It might look better without the anchor=center (since then it defaults to anchor=base for matrix of math nodes) and using the mid west, mid east anchors on the arrows. – Loop Space May 1 '12 at 12:28
@AndrewStacey That's probably the way to go. Today somehow, I'm not thinking straight. – percusse May 1 '12 at 12:51

You could make the code even more clean by using every path and every node:

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains, calc}
\begin{document}

\begin{tikzpicture}
\matrix (m)[matrix of math nodes, column  sep=1cm,row  sep=8mm, nodes={draw=none, anchor=center,text depth=0pt} ]{
X_1 & X_2 & X_3 &\ldots & X_{\frac{n}{8}-1} & X_{\frac{n}{8}} \\
Y_1 & Y_2 & Y_3 &\ldots & Y_{\frac{n}{8}-1} & Y_{\frac{n}{8}} \\
};

\tikzstyle{every path}=[>=latex,->]
\tikzstyle{every node}=[above]
\path (m-1-1) edge (m-2-1)
(m-1-1) edge node {$\ast$} (m-1-2)
(m-1-2) edge (m-2-2)
(m-1-2) edge (m-2-3)
(m-1-3) edge node {$\ast$} (m-1-2)
(m-1-3) edge node {$\ast$} (m-1-4)
(m-1-4) edge (m-2-5)
(m-1-5) edge node {$\ast$} (m-1-4)
(m-1-5) edge node {$\ast$} (m-1-6)
(m-1-6) edge (m-2-6);
\draw[shorten <=-1cm,shorten >=-1cm,>=] ($(m-1-1)!0.5!(m-2-1)$)--($(m-1-6)!0.5!(m-2-6)$);
\end{tikzpicture}

\end{document}

-