# How can I vertically align the labels in a tikz diagram

I have drawn the above diagram with diagonal arrows in TikZ, but the labels look like they have been placed on a curve instead of next to each other. I think I understand why they are placed like that: TikZ puts the labels in the middle of the arrows and the diagonal arrows end slightly lower. But I'm unable to fix this.

The code I have written for this diagram is:

\documentclass[border=1]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}

\begin{document}
\begin{tikzpicture}
[
arr/.style={->,font=\scriptsize},
description/.style={fill=white,inner sep=2pt}
]
\matrix (m) [
matrix of math nodes, row sep=3em, column sep=4em, text height=1.5ex, text depth=0.25ex
]
{A[f]_0:=A & A[f]_1 & A[f]_2 & \cdots \\
&        & B      \\};
\draw[arr] (m-1-1) -- node[above]       {$\pi_0$}  (m-1-2);
\draw[arr] (m-1-1) -- node[description] {$f_0:=f$} (m-2-3);
\draw[arr] (m-1-2) -- node[above]       {$\pi_1$}  (m-1-3);
\draw[arr] (m-1-2) -- node[description] {$f_1$}    (m-2-3);
\draw[arr] (m-1-3) -- node[above]       {$\pi_2$}  (m-1-4);
\draw[arr] (m-1-3) -- node[description] {$f_2$}    (m-2-3);
\draw[arr] (m-1-4) -- node[description] {$f_3$}    (m-2-3);
\end{tikzpicture}
\end{document}


So the question is: how can I put the labels $f_0:=f$, $f_1$, $f_2$ and $f_3$ at the same height, preferably the height of $f_2$?

-
a very ugly hack is to tweak the positions manually, using something like node[description,pos=0.4]... I'm sure one of the gurus will have a better method, but at least this gives you something :) – cmhughes May 27 '12 at 17:50
@cmhughes: How will I be able to determine the right number to give to pos? It seems unlikely that this is going to be very precise... – Egbert May 27 '12 at 18:27
that's why I said it was an ugly hack :) – cmhughes May 27 '12 at 18:35
You can place the label you prefer ($f_2$?) like you did and place other nodes over intersections of an horizontal line through f_2 and arrows. Not so easy like your solution but possible. – Ignasi May 27 '12 at 18:35
That sounds like a solution. But I'm not sure how to do that. Could you give an example of how to do it? – Egbert May 27 '12 at 18:50

As I told you in my comment you need to set an horizontal line. I selected f_2 as a reference point and named it while placing on the path

\draw[arr] (m-1-3) -- node[description] (f2) {$f_2$} (m-2-3)


the horizontal line crossing f2 is declared with

\path[name path=pf2] (m-1-1|-f2)--(m-1-4|-f2);


Then you need to name all other paths like

\draw[arr,name path=pf0] (m-1-1) -- (m-2-3);


and place labels over intersections

\node[arr,description,name intersections={of=pf0 and pf2}] (f0) at (intersection-1) {$f_0:=f$};


My complete code is

\documentclass[border=1]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,intersections}

\begin{document}

\begin{tikzpicture}
[
arr/.style={->,font=\scriptsize},
description/.style={fill=white,inner sep=2pt}
]
\matrix (m) [
matrix of math nodes, row sep=3em, column sep=4em, text height=1.5ex, text depth=0.25ex
]
{A[f]_0:=A & A[f]_1 & A[f]_2 & \cdots \\
&        & B      \\};
\draw[arr] (m-1-1) -- node[above]       {$\pi_0$}  (m-1-2);
\draw[arr] (m-1-2) -- node[above]       {$\pi_1$}  (m-1-3);
\draw[arr] (m-1-3) -- node[above]       {$\pi_2$}  (m-1-4);

\draw[arr] (m-1-3) -- node[description] (f2) {$f_2$}    (m-2-3);
\draw[arr,name path=pf0] (m-1-1) -- (m-2-3);
\draw[arr,name path=pf1] (m-1-2) -- (m-2-3);
\draw[arr,name path=pf3] (m-1-4) -- (m-2-3);
\path[name path=pf2] (m-1-1|-f2)--(m-1-4|-f2);
\draw[red] (m-1-1|-f2)--(m-1-4|-f2);

\node[arr,description,name intersections={of=pf0 and pf2}] (f0) at (intersection-1) {$f_0:=f$};
\node[arr,description,name intersections={of=pf1 and pf2}] (f1) at (intersection-1) {$f_1$};
\node[arr,description,name intersections={of=pf3 and pf2}] (f3) at (intersection-1) {$f_3$};
\end{tikzpicture}

\end{document}


and the result

-
This looks very nice, great solution! – Egbert May 28 '12 at 8:53