仅当页面不是实际页面时才引用浮动对象的页面

仅当页面不是实际页面时才引用浮动对象的页面

是否可以告诉 LaTeX,如果引用的对象不在实际页面上,则仅使用 \pageref?

if \pageref{fig:hello_world} != self.page then
  \pageref{fig:hello_world}
end

答案1

我想你也许会喜欢varioref和/或cleveref

varioref

截屏

\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

截屏

\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}

答案2

您可以使用refcount包获取参考文献的页码。然后您可以使用包xifthen对整数值进行比较。您可以使用\value{page}命令获取当前页面的值。在下面的 MWE 中,您可以注释或取消注释\clearpage以演示行为的差异。

\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}

您现在可以通过更改命令来简化事情pageref,以便您可以像平常一样使用 pageref,但它会运行您的条件代码。

\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}

相关内容