You can check the y-coordinate of a mark on a page and perform a comparison to decide whether you should print above
or below
. zref
's savepos
module can help with that:
\documentclass{article}
\usepackage{zref-savepos}
\newcounter{whereq}
\newcommand{\location}[1]{\zsaveposy{#1}}
\newcommand{\where}[1]{%
\stepcounter{whereq}% New "where" request
\location{whereq-\thewhereq}% Mark location
% Test location based on y-coordinate
\ifdim\zposy{whereq-\thewhereq}sp<\zposy{#1}sp
above%
\else
below%
\fi}
\begin{document}
As mentioned \where{test}, $2+2=4$.
We state that $2+2=4$.\location{test}
As mentioned \where{test}, $2+2=4$.
\end{document}
Of course, the above doesn't really care what page you perform the \location
and \where
macros on. However, it could be extended to condition on the page number as well.