I'm trying to draw a explanation of SAD (Sum of Absolute differences). After a lot of tweaking I have something that sort of looks OK except that the horizontal as well as the vertical alignment/distances are terrible! And, as you can see, everything is hardcoded.
How can I get everything to line up beautifully?
Label Right which isn't aligned with Left and Center as well as the vertical distance between the 3 "rows". The Left, Center & Right matrixes should probably be moved to the right a bit to have everything centered on some imaginary vertical axis. To add a offset repetable until it looks better is a pain since there are many values to change, e.g. for the grids.
Output:

MWE:
\documentclass[convert={density=300,size=640x480,outext=.png}]{standalone}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{adjustbox}
\usetikzlibrary{calc, matrix}
\begin{document}
\begin{adjustbox}{max size={.95\textwidth}{.8\textheight}}
\begin{tikzpicture}
\draw[step=0.5cm,color=lightgray] (-.75,-.75) grid (.75,.75);
\matrix (template) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
2 & 5 & 5 \\
4 & 0 & 7 \\
7 & 5 & 9 \\
};
\node[label={[xshift=0cm, yshift=0cm]Template}] at (template.north) {};
\draw[step=0.5cm,color=lightgray] (1.75,-.75) grid (4.25,.75);
\matrix (search) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (3,0){
2 & 7 & \node[red]{5}; & \node[red]{8}; & \node[red]{6}; \\
1 & 7 & \node[red]{4}; & \node[red]{2}; & \node[red]{7}; \\
8 & 4 & \node[red]{6}; & \node[red]{8}; & \node[red]{5}; \\
};
\node[label={Search Image}] at (search.north) {};
\draw[step=0.5cm,color=lightgray] (-1.75,-3.75) grid (-.25,-2.25);
\matrix (left) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (-1,-3){
0 & 2 & 0 \\
3 & 7 & 3 \\
1 & 1 & 3 \\
};
\node[label={Left}] at (left.north) {};
\draw[step=0.5cm,color=lightgray] (.25,-3.75) grid (1.75,-2.25);
\matrix (center) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (1,-3){
5 & 0 & 3 \\
3 & 4 & 5 \\
3 & 1 & 1 \\
};
\node[label={Center}] at (center.north) {};
\draw[step=0.5cm,color=lightgray] (2.25,-3.75) grid (3.75,-2.25);
\matrix (right) [matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}] at (3,-3){
\node[red]{3}; & \node[red]{3}; & \node[red]{1}; \\
\node[red]{0}; & \node[red]{2}; & \node[red]{0}; \\
\node[red]{1}; & \node[red]{3}; & \node[red]{4}; \\
};
\node[label={Right}] at (right.north) {};
\matrix (sad) [matrix of nodes] at (1,-5) {
20 & 25 & \node[red]{17}; \\
};
\node[label={SAD}] at (sad.north) {};
% ..
\end{tikzpicture}
\end{adjustbox}
\end{document}


\documentclassand the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Apr 24 '14 at 20:27Rightwhich isn't aligned withLeftandCenteras well as the vertical distance between the 3 "rows" – Fredrik Pihl Apr 24 '14 at 20:32