# references to floats without ever using figure numbers (possbily using lua or python)

Stephen Wolfram's 'A New Kind of Science' states in its colophon that it has 973 illustrations spread out over 1280 pages. Remarkably, he accomplishes this without ever using figure numbers. No figure has any number and each figure is referenced in the text using statements like '... the picture on the facing page ...' or '... the picture below ... ' or '... as illustrated to the left ...', etc.

Leaving aside the dubious science in this book, I find the positioning in the book to be a tour de force of book layout. My question is whether this can be accomplished using TeX?

I have never seen, or heard mention of, any LaTeX package that does anything like this, so I presume accomplishing something like this would require fine grained control of LaTeX's float positioning algorithm and implementing a custom solution, probably using LuaTeX or pythonTeX. Wolfram states that he used Mathematica to position the figures and to solve "innumerable small geometrical puzzles to do so".

• I think either varioref or cleveref does something similar to this, if the figure is on the same or adjacent page. – You Jun 28 '14 at 19:19
• Does Lua(La)TeX provide a "hook" (possibly in the form of a callback) into LaTeX's float placement algorithm? – Mico Jun 28 '14 at 19:34
• @You - The varioref will indeed let you create such page references, but it still requires you to create floats with captions that are assigned labels. – Mico Jun 28 '14 at 20:14
• @Mico: Not printing the label while still keeping the reference isn't really a technical problem. There will be lots of manual float placement hinting and making sure references aren't ambiguous, though. – You Jun 28 '14 at 20:25
• @You varioref doesn't really have a restriction to it being on the same or adjacent page, it could easily say "top of page x" if the float is further away. – David Carlisle Jun 28 '14 at 23:52

This can handle up to 11 pictures per page.

\documentclass{article}
\usepackage{graphics,mwe}

\makeatletter
\newcounter{relative}
\newcount\R@temp
\newif\ifR@label
\newif\ifR@loop

\newcommand\new@relative[3]{% #1=label, #2=count, #3=page
\expandafter\gdef\csname R@#1@count\endcsname{#2}%
\expandafter\gdef\csname R@page#2\endcsname{#3}%
}%
\newcommand\Rlabel[1]{% #1 = label
\@ifundefined{hypertarget}{}{\hypertarget{R@#1}{}}%
\stepcounter{relative}%
\@bsphack
\begingroup
\protected@write\@auxout{}{%
\string\new@relative{#1}{\arabic{relative}}{\thepage}%
}%
\endgroup
\@esphack
}%
\newcommand\Rref[1]{% #1 = label
\@ifundefined{R@#1@count}{\R@labelfalse}{\R@labeltrue}%
\ifR@label%
\edef\R@count{\csname R@#1@count\endcsname}%
\edef\R@page{\csname R@page\R@count\endcsname}%
% find first picture on page
\R@temp=\R@count
\ifnum\R@temp>0
\if\csname R@page\the\R@temp\endcsname\R@page \R@looptrue%
\else\R@loopfalse\fi%
\else\R@loopfalse\fi%
\ifR@loop\repeat%
\edef\R@first{\the\R@temp}%
% find the last picture on page
\@ifundefined{R@page\the\R@temp}{\R@loopfalse}{\R@looptrue}%
\ifR@loop
\if\csname R@page\the\R@temp\endcsname\R@page \relax%
\else\R@loopfalse\fi%
\fi%
\ifR@loop\repeat%
\edef\R@last{\the\R@temp}%
% same page
\if\R@page\thepage
\R@temp=\c@relative%
\ifnum\R@temp=0 {\picture{} above}%
\else\ifnum\R@temp=-1 {\picture{} below}%
\else\if\R@count\R@first{top \picture{} above}%
\else\if\R@count\R@last{bottom \picture{} below}%
\else\ifnum\R@temp>0 {\R@num{\the\R@temp} \picture{} above}%
\R@temp=-\R@temp%
{\R@num{\the\R@temp} \picture{} below}%
\fi\fi\fi\fi\fi%
% only one picture on page
\else\if\R@first\R@last
\R@temp=\c@page%
\ifnum\R@temp=-1 {\picture{} on the next page}%
\else\ifnum\R@temp=1 {\picture{} on the previous page}%
\else {\picture{} on page \R@page}%
\fi\fi%
% multiple pictures on page
\else\R@temp=\R@count
\edef\R@number{\the\R@temp}% 0=top, 1=second...
\R@temp=\c@page%
\ifnum\R@temp=1
\if\R@count\R@first{top \picture{} on the previous page}%
\else\if\R@count\R@last{bottom \picture{} on the previous page}%
\else{\R@num{\R@number} \picture{} on the previous page}%
\fi\fi%
\else\ifnum\R@temp=-1
\if\R@count\R@first{top \picture{} on the next page}%
\else\if\R@count\R@last{bottom \picture{} on the next page}%
\else{\R@num{\R@number} \picture{} on the next page}%
\fi\fi%
\else%
\if\R@count\R@first{top \picture{} on page \R@page}%
\else\if\R@count\R@last{bottom \picture{} on page \R@page}%
\else{\R@num{\R@number} \picture{} on page \R@page}%
\fi\fi\fi\fi\fi\fi%
% label not defined
\else{as yet unknown \picture}
\fi}%
% some definitions
\def\R@num#1{\csname R@num#1\endcsname}%
\expandafter\edef\csname R@num1\endcsname{second}%
\expandafter\edef\csname R@num2\endcsname{third}%
\expandafter\edef\csname R@num3\endcsname{fourth}%
\expandafter\edef\csname R@num4\endcsname{fifth}%
\expandafter\edef\csname R@num5\endcsname{sixth}%
\expandafter\edef\csname R@num6\endcsname{seventh}%
\expandafter\edef\csname R@num7\endcsname{eighth}%
\expandafter\edef\csname R@num8\endcsname{ninth}%
\makeatother

\begin{document}

\begin{center}
\Rlabel{first}\includegraphics{example-image-a}
\end{center}

You may have noticed in the \Rref{first} and the \Rref{second}
that they are not quite the same as the \Rref{third}.

\begin{center}
\Rlabel{second}\includegraphics{example-image-b}
\end{center}

You may have noticed in the \Rref{first} and the \Rref{second}
that they are not quite the same as the \Rref{third}.

\begin{center}
\Rlabel{third}\includegraphics{example-image-c}
\end{center}

You may have noticed in the \Rref{first} and the \Rref{second}
that they are not quite the same as the \Rref{third}.

\Rref{test1}\\
\Rref{test2}\\
\Rref{test3}\\
\Rref{test4}\\
\Rref{test5}\\

\newpage
\Rref{test1}\\
\Rref{test2}\\
\Rref{test3}\\
\Rref{test4}\\
\Rref{test5}

\begin{center}
\Rlabel{test1}\Rlabel{test2}\Rlabel{test3}\Rlabel{test4}\Rlabel{test5}%
\includegraphics{example-image}
\end{center}

\Rref{test1}\\
\Rref{test2}\\
\Rref{test3}\\
\Rref{test4}\\
\Rref{test5}\\
\end{document}