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.

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:

enter image description here

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}
share|improve this question

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer
1  
@Jake Ah, I completely missed that question. Thanks! Added now. –  percusse May 1 '12 at 11:56
    
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}
share|improve this answer

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.