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 tried to put up a nice little graphic, to make an example for String-Matching. My Problem is the "b" is not aligned with a and c and i can't find a way to handle this:

\begin{figure}[h]

            \begin{tikzpicture}[align=bottom,shorten >=1pt,node distance=1cm,on grid,auto]
              \node(q_0) {$c$};
              \node(q_1) [right of=q_0] {$b$};
              \node(q_2) [right of=q_1] {$a$};
              \node(q_3) [right of=q_2] {$c$};
              \node(q_4) [right of=q_3] {$b$};
              \node(q_5) [right of=q_4] {$a$};
              \node(q_6) [right of=q_5] {$b$};
              \node(q_7) [right of=q_6] {$a$};

              \node(t_0) [below of=q_0]{$a$};
              \node(t_1) [right of=t_0] {$c$};
              \node(t_2) [right of=t_1] {$b$};
              \node(t_3) [right of=t_2] {$a$};
              \node(t_4) [right of=t_3] {$a$};
              \node(t_5) [right of=t_4] {$c$};
              \node(t_6) [right of=t_5] {$b$};
              \node(t_7) [right of=t_6] {$a$};
              \node(t_8) [right of=t_7] {$c$};
              \node(t_9) [right of=t_8] {$b$};
              \node(t_10) [right of=t_9] {$a$};
              \node(t_11) [right of=t_10] {$b$};
              \node(t_12) [right of=t_11] {$c$};

              \path[->] (q_0) edge node {$\neq$} (t_0);
              \end{tikzpicture}
    \end{figure}
share|improve this question
1  
There is no align=bottom. Instead add every node/.append style={text height=2ex,text depth=0.5ex} to the picture options. –  percusse Jul 17 at 9:26
    
@percusse Thanks this did help me alot –  Daniel Jul 17 at 9:32
    
@cfr sadly it didnt work but percusse`s did thanks guys –  Daniel Jul 17 at 9:33
    
@Daniel I know. I deleted it before reading your comment because I tried it and couldn't get it to work. See page 229 of the manual for why I thought it might. Also, I used this for something yesterday and that seemed to work so now I'm really very confused! –  cfr Jul 17 at 9:44
    
@Daniel Apparently on grid redefines the anchors? (I don't entirely understand the explanation in the docs...) –  cfr Jul 17 at 10:01

2 Answers 2

You can use

every node/.style={font=\strut}

enter image description here

Code

\documentclass[tikz,margin=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[
    shorten >=1pt,
    every node/.style={font=\strut},
    node distance=1cm,
    auto
  ]
  \node(q_0) {$c$};
  \node(q_1) [right of=q_0] {$b$};
  \node(q_2) [right of=q_1] {$a$};
  \node(q_3) [right of=q_2] {$c$};
  \node(q_4) [right of=q_3] {$b$};
  \node(q_5) [right of=q_4] {$a$};
  \node(q_6) [right of=q_5] {$b$};
  \node(q_7) [right of=q_6] {$a$};

  \node(t_0) [below of=q_0]{$a$};
  \node(t_1) [right of=t_0] {$c$};
  \node(t_2) [right of=t_1] {$b$};
  \node(t_3) [right of=t_2] {$a$};
  \node(t_4) [right of=t_3] {$a$};
  \node(t_5) [right of=t_4] {$c$};
  \node(t_6) [right of=t_5] {$b$};
  \node(t_7) [right of=t_6] {$a$};
  \node(t_8) [right of=t_7] {$c$};
  \node(t_9) [right of=t_8] {$b$};
  \node(t_10) [right of=t_9] {$a$};
  \node(t_11) [right of=t_10] {$b$};
  \node(t_12) [right of=t_11] {$c$};

  \path[->] (q_0) edge node {$\neq$} (t_0);
\end{tikzpicture}
\end{document}

But I would use a matrix to position the nodes

\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,auto]
  \matrix[
      matrix of math nodes,
      row sep={1cm,between origins},
      column sep={1cm,between origins}
    ](m){%
      c&b&a&c&b&a&b&a\\
      a&c&b&a&a&c&b&a&c&b&a&b&c\\
   };
  \path[->](m-1-1)edge node{$\neq$}(m-2-1);
\end{tikzpicture}
\end{document}
share|improve this answer
    
+1 for the matrix –  JLDiaz Jul 17 at 9:50
    
+1 I like this. I always think matrices look neat as solutions yet I've never used one. (I think it is the name - I assume they are irrelevant if I'm not doing maths!) –  cfr Jul 17 at 10:06

I think the matrix of nodes is a more elegant solution, but a less elegant alternative would be to use base right=of from the positioning library rather than on grid.

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
  \begin{tikzpicture}[shorten >=1pt,node distance=1cm]
      \node(q_0)  {$c$};
      \node(q_1) [base right=of q_0] {$b$};
      \node(q_2) [base right=of q_1] {$a$};
      \node(q_3) [base right=of q_2] {$c$};
      \node(q_4) [base right=of q_3] {$b$};
      \node(q_5) [base right=of q_4] {$a$};
      \node(q_6) [base right=of q_5] {$b$};
      \node(q_7) [base right=of q_6] {$a$};

      \node(t_0) [below of=q_0]{$a$};
      \node(t_1) [base right=of t_0] {$c$};
      \node(t_2) [base right=of t_1] {$b$};
      \node(t_3) [base right=of t_2] {$a$};
      \node(t_4) [base right=of t_3] {$a$};
      \node(t_5) [base right=of t_4] {$c$};
      \node(t_6) [base right=of t_5] {$b$};
      \node(t_7) [base right=of t_6] {$a$};
      \node(t_8) [base right=of t_7] {$c$};
      \node(t_9) [base right=of t_8] {$b$};
      \node(t_10) [base right=of t_9] {$a$};
      \node(t_11) [base right=of t_10] {$b$};
      \node(t_12) [base right=of t_11] {$c$};

    \path[->] (q_0) edge node {$\neq$} (t_0);
  \end{tikzpicture}
\end{document}

aligned nodes

Optically, these do not look aligned to me but it seems they are when I put a box around them in my viewer!

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.