# Tikz matrix arrows alignment

I have a problem that I cannot solve for myself.

The following code produces the following image, which I do not really like.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, arrows}
\usepackage{amssymb}

\begin{document}

\begin{center}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep=0em, column sep=4em, minimum width=2em]
{ 0 & \mathbb{Z}\times\mathbb{Z} & \mathbb{Z}^2          & 0 \\
& (\alpha_0, \alpha_1)       & (-\alpha_0, \alpha_1) &   \\
&                            & \beta_{01}            & 0 \\ };
\path[>=stealth, ->]
(m-1-1.east|-m-1-2) edge node [above] {\scriptsize$d_{-1}$} (m-1-2.west|-m-1-2)
(m-1-2.east|-m-1-2) edge node [above] {\scriptsize$d_{ 0}$} (m-1-3.west|-m-1-2)
(m-1-3.east|-m-1-2) edge node [above] {\scriptsize$d_{ 1}$} (m-1-4.west|-m-1-2);
\path[>=stealth, |->]
(m-2-2.east|-m-2-2) edge (m-2-3.west|-m-2-2);
\path[>=stealth, |->]
(m-3-3.east|-m-3-3) edge (m-3-4.west|-m-3-3);
\end{tikzpicture}
\end{center}

\end{document}


What I want to achieve is having arrows aligned, more or less as in the following image

Does someone have some hint?
I prefer to use tikz, but any other package can be welcome.

• instead of minimum width=2em put nodes={text width=1.5cm} (tweak if you wish). Aug 21, 2014 at 12:57
• Thanks for the hint. Unfortunately this alignes the text on the left, which is even uglier than before :S Aug 21, 2014 at 13:03
• Thanks again for pointing me in the right direction. Indeed this almost solves my problem: nodes={text width=1.5cm, align=center}. The problem is with the last 0 on the right, which gets too distant from the arrow. Aug 21, 2014 at 13:19
• I managed to solve it with the use of the column styler, I'll write an answer. Aug 21, 2014 at 13:25

Unless you use TiKZ for some other reason. There is no need to use it to obtain your diagram.

With help from "The Comprehensive LaTeX symbols list" (for \xrightarrow and \xmapsto) and egreg's answer to Make box with minimum width (in math mode) (for \minwidthbox) is possible to write:

\documentclass{article}
\usepackage{amsmath,amssymb,empheq,mathtools}

\DeclareRobustCommand{\minwidthbox}[2]{%
\ifmmode
\expandafter\mathmakebox
\else
\expandafter\makebox
\fi
[\ifdim#2<\width\width\else#2\fi]{#1}%
}

\begin{document}

$\begin{array}{ccccccc} 0 & \xrightarrow{\minwidthbox{d_{-1}}{3em}} & \mathbb{Z}\times\mathbb{Z} & \xrightarrow{\minwidthbox{d_0}{3em}} & \mathbb{Z}^2 & \xrightarrow{\minwidthbox{d_1}{3em}} & 0 \\ & & (\alpha_0,\alpha_1) & \xmapsto{\minwidthbox{}{3em}} & (-\alpha_0,\alpha_1) & & \\ & & & & \beta_{01} & \xmapsto{\minwidthbox{}{3em}} & 0 \end{array}$
\end{document}


which produces

Thanks to @percusse for pointing me in the right direction to find the solution.

Styling column by column is a solution to this problem (altought not as elegant as I expected).
The point is to modify each column definition to obtain what I want, so for example

column 2/.style={nodes={text width=1.5cm, align=center}}


The final code is the following

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, arrows}
\usepackage{amssymb}

\begin{document}

\begin{center}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep=0em, column sep=4em, column 2/.style={nodes={text width=1.5cm, align=center}},column 3/.style={nodes={text width=1.5cm, align=center}}]
{ 0 & \mathbb{Z}\times\mathbb{Z} & \mathbb{Z}^2          & 0\\
& (\alpha_0, \alpha_1)       & (-\alpha_0, \alpha_1) & \\
&                            & \beta_{01}            & 0\\ };
\path[>=stealth, ->]
(m-1-1.east|-m-1-2) edge node [above] {\scriptsize$d_{-1}$} (m-1-2.west|-m-1-2)
(m-1-2.east|-m-1-2) edge node [above] {\scriptsize$d_{ 0}$} (m-1-3.west|-m-1-2)
(m-1-3.east|-m-1-2) edge node [above] {\scriptsize$d_{ 1}$} (m-1-4.west|-m-1-2);
\path[>=stealth, |->]
(m-2-2.east|-m-2-2) edge (m-2-3.west|-m-2-2);
\path[>=stealth, |->]
(m-3-3.east|-m-3-3) edge (m-3-4.west|-m-3-3);
\end{tikzpicture}
\end{center}

\end{document}


You could also define an \Arrow command of fixed width and variable style and let the \matrix manage the alignment and column widths instead of doing it manually:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, arrows}
\usepackage{amssymb}

\newcommand{\Arrow}[2][->]{%
\tikz[baseline=-0.7ex, x=2cm, #1]
\path (0,0) edge node [above] {\scriptsize#2} (1,0);
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep=0em]
{ 0 & \Arrow{$d_{-1}$} & \mathbb{Z}\times\mathbb{Z} & \Arrow{$d_{0}$} & \mathbb{Z}^2          & \Arrow{$d_{1}$} & 0 \\
&                  & (\alpha_0, \alpha_1)       & \Arrow[|->]{}   & (-\alpha_0, \alpha_1) &                 &   \\
&                  &                            &                 & \beta_{01}            &  \Arrow[|->]{}  & 0 \\ };
\end{tikzpicture}
\end{center}
\end{document}


Result:

• don't nest tikzpictures. They are always trouble. Aug 21, 2014 at 14:25
• You mean styles might leak from the parent picture to the child picture? That doesn't seem to be a problem in this simple example, but of course it's good to be aware that it might be the cause of strange problems. I guess it would be possible to achieve this without nesting if the matrix of nodes key were removed. Aug 21, 2014 at 14:33