我希望前向引用的颜色与后向引用的颜色不同。无论是否使用,这都应该有效hyperref
(实际上,我只使用它hyperref
,但为了完整性,它hyperref
也应该在没有的情况下工作)。
我需要这个来检查参考未来定理的证明(通常表示有错误)。
如果目前还没有适用于此的 LaTeX 包,那么应该设计一个符合我的想法的新包。
@HeikoOberdiek 感谢您的示例。但我需要这个来与\ref
命令一起使用\myref
(因为我使用 LyX 进行交叉引用)。我尝试修改您的代码,但它不起作用。请帮忙找出错误:
\documentclass{article}
\usepackage{xcolor}
\usepackage[colorlinks]{hyperref}
\usepackage{auxhook}
\newcounter{labelknownref}
\renewcommand*{\thelabelknownref}{\the\value{labelknownref}}
\makeatletter
\AddLineBeginAux{%
\string\providecommand\string\LabelKnown[2]{}%
}
\newcommand*{\LabelKnown}[2]{%
\expandafter\xdef\csname lkr@#2\endcsname{%
\@ifundefined{r@#1}{0}{1}%
}%
}
% \LetLtxMacro{\OldRef}{\ref}
\let\OldRef=\ref
\renewcommand*{\ref}[1]{%
\begingroup
\stepcounter{labelknownref}%
\if@filesw
\protected@write\@auxout{}{%
\string\LabelKnown{#1}{\thelabelknownref}%
}%
\fi
\if\csname lkr@\thelabelknownref\endcsname 1%
\hypersetup{linkcolor=blue}%
\OldRef{#1}\textsuperscript{\textcolor{blue}{(ok)}}%
\else
\if\csname lkr@\thelabelknownref\endcsname 0%
\hypersetup{linkcolor=red}%
\OldRef{#1}\textsuperscript{\textcolor{red}{(forward reference!)}}%
\else
\hypersetup{linkcolor=orange}%
\OldRef{#1}\textsuperscript{\textcolor{orange}{(unknown)}}%
\fi
\fi
\endgroup
}
\makeatother
\begin{document}
\noindent
A forward reference to section \ref{sec:intro}.
\section{Introduction}
\label{sec:intro}
In this section \ref{sec:intro} we introduce something\\
and show figures \ref{fig:top} and \ref{fig:bottom}.
\begin{figure}[t]
\caption{Figure at the top}
\label{fig:top}
\end{figure}
\begin{figure}[b]
\caption{Figure at the bottom}
\label{fig:bottom}
\end{figure}
\end{document}
答案1
下面的示例定义了宏,它将带有两个参数的命令\myref
写入文件,即引用的标签名称和此实例的唯一标识号(同一个标签可以被引用多次)。在下次运行 LaTeX 时读取文件时,如果已经读取标签命令并且知道标签引用。这意味着,标签的写入命令是在的写入请求之前发出的,并且标签很可能在引用之前定义。然后为引用设置适当的颜色并添加注释文本以供说明。.aux
\LabelKnown
\myref
.aux
\begin{document}
\LabelKnown
\LabelKnown
\myref
\documentclass{article}
\usepackage{xcolor}
\usepackage[colorlinks]{hyperref}
\usepackage{auxhook}
\newcounter{labelknownref}
\renewcommand*{\thelabelknownref}{\the\value{labelknownref}}
\makeatletter
\AddLineBeginAux{%
\string\providecommand\string\LabelKnown[2]{}%
}
\newcommand*{\LabelKnown}[2]{%
\expandafter\xdef\csname lkr@#2\endcsname{%
\@ifundefined{r@#1}{0}{1}%
}%
}
\newcommand*{\myref}[1]{%
\begingroup
\stepcounter{labelknownref}%
\if@filesw
\protected@write\@auxout{}{%
\string\LabelKnown{#1}{\thelabelknownref}%
}%
\fi
\if\csname lkr@\thelabelknownref\endcsname 1%
\hypersetup{linkcolor=blue}%
\ref{#1}\textsuperscript{\textcolor{blue}{(ok)}}%
\else
\if\csname lkr@\thelabelknownref\endcsname 0%
\hypersetup{linkcolor=red}%
\ref{#1}\textsuperscript{\textcolor{red}{(forward reference!)}}%
\else
\hypersetup{linkcolor=orange}%
\ref{#1}\textsuperscript{\textcolor{orange}{(unknown)}}%
\fi
\fi
\endgroup
}
\makeatother
\begin{document}
\noindent
A forward reference to section \myref{sec:intro}.
\section{Introduction}
\label{sec:intro}
In this section \myref{sec:intro} we introduce something\\
and show figures \myref{fig:top} and \myref{fig:bottom}.
\begin{figure}[t]
\caption{Figure at the top}
\label{fig:top}
\end{figure}
\begin{figure}[b]
\caption{Figure at the bottom}
\label{fig:bottom}
\end{figure}
\end{document}
重新定义\ref
\ref
被不同的包多次定义和重新定义。以下示例加载nameref
较早,否则它将在 中加载。旧含义通过包\AtBeginDocument
保存,因为通常通过 定义。此外,实际的重新定义被移动到。\LetLtxMacro
letltxmacro
\ref
\DeclareRobustCommand
\begin{document}
\documentclass{article}
\usepackage{xcolor}
\usepackage[colorlinks]{hyperref}
\usepackage{auxhook}
\newcounter{labelknownref}
\renewcommand*{\thelabelknownref}{\the\value{labelknownref}}
\makeatletter
\AddLineBeginAux{%
\string\providecommand\string\LabelKnown[2]{}%
}
\newcommand*{\LabelKnown}[2]{%
\expandafter\xdef\csname lkr@#2\endcsname{%
\@ifundefined{r@#1}{0}{1}%
}%
}
\usepackage{nameref}% load it now, because it redefines \ref
\usepackage{letltxmacro}
\AtBeginDocument{%
\LetLtxMacro\myorgref\ref
\DeclareRobustCommand*{\ref}[1]{%
\begingroup
\stepcounter{labelknownref}%
\if@filesw
\protected@write\@auxout{}{%
\string\LabelKnown{#1}{\thelabelknownref}%
}%
\fi
\if\csname lkr@\thelabelknownref\endcsname 1%
\hypersetup{linkcolor=blue}%
\myorgref{#1}\textsuperscript{\textcolor{blue}{(ok)}}%
\else
\if\csname lkr@\thelabelknownref\endcsname 0%
\hypersetup{linkcolor=red}%
\myorgref{#1}\textsuperscript{\textcolor{red}{(forward reference!)}}%
\else
\hypersetup{linkcolor=orange}%
\myorgref{#1}\textsuperscript{\textcolor{orange}{(unknown)}}%
\fi
\fi
\endgroup
}%
}
\makeatother
\begin{document}
\noindent
A forward reference to section \ref{sec:intro}.
\section{Introduction}
\label{sec:intro}
In this section \ref{sec:intro} we introduce something\\
and show figures \ref{fig:top} and \ref{fig:bottom}.
\begin{figure}[t]
\caption{Figure at the top}
\label{fig:top}
\end{figure}
\begin{figure}[b]
\caption{Figure at the bottom}
\label{fig:bottom}
\end{figure}
\end{document}