Rectangular diagram with TikZ

I have a problem with getting rectangular diagrams in LaTeX. For example, the diagram below gives a diagram which looks like a trapezium. What do I need to change to get what I want ?

\begin{center}
\begin{tikzpicture}
% Tell it where the nodes are
\node (A) {$short expression$};
\node (B) [below=of A] {$long expression$};
\node (C) [right=of A] {$short expression$};
\node (D) [right=of B] {$long expression$};
% Tell it what arrows to draw
\draw[-stealth] (A)-- node[left] {} (B);
\draw[stealth-] (B)-- node [below] {} (D);
\draw[stealth-] (A)-- node [above] {} (C);
\draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}
\end{center}

-
 I've edited your code into a block: you can highlight the code and click the "code" button (with "{}" on it). – Joseph Wright♦ Dec 12 '11 at 18:39 I'm going to warn you about my question tex.stackexchange.com/q/5786/575 right now, since you're going to come up against this problem really soon, I think. (If you have nodes aligned at the bottom and you draw an edge from one to the other, it will slant if they are of different heights. This happens if you put, say, \times in one but not the other.) – Ryan Reich Dec 12 '11 at 21:15

You need to specify a minimum width for each of the nodes so that they are all the same size. If this is something you will be doing a lot, it's easiest to create a style for this, as I've done in the example below:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[fixed/.style={minimum width={1.25in}}]

% Tell it where the nodes are
\node[fixed] (A) {$short expression$};
\node[fixed] (B) [below=of A] {$long expression$};
\node[fixed] (C) [right=of A] {$short expression$};
\node[fixed] (D) [right=of B] {$long expression$};
% Tell it what arrows to draw
\draw[-stealth] (A)-- node[left] {} (B);
\draw[stealth-] (B)-- node [below] {} (D);
\draw[stealth-] (A)-- node [above] {} (C);
\draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}

\end{document}


This solution has a certain advantage over using the every node/.style since if you have other nodes in the drawing you may not want them all to be of the same size.

-

The matrix library is very adapted for this kind of diagram. minimum width is not necessary only if you want to have edges with the same length.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
\matrix [matrix of math nodes,row sep=2cm,column sep=2cm,minimum width=2cm]
{
|(A)| \displaystyle\frac{B}{A} &   |(B)| C                  \\
|(C)| A\times B                &   |(D)|  A\times B\times C \\
};
\draw[->]  (A)--(B);
\draw[->]  (A)--(C);
\draw[<->] (C)--(D);
\draw[->]  (B)--(D);
\end{tikzpicture}

\end{document}


-
 This is, in my opinion, the correct answer. With TikZ it's often hard to know which of the many equivalent techniques is appropriate, but a situation like this will show you which ones are unfavorable. Eventually you learn what different mental models are favored by the different techniques and how to discern which model is yours (for this picture). A commutative diagram is basically a grid, and it is "wrong" to refer to one entry as being preferentially "above" another rather than "right of" a third. – Ryan Reich Dec 12 '11 at 21:11

You could use nodes with the same dimensions, you can achieve it here for example by setting a minimum width for all nodes, similar for the height:

\begin{tikzpicture}[every node/.style={minimum width = 20ex}]
% Tell it where the nodes are
\node (A) {$short expression$};
\node (B) [below=of A] {$long expression$};
\node (C) [right=of A] {$short expression$};
\node (D) [right=of B] {$long expression$};
% Tell it what arrows to draw
\draw[-stealth] (A)-- node[left] {} (B);
\draw[stealth-] (B)-- node [below] {} (D);
\draw[stealth-] (A)-- node [above] {} (C);
\draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}


If you don't like to have equal widths, but wish to have a rectangle, you can get horizontal and vertical lines also with different dimensions in such a way:

\draw[-stealth] (C) -- node {} (C|-D.north);


such as in

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (A) {$A\times B$};
\node (B) [below=of A] {$\displaystyle\frac{B}{A}$};
\node (C) [right=of A] {$C$};
\node (D) [right=of B] {$A\times B\times C$};
\draw[-stealth] (A)-- node[left] {} (B);
\draw[stealth-] (B)-- node [below] {} (D);
\draw[stealth-] (A)-- node [above] {} (C);
\draw[-stealth] (C)-- node {} (C|-D.north);
\end{tikzpicture}
\end{document}


Generally try correct positioning first, as Tom wrote. Here, below=of C for D could center D horizontally with C, but then vertical centering with B is lost. It can be corrected in any way, but I think it's good do know that you can get a vertical or horizontal lines using |- or -| relation expressions.

-

It was quite easy: just position node D below of B:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{center}
\begin{tikzpicture}
% Tell it where the nodes are
\node (A) {$short expression$};
\node (B) [below=of A] {$long expression$};
\node (C) [right=of A] {$short expression$};
\node (D) [below=of C] {$long expression$};
% Tell it what arrows to draw
\draw[-stealth] (A)-- node[left] {} (B);
\draw[stealth-] (B)-- node [below] {} (D);
\draw[stealth-] (A)-- node [above] {} (C);
\draw[-stealth] (C)-- node [right] {} (D);,
\end{tikzpicture}
\end{center}

\end{document}


-