# Adjust the length of an arrow to that of another arrow in tikz

I have created a commutative diagram using tikz, and I feel like I'm nearly there. Here is what I did so far:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath,amsfonts}
\usetikzlibrary{matrix,arrows}
\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep=1.5em, column sep=2.5em, text height=1.5ex, text depth=0.25ex]
{ \mathbb{C}[x_1,...,x_s]/J(f) & \mathbb{C}[x_1,...,x_s]/J(g)\\
\mathbb{C}[x]/\langle J(f),x_{s+1},...,x_n\rangle & \mathbb{C}[x]/\langle J(g),x_{s+1},...,x_n\rangle\\ };
\path[->]
(m-1-1) edge node[above] {$\sim$} (m-1-2)
(m-2-1) edge[dashed] node[above] {$\sim$} (m-2-2);
\draw[transparent]
(m-1-1) edge node[rotate=270,opacity=1] {$\cong$} (m-2-1)
(m-1-2) edge node[rotate=270,opacity=1] {$\cong$} (m-2-2);
\end{tikzpicture}
\end{document}


So far I like it, but is there a way to adjust the length of the arrows (1,1) -> (1,2) and (2,1) -> (2,2)? Ideally, they should both be of the same length, namely that of the lower arrow.

-
The quickest fix is to get the intersection nodes: (m-1-1 -| m-2-1.east) edge node[above] {$\sim$} (m-1-2 -| m-2-2.west) in the first row. –  percusse Jun 4 '12 at 10:11
Thank you @percusse! Feel free to post this as an answer, and I'll accept it :) –  InvisiblePanda Jun 4 '12 at 10:15

One simple solution is to reduce the width of the first arrow with the calc library.

Example:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{amsmath,amssymb}
\usetikzlibrary{matrix,arrows,calc}

\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep=1.5em, column sep=2.5em, text height=1.5ex, text depth=0.25ex]
{ \mathbb{C}[x_1,...,x_s]/J(f) & \mathbb{C}[x_1,...,x_s]/J(g)\\
\mathbb{C}[x]/\langle J(f),x_{s+1},...,x_n\rangle & \mathbb{C}[x]/\langle J(g),x_{s+1},...,x_n\rangle\\ };
\path[->]
($(m-1-1.east)+(0.5,0)$) edge node[above] {$\sim$} ($(m-1-2.west)-(0.5,0)$)
(m-2-1) edge[dashed] node[above] {$\sim$} (m-2-2);
\draw[transparent]
(m-1-1) edge node[rotate=270,opacity=1] {$\cong$} (m-2-1)
(m-1-2) edge node[rotate=270,opacity=1] {$\cong$} (m-2-2);
\end{tikzpicture}

\end{document}


Graphical solution:

This could be an approach if that is the only picture you have to adjust otherwise it is possible to automate more the process: 1) based on @percusse comment, 2) using TikZ: How to determine the vector between two co-ordinates to determine the length of the shortest arrow and adapt the other arrows to that.

EDIT by percusse

One can take the orthogonal intersection of different nodes via (m-1-1 -| m-2-1.east) edge node[above] {$\sim$} (m-1-2 -| m-2-2.west) which would allow to get the vertical component of the row and the horizontal component of the end and the start points of the second row entries respectively.

To automate further, one can fix node text width such that every cell would inherit the same text width and centered. Passing the options to the individual cells is possible via nodes={.....} key supplied to the matrix

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath,amsfonts}
\usetikzlibrary{matrix,arrows}
\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
row sep=1.5em,
column sep=2.5em,
nodes={
text width=4cm,
align=center
}
]
{ \mathbb{C}[x_1,...,x_s]/J(f) & \mathbb{C}[x_1,...,x_s]/J(g)\\
\mathbb{C}[x]/\langle J(f),x_{s+1},...,x_n\rangle & \mathbb{C}[x]/\langle J(g),x_{s+1},...,x_n\rangle\\ };
\path[->]
(m-1-1) edge node[above] {$\sim$} (m-1-2)
(m-2-1) edge[dashed] node[above] {$\sim$} (m-2-2);
\draw[transparent]
(m-1-1) edge node[rotate=270,opacity=1] {$\cong$} (m-2-1)
(m-1-2) edge node[rotate=270,opacity=1] {$\cong$} (m-2-2);
\end{tikzpicture}
\end{document}


-
...or (m-1-1) edge[shorten <= .5cm, shorten >= .5cm] node[above] {$\sim$} (m-1-2) also helps avoid using calc. But that's a quick & dirty fix IMHO. The variant with the intersections is the way to go. –  Count Zero Jun 4 '12 at 10:21
@ClaudioFiandrino: +1 from me too :) –  Count Zero Jun 4 '12 at 10:37

As I mentioned in my comment, a quick and dirty fix to avoid using calc could be:

(m-1-1) edge[shorten <= .5cm, shorten >= .5cm] node[above] {$\sim$} (m-1-2)


The big disadvantage is that you need to set the amount to shorten manually, which is not necessarily very accurate.

-