3

If a figure is mentioned in a text, say page 10, but the corresponding figure appears on page 11, how can I automatically introduce some additional text (e.g.: on page 11) beside the figure mention in the text.

Instead of doing this: Figure \ref{fig:test} on page \pageref{fig:test} shows bla bla.

Autocheck: Figure \\ref{fig:test} \check shows bla bla.

Where \check is a macro to check the co-existence of the figure referencing and figure (float), whether they are on the same page. If true, return Null, else return on page \pageref{fig:test}.

siv
  • 31

2 Answers2

2

Consider using the varioref package and its \vpageref command to reference the page associated with another \label. It does a check to see whether the reference is on the same page. If so, the default text (on this page) is printed. You can override this globally, or locally by supplying an optional argument. You're most likely looking for \vpageref[]{<label>} with an empty optional argument (so as to print nothing).

enter image description here

\documentclass{article}

\usepackage{graphicx,lipsum} \usepackage[nospace]{varioref}

\begin{document}

\lipsum[1]

\begin{figure} \centering \includegraphics[width=.3\linewidth]{example-image} \caption{A figure} \label{fig:figure} \end{figure}

\lipsum[2]

See Figure~\ref{fig:figure} on page~\pageref{fig:figure}.

See Figure~\ref{fig:figure} \vpageref{fig:figure}.

See Figure~\ref{fig:figure} \vpageref[]{fig:figure}.% Note the empty optional argument

\lipsum[3]

\end{document}

Since \vpageref itself sets a \label in order to compare the page numbers its referencing, you'll need to compile twice with every change in the location of \vpageref.

Werner
  • 603,163
2

OpTeX users can create following definition of the \check macro:

\def\check{\label[x/\_lastreflabel]\wlabel{}%
   \space
   \ea\ifx\csname _pgref:x/\_lastreflabel\ea\endcsname 
          \csname _pgref:\_lastreflabel\endcsname
   \else on page \pgref[\_lastreflabel] \fi
}

% Test:

Figure \ref[fig:label]\check shows bla bla.

%\vfil\break %% <-- you can comment out this to show the effect

... a figure ... \cskip \caption/f [fig:label] Figure blba bla

\bye

Of course, you have to run TeX twice when changes were done.

The macro \check creates its own label x/label an writes its position to the .ref file using \wlabel{}. The following \ifx checks it the reference macros \_pgref:x/label and \_pgref:label are the same. If it is true then it does nothing else it writes the text on page \pgref[label] .

wipet
  • 74,238