3

Is it possible to tell LaTeX only use \pageref if the referenced object isn't on actual page?

if \pageref{fig:hello_world} != self.page then
  \pageref{fig:hello_world}
end
1
  • 1
    Do you know package varioref (and cleveref)? varioref adds \pageref into the reference, if the figure or table is more than one page away.
    – Mensch
    Aug 30 '12 at 16:18
2

I think perhaps you might like the varioref and/or the cleveref packages

varioref

screenshot

\documentclass{article}

\usepackage{varioref}

\begin{document}

\begin{itemize}
  \item Here is a reference to Table \vref{tab:thispage}. 
  \item Here's a reference to Table \vref{tab:nextpage}.
\end{itemize}

\begin{table}[!htb]
  \centering
  \begin{tabular}{cc}
    1 & 2
  \end{tabular}
  \caption{Table}
  \label{tab:thispage}
\end{table}

\clearpage
\setcounter{page}{20}

\begin{table}[!htb]
  \centering
  \begin{tabular}{cc}
    1 & 2
  \end{tabular}
  \caption{Table}
  \label{tab:nextpage}
\end{table}
\end{document}

cleveref

screenshot

\documentclass{article}

\usepackage{varioref}
\usepackage{cleveref}

% each of the following has two versions
%   \crefname{environmentname}{singular}{plural}, to be used mid-sentence
%   \Crefname{environmentname}{singular}{plural}, to be used at the beginning of a sentence

% standard environments
\crefname{table}{Table}{Tables}
\Crefname{table}{Table}{Tables}

\begin{document}

\begin{itemize}
  \item Here is a reference to  \vref{tab:thispage}. 
  \item Here's a reference to \vref{tab:nextpage}.
\end{itemize}

\begin{table}[!htb]
  \centering
  \begin{tabular}{cc}
    1 & 2
  \end{tabular}
  \caption{Table}
  \label{tab:thispage}
\end{table}

\clearpage
\setcounter{page}{20}

\begin{table}[!htb]
  \centering
  \begin{tabular}{cc}
    1 & 2
  \end{tabular}
  \caption{Table}
  \label{tab:nextpage}
\end{table}
\end{document}
4

You can use the refcount package to get the page number of the reference. Then you can use the xifthen package to do a comparison of integer values. You can get the value of the current page with the \value{page} command. In the MWE below, you can comment or uncomment the \clearpage to demonstrate the difference in behavior.

\documentclass{article}
\usepackage{refcount}
\usepackage{xifthen}
\usepackage[demo]{graphicx}

\begin{document}
\begin{figure}
    \caption{Testing}\label{fig:test}
    \includegraphics[width=0.4\textwidth]{temp}
\end{figure}
%Uncomment the clearpage command to get the pageref to print
%\clearpage
\ifthenelse{\getpagerefnumber{fig:test}=\value{page}}{}{\pageref{fig:test}}

\end{document}

You could now simplify things by changing the pageref command, so that you could can use pageref like normal, but it would run your conditional code.

\documentclass{article}
\usepackage{refcount}
\usepackage{xifthen}
\usepackage[demo]{graphicx}

\let\originalPageref\pageref
\renewcommand{\pageref}[1]{\ifthenelse{\getpagerefnumber{#1}=\value{page}}{}{\originalPageref{#1}}}
\begin{document}
\begin{figure}
    \caption{Testing}\label{fig:test}
    \includegraphics[width=0.4\textwidth]{temp}
\end{figure}
%Uncomment the clearpage command to get the pageref to print
%\clearpage
\pageref{fig:test}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.