new version
The idea from percusse here can be applied in this case and the code is better.
\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter
\tikzset{minimum dist/.code 2 args={%
\path (#1);
\pgfgetlastxy{\xa}{\ya}
\path (#2);
\pgfgetlastxy{\xb}{\yb}
\pgfpointdiff{\pgfpoint{\xa}{\ya}}%
{\pgfpoint{\xb}{\yb}}%
\pgf@xa=\pgf@x}
,
minimum width=\pgf@xa
}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw] (a) {a\strut};
\node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
\node[rectangle,draw,below=0.2cm of b.south east,
anchor=north east,minimum dist={a.west}{b.east}] (ab) {ab\strut};
\end{tikzpicture}
\end{document}
A macro is not a function ( mathematical term). When yo use minimum width
you need to give a length. I'm not a great (TeX)pert but I think it's difficult to make exactly what you want (call a macro and get only the length).
A possibility
\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter
\tikzset{minimum dist/.style n args={4}{%
insert path={%
\pgfextra{%
\pgfpointdiff{\pgfpointanchor{#1}{#2}}%
{\pgfpointanchor{#3}{#4}}%
\pgf@xa=\pgf@x}
},
minimum width=\pgf@xa}
}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw] (a) {a\strut};
\node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
\node[rectangle,draw,below=0.2cm of b.south east,
anchor=north east,minimum dist={a}{west}{b}{east}] (ab) {ab\strut};
\end{tikzpicture}
\end{document}
Update
A variant to use 2 arguments only.
\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter
\tikzset{minimum dist/.style 2 args={%
insert path={%
\pgfextra{%
\path (#1);
\pgfgetlastxy{\xa}{\ya}
\path (#2);
\pgfgetlastxy{\xb}{\yb}
\pgfpointdiff{\pgfpoint{\xa}{\ya}}%
{\pgfpoint{\xb}{\yb}}%
\pgf@xa=\pgf@x}
},
minimum width=\pgf@xa}
}
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw] (a) {a\strut};
\node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
\node[rectangle,draw,below=0.2cm of b.south east,
anchor=north east,minimum dist={a.west}{b.east}] (ab) {ab\strut};
\end{tikzpicture}
\end{document}

If you need to get the minimum height, the next code gives the two dimensions
\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter
\newdimen\y@min@dim
\newdimen\x@min@dim
\tikzset{h minimum dist/.code 2 args={%
\path (#1);
\pgfgetlastxy{\xa}{\ya}
\path (#2);
\pgfgetlastxy{\xb}{\yb}
\pgfpointdiff{\pgfpoint{\xa}{\ya}}%
{\pgfpoint{\xb}{\yb}}%
\y@min@dim=\pgf@y}
,
minimum height=\y@min@dim
}
\tikzset{w minimum dist/.code 2 args={%
\path (#1);
\pgfgetlastxy{\xa}{\ya}
\path (#2);
\pgfgetlastxy{\xb}{\yb}
\pgfpointdiff{\pgfpoint{\xa}{\ya}}%
{\pgfpoint{\xb}{\yb}}%
\x@min@dim=\pgf@x}
,
minimum width=\x@min@dim
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[rectangle,draw] (a) {a\strut};
\node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
\node[rectangle,draw,below=0.2cm of b.south east,
anchor=north east,
w minimum dist={a.west}{b.east}] (hab) {h ab\strut};
\node[rectangle,draw,right=0.2cm of b.south east,
anchor=south west,
h minimum dist={b.south}{a.north}
] (wab) {w ab\strut};
\node[rectangle,draw=red,
anchor=north west,
h minimum dist={b.south}{a.north},
w minimum dist={a.west}{b.east},
] (test) at (a.north west) {test};
\end{tikzpicture}
\end{document}

One would assume that you're dealing with real numbers that you want to compare. In that sense, a comparison of numbers is not appropriate, since they do not work with fractional components. Dimensions (or lengths) on the other hand do. You can trick a function to work with lengths rather than numbers in the following way:

\documentclass{article}
\makeatletter
\newcommand{\compare}[1]{%
\ifdim\dimexpr#1pt+45pt>\z@
above%
\else
left%
\fi
}
\makeatother
\begin{document}
$1$ is \compare{1}, % 1 + 45 = 46 > 0 -> above
while $-90$ is \compare{-90} % -90 + 45 = -45 < 0 -> left
and $-45$ is \compare{-45}. % -45 + 45 = 0 ... -> left
\end{document}
The function \compare{<num>}
uses <num>
in a "dimension" form as <num>pt
, adds 45pt
to that, checks if this is greater than \z@
(or 0pt
) and conditions accordingly.
Note the use of %
to avoid spurious spaces. See What is the use of percent signs (%
) at the end of lines?
Best Answer
This is actually very simple using a normal LaTeX counter: