# Referencing to “above” or “below”?

I would like to know if it is good practice, and if so how it can be achieved, to make references like: "as mentioned above...". Or am I going to far and should I just type the word?

I've read something about varioref, but it seems to produce page numbers or section numbers, but I can't find how you can just print if a section is above or below the reference.

-
Please have a look to the related question Style in the table below. To say it short: no, it is not a good practice ... –  Kurt Jan 31 '13 at 0:14

You can also implement the labels yourself by having labels define a flag and references check the flag existence. Some additional bookkeeping is needed to warn about undefined labels.

\documentclass{article}

\makeatletter
\newcount\here@undef
\newcommand{\here}[1]{%
\@namedef{here@#1}{}}
\newcommand{\where}[1]{%
\@ifundefined{here@#1}{%
below%
\@ifundefined{here@#1@undef}{%
\@namedef{here@#1@undef}{}%
}{}%
}{%
above%
}%
}
\AtEndDocument{%
\ifnum\here@undef>0
\GenericWarning{}{There were undefined above/below labels}%
\fi}
\makeatother

\begin{document}

As mentioned \where{test}, $2+2=4$.

We state that $2+2=4$.\here{test}

As mentioned \where{test}, $2+2=4$.

\end{document}


-
I find it so strange that everything needs some extra programming and that such simple things that are easily found in word (shouldn't mention that here maybe ;)) are still undefined in Latex. This looks like a nice solution. Thanks. –  Marnix Oct 22 '11 at 9:50
@Marnix: This likely means that the feature is not widely used. Most people will not be arbitrarily moving text blocks after they write them, so there is no need for a programmatic choice: you write either above or below directly. Besides, such references are only useful at short distances, hence what varioref does in Daniel's answer. –  Andrey Vihrov Oct 22 '11 at 9:55
@Andrey: Would it not be better to just patch the label command (or use \myspeciallabel instead of \label') and have that do what \here does now? Otherwise, you would need to add a \here command to many labels. –  einpoklum Oct 28 '11 at 8:07
@EyalRozenberg: Yes, it could be done so. –  Andrey Vihrov Oct 28 '11 at 9:35
@Marnix, conversely I suspect that one reason many prefer latex to word is precisely because they can write code like this to do just about anything, whereas in word it has to be a 'feature'. –  a different ben Jan 12 at 23:23

varioref can in fact do this you just need to give its \vpageref command a hint what to write to refer to an element on the same page. From the documentation:

But in fact \vpageref allows even more control. If has two optional arguments. With the first one, one can specify the text that should be used if label and reference fall on the same page. This is very helpful if both are near to each other, so that they may or may not be separated by a page break. In such a case we usually know (!) whether the reference is before or after the label so that we can say something like ... see the example \vpageref[above]{ex:foo} which shows ... which will then come out as “. . . see the example above which shows . . . ”

The point is that if there is a page break in between the label and the element, \vpageref will notice this and insert something like on the previous page instead.

-
After reading the documentation, it sounds quite certain that it will produce above when the page and the label are on the same page. But what if I always want to use above and below. This only seems to specify above assuming that the label is above the reference. I can't make this assumption. –  Marnix Oct 21 '11 at 23:22
I'd like to come back to this. What I'd like to achieve is that references to a chapter come with an arrow whose direction depends on whether the referenced chapter is above or below (←/→ chapter 2). Neither the accepted solution nor varioref` does allow this kind of automatic switching between 'above' and 'below'. –  Simifilm May 15 '13 at 8:51
@Simifilm: Please consider asking this as a dedicated question, including a complete minimal working example (MWE) that illustrates your problem. –  Daniel May 15 '13 at 8:56