[Tex/LaTex] Adjust the length of an arrow to that of another arrow in tikz

tikz-pgf

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}

enter image description here

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.

Best Answer

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:

enter image description here

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}

enter image description here