[Tex/LaTex] How to vertically align the labels in a tikz diagram

diagramstikz-pgf

enter image description here


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$?

Best Answer

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

enter image description here