[Tex/LaTex] How to do this diagram with tikz

tikz-pgf

How can I do this diagram with tikz?

Best Answer

My tikz answer is a little longer than with egreg's xy-pic solution. Here is the code.

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.pathreplacing}

%this shifts a straight line perpendicular to its direction
\tikzstyle{s}=[decorate,decoration={show path construction,
lineto code={
    \draw let 
    \p1 = (\tikzinputsegmentfirst),
    \p2 = (\tikzinputsegmentlast),
    \p3 = ($(\p2)-(\p1)$),
    \p4 = ($(\p1)+{2/veclen(\x3,\y3)}*(\p3)$)
    in
    ($(\p1)!1!90:(\p4)$) -- ++ (\p3)
;}}]

\begin{document}

\begin{tikzpicture}

\matrix[matrix of math nodes,column sep=1cm,row sep=1cm] (m) {
0&K&P&M&0\\
0&K'&P'&M&0\\};

\draw[->] (m-1-1) -- (m-1-2);
\draw[->] (m-1-2) -- (m-1-3);
\draw[->] (m-1-3) -- (m-1-4) node[above,midway] {$\scriptstyle\alpha$};
\draw[->] (m-1-4) -- (m-1-5);
\draw[->] (m-2-1) -- (m-2-2);
\draw[->] (m-2-2) -- (m-2-3);
\draw[->] (m-2-3) -- (m-2-4) node[above,midway] {$\scriptstyle\alpha'$} ;
\draw[->] (m-2-4) -- (m-2-5);

\draw[double equal sign distance,shorten <=5pt,shorten >=5pt] (m-1-4) -- (m-2-4);
\draw[s,->] (m-1-3) -- (m-2-3) node[right,midway] {$\scriptstyle\lambda$};
\draw[s,->] (m-2-3) -- (m-1-3) node[left,midway] {$\scriptstyle\lambda'$};
\end{tikzpicture}
\end{document}

The result is

enter image description here

From comments: There is problem with horizontal lines between node. Here are two solutions. 1- (suggested from the link in the comments) Add text height and width for the nodes. The beginning code for the matrix is then

\matrix[matrix of math nodes,column sep=1cm,row sep=1cm,text height=1.5ex, text depth=0.25ex]

2- Here are two styles you can use to make sure you get horizontal lines. You can also use small adjustments such as \vphantom{'} (see comments). They are a little like the -| and |- to get lines, but without the |.

%draws a horizontal line with the vertical position determined by
%the end point of the specified points.
%hwrend : hor. with respect to end
\tikzstyle{hwrend}=[decorate,decoration={show path construction,
lineto code={
    \draw let 
    \p1 = (\tikzinputsegmentfirst),
    \p2 = (\tikzinputsegmentlast),
    \p3 = ($(\p2)-(\p1)$)
    in
    (\x1,\y2) -- ++ (\x3,0)
;}}]

%hwrstart : hor. with respect to start
\tikzstyle{hwrstart}=[decorate,decoration={show path construction,
lineto code={
    \draw let 
    \p1 = (\tikzinputsegmentfirst),
    \p2 = (\tikzinputsegmentlast),
    \p3 = ($(\p2)-(\p1)$)
    in
    (\x1,\y1) -- ++ (\x3,0)
;}}]
Related Question