# Aligning text vertically in Tikz

I need to know the correct way of handling vertical alignment in TIKZ. Let me elaborate an example:

\documentclass{article}
\usepackage{amsmath,tikz}
\begin{document}
\begin{tikzpicture}
\node (A1) at (0,4) {($\overset{\surd}{1}$ \indent 2)};
\node (B1) at (0,3) {($\overset{\surd}{3}$ \indent 4)};

\node (A) at (2,4) {$\phi_1$} ;
\node (B) at (2,3) {$\phi_2$} ;

\draw [->, line width=1pt] (A) -- (A1)
node [near start, above]
{
\footnotesize{R1}
};

\node (A2) at (6,4) {$7^1$ \indent $8^1$ \indent $3^1$ \indent $4^1$\indent $\parallel$ 2};
\node (B2) at (6,3) {$1^3$ \indent $2^3$ \indent $5^3$ \indent $6^3$\indent $\parallel$ 4};

%fake caption
\node at (8,4.5) {fake};

\end{tikzpicture}

Which results in this:

I just want to know is ther any way to force the lines into a bottom line here so that $\phi_1$ and the left would be aligned? Should I use phantom to do so or is there any thing better I am unaware of?
The next point is about the word "fake" in the top right corner. I want it over the "2" but I have placed it as a node. I could use \overset{fake}{\overline{2}} to have a "fake" above the "2" but it pushes the line to the left which is not desired. And as you might have guessed I also need a line between "fake" and "2" should I use \overline{2}?

-

I assume you're using amsmath for the \overset. To get the alignment right, stick a [every node/.style = {anchor = base}] as an argument to {tikzpicture}, and to make the arrow from \phi work, you should change its target to (A-|A1.east) (this specifies the point which is at the intersection of a horizontal line from A and a vertical line through A1.east, which gives a nice flat arrow).

For your second question, I suggest you abandon your current approach and use some TikZ libraries. In particular, you should use chains to get nodes in a line easily and with any spacing you like, and positioning to put the "fake" in the right place. Once you do that, you can use a \draw command to get the line you want.

I've also used scopes to simplify the code. It allows me to write braces {...} instead of \begin{scope}...\end{scope}. (If you don't know: scopes in TikZ allow you to pass optional arguments which take effect locally.) Final code:

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{chains, scopes, positioning}
\begin{document}
\begin{tikzpicture}
[every node/.style = {anchor = base}]
\node (A1) at (0,4) {($\overset{\surd}{1}$ \quad 2)};
\node (B1) at (0,3) {($\overset{\surd}{3}$ \quad 4)};

\node (A) at (2,4) {$\phi_1$} ;
\node (B) at (2,3) {$\phi_2$} ;

\draw [->, line width=1pt] (A) -- (A-|A1.east)
node [near start, above]
{
\footnotesize{R1}
};

{ [start chain, every node/.append style = on chain, node distance = 1em]
\node at (4,4) {$7^1$};
\node {$8^1$}; \node {$3^1$}; \node {$4^1$}; \node {$\parallel$};
\node (two) {$2$};
}
{ [start chain, every node/.append style = on chain, node distance = 1em]
\node at (4,3) {$1^3$};
\node {$2^3$}; \node {$5^3$}; \node {$6^3$}; \node {$\parallel$}; \node {$4$};
}

\node (fake) [above = 1ex of two.north] {fake};
\draw (fake.south east) -- (fake.south west);

\end{tikzpicture}
\end{document}
-
Thank you for your answer. I update the code now to reflect a full tex document. The first problem is solved, but how about the fake keyword and line below it? Do you have any suggestion? –  Yasser Sobhdel Dec 30 '10 at 10:44
Included now, second paragraph plus updated example. –  Ryan Reich Dec 30 '10 at 11:19
@Ryan Reich: Can you please tell me how can I markup code block like that? –  user1996 Dec 30 '10 at 11:20
@an_ant: I used the scopes library. I've edited my answer to explain. –  Ryan Reich Dec 30 '10 at 11:39
@an_ant: Oh, I see. You put your code block inside a list; apparently, you need to indent it four additional spaces. You can use the preview to make sure it looks right before committing. –  Ryan Reich Dec 30 '10 at 12:12

This exact problem is discussed in pgfmanual (you can read it running texdoc pgf in bash, also) in "3 Tutorial: A Petri-Net for Hagen" (pg. 37) and in "5 Tutorial: Putting a Diagram in Chains" (pg. 56). Two placing methods discussed there are:

• 3.8 Placing Nodes Using Relative Placement - works like this. You use

\node (waiting) {};
\node (critical) [below of waiting] {};
\node (leave critical) [right of waiting] {};

to place the first node, and every following one in relative positioning to that 1st one, or any other node you name.

• 5.3 Aligning the Nodes Using Matrices - is done this way

\matrix[row sep=1mm,column sep=5mm] {
% First row:
& & & & \node [terminal] {+}; & \\
% Second row:
\node [nonterminal] {unsigned integer}; &
\node [terminal] {.}; &
\node [terminal] {digit};  &
\node [terminal] {E}; &
&
\node [nonterminal] {unsigned integer}; \\
% Third row:
& & & & \node [terminal] {-}; & \\
};

You actually add nodes in matrix, like you would add values in tables - members of rows and columns are therefore aligned appropriately. In combination with row sep, column sep and leaving atoms in matrix empty you can modify spaces between particular items in matrix.

-
For this matter, I'd like placing nodes using coordinates and not using the matrix. About the relative placement, i don't think it would solve the base line problem. Actually if there are imbalanced nodes (in hight). I have the experience of using relative coordinates and it made me to use phantoms to level that up (may be my bad!). –  Yasser Sobhdel Dec 30 '10 at 11:16