区分前向交叉引用和后向交叉引用

区分前向交叉引用和后向交叉引用

Andrew 在如何引用索引词(正在工作)非常非常好)。碰巧的是,它允许引用forward单词的定义位置以及通常的backward。这实际上非常有用,例如在章节开头解释将要做什么时,但如果可以区分,例如使用不同的,则会更有帮助forwardbackwardhyperref color是 Andrew 回答的 MWF,修改后使用hyperref color而不是方框并带有参考forward

    \documentclass[11pt]{book}
    \usepackage{makeidx}
    \makeindex
    \usepackage{tcolorbox}
%==================Begin HYPERREF
    \usepackage[hyperfootnotes=false]{hyperref}%[pdfborder={0 0 0}]

%Begin the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\makeatletter
\ifHy@hyperindex
  \def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
  \let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
  \def\HyInd@showidx#1{%
    \@showidx{#1}%
    \if@nobreak
      \ifvmode
        \nobrak
      \fi
    \fi
  }%
% Load package showidx
  \let\siOrg@makeindex\makeindex
  \let\siOrg@@index\@index
  \let\siOrg@@wrindex\@wrindex
  \let\siOrg@index\index
  \RequirePackage{showidx}
  \let\makeindex\siOrg@makeindex
  \let\@index\siOrg@@index
  \let\@wrindex\siOrg@@wrindex
  \let\index\siOrg@index
% rest of hyperref part
  \@ifpackageloaded{multind}{%
    \let\HyInd@org@wrindex\@wrindex
    \def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
    \def\HyInd@@wrindex#1#2|#3|#4\\{%
      \ifx\\#3\\%
        \HyInd@org@wrindex{#1}{#2|hyperpage}%
      \else
        \def\Hy@temp@A{#3}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          HyInd@org@wrindex{#1}{#2|#3hyperpage}%
        \else
          \HyInd@org@wrindex{#1}{#2|#3}%
        \fi
      \fi
    }%
  }{%
    \def\@wrindex#1{\@@wrindex#1||\\}
    \def\@@wrindex#1|#2|#3\\{%
      \ifx\\#2\\%
        \protected@write\@indexfile{}{%
          \string\indexentry{#1|hyperpage}{\thepage}%
        }%
      \else
        \def\Hy@temp@A{#2}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          \protected@write\@indexfile{}{%
             \string\indexentry{#1|#2hyperpage}{\thepage}%
          }%
        \else
          \protected@write\@indexfile{}{%
            \string\indexentry{#1|#2}{\thepage}%
          }%
        \fi
      \fi
      \endgroup
      \HyInd@showidx{#1}%
      \@esphack
    }%
  }%
\fi
\makeatother
%End the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
    \hypersetup{%begin colors
                   colorlinks,
                   linkcolor={red!50!black},
                   citecolor={blue!50!black},
                  urlcolor={blue!80!black}
                }%end colors

    \usepackage{cleveref}
%Begin the Sharpe code as modified by Schulz (2016-02-1)
\let\oldopenparen\(
\let\oldcloseparen\)
\def\({\protect\oldopenparen}
\def\){\protect\oldcloseparen}
%End the Sharpe code as modified by Schulz (2016-02-1)

\newcommand{\INDEX}[2][]{%
\if\relax\detokenize{#1}\relax\index{#2}\label{ind:#2}%
   \else\index{#2}\label{ind:#1}\fi%
}
\newcommand{\DEX}[2][]{%
   \if\relax\detokenize{#1}\relax\hyperref[ind:#2]{#2} (\Cpageref{ind:#2})%
   \else\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})\fi%
}%

\newcounter{abc}%
\newtcolorbox%
[%beginForReferences
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={definition}{definitions},
Crefname={Definition}{Definitions},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=DEFINITION \thetcbcounter,%
#1,%Required for references.
}%



    \begin{document}

A \textbf{minimum}\INDEX{minimum} of a function is blah blah.

And here is a reference ahead: \DEX[]{Carla}

\begin{greement}[label={abc:3-1}]{Maximum}
  An input whose output is larger than the outputs of nearby inputs
\end{greement}

\INDEX[pm]{$\pm$}
\newpage

A function can have more than one \DEX{minimum}.

A function can have more than one \DEX{minimum}.

\chapter{one} \textbf{Andy}\INDEX[]{Andy} \chapter{two} \textbf{Beth}\INDEX[]{Beth} \chapter{three} \textbf{Carla}\INDEX[]{Carla} \chapter`{four}` \DEX[]{Andy} \DEX[]{Beth} .

\printindex


    \end{document}

所以,我的问题是:如何才能forward cross-references使看起来与众不同backward cross-references

答案1

只要仅\DEX使用宏来生成对链接词的引用,就有相当简单的方法可以区分前向和后向链接:

检查当前页码并与带有 的链接词的页码引用进行比较\ifnum\value{page} > \getpagerefnumber{ind:#2}...\else...\fi

在第一种情况下,我们有一个向后引用,在第二种情况下,我们有一个向前引用,对于同一页面上的引用,这还不起作用(目前),我将尝试稍后(即明天)使用更好的版本进行更新)

我提供了两种新的颜色:forwardlinkcolorbackwardlinkcolor,随意更改它们的定义。

\newcommand{\DEX}[2][]{%
  \begingroup
  \ifnum\value{page} > \getpagerefnumber{ind:#2}%
  \hypersetup{%
    linkcolor=backwardlinkcolor
  }%
  \else
  \hypersetup{%
    linkcolor=forwardlinkcolor
  }%
  \fi
  \if\relax\detokenize{#1}\relax\hyperref[ind:#2]{#2} (\Cpageref{ind:#2})%
  \else\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})\fi%
  \endgroup
}%

完整代码:

\documentclass[11pt]{book}
\usepackage{makeidx}
\makeindex
\usepackage{tcolorbox}
% ==================Begin HYPERREF
\usepackage[hyperfootnotes=false]{hyperref}%[pdfborder={0 0 0}]

%Begin the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\makeatletter
\ifHy@hyperindex
  \def\HyInd@ParenLeft{(}%
% Hook in \HyInd@@wrindex
  \let\HyInd@showidx\@empty
% Hook in \HyInd@@wrindex for package showidx
  \def\HyInd@showidx#1{%
    \@showidx{#1}%
    \if@nobreak
      \ifvmode
        \nobrak
      \fi
    \fi
  }%
% Load package showidx
  \let\siOrg@makeindex\makeindex
  \let\siOrg@@index\@index
  \let\siOrg@@wrindex\@wrindex
  \let\siOrg@index\index
  \RequirePackage{showidx}
  \let\makeindex\siOrg@makeindex
  \let\@index\siOrg@@index
  \let\@wrindex\siOrg@@wrindex
  \let\index\siOrg@index
% rest of hyperref part
  \@ifpackageloaded{multind}{%
    \let\HyInd@org@wrindex\@wrindex
    \def\@wrindex#1#2{\HyInd@@wrindex{#1}#2||\\}%
    \def\HyInd@@wrindex#1#2|#3|#4\\{%
      \ifx\\#3\\%
        \HyInd@org@wrindex{#1}{#2|hyperpage}%
      \else
        \def\Hy@temp@A{#3}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          HyInd@org@wrindex{#1}{#2|#3hyperpage}%
        \else
          \HyInd@org@wrindex{#1}{#2|#3}%
        \fi
      \fi
    }%
  }{%
    \def\@wrindex#1{\@@wrindex#1||\\}
    \def\@@wrindex#1|#2|#3\\{%
      \ifx\\#2\\%
        \protected@write\@indexfile{}{%
          \string\indexentry{#1|hyperpage}{\thepage}%
        }%
      \else
        \def\Hy@temp@A{#2}%
        \ifx\Hy@temp@A\HyInd@ParenLeft
          \protected@write\@indexfile{}{%
             \string\indexentry{#1|#2hyperpage}{\thepage}%
          }%
        \else
          \protected@write\@indexfile{}{%
            \string\indexentry{#1|#2}{\thepage}%
          }%
        \fi
      \fi
      \endgroup
      \HyInd@showidx{#1}%
      \@esphack
    }%
  }%
\fi
\makeatother
%End the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx
\hypersetup{%begin colors
  colorlinks,
  linkcolor={red!50!black},
  citecolor={blue!50!black},
  urlcolor={blue!80!black}
}%end colors

\usepackage{cleveref}
%Begin the Sharpe code as modified by Schulz (2016-02-1)
\let\oldopenparen\(
\let\oldcloseparen\)
\def\({\protect\oldopenparen}
\def\){\protect\oldcloseparen}
%End the Sharpe code as modified by Schulz (2016-02-1)

\newcommand{\INDEX}[2][]{%
\if\relax\detokenize{#1}\relax\index{#2}\label{ind:#2}%
   \else\index{#2}\label{ind:#1}\fi%
}

\colorlet{forwardlinkcolor}{red!50!black}
\colorlet{backwardlinkcolor}{green!40!blue}

\newcommand{\DEX}[2][]{%
  \begingroup
  \ifnum\value{page} > \getpagerefnumber{ind:#2}%
  \hypersetup{%
    linkcolor=backwardlinkcolor
  }%
  \else
  \hypersetup{%
    linkcolor=forwardlinkcolor
  }%
  \fi
  \if\relax\detokenize{#1}\relax\hyperref[ind:#2]{#2} (\Cpageref{ind:#2})%
  \else\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})\fi%
  \endgroup
}

\newcounter{abc}%
\newtcolorbox%
[%beginForReferences
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={definition}{definitions},
Crefname={Definition}{Definitions},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=DEFINITION \thetcbcounter,%
#1,%Required for references.
}%



\begin{document}

A \textbf{minimum}\INDEX{minimum} of a function is blah blah.

And here is a reference ahead: \DEX[]{Carla}

\begin{greement}[label={abc:3-1}]{Maximum}
  An input whose output is larger than the outputs of nearby inputs
\end{greement}

\INDEX[pm]{$\pm$}
\newpage

A function can have more than one \DEX{minimum}.

A function can have more than one \DEX{minimum}.

\chapter{one} \textbf{Andy}\INDEX[]{Andy} \chapter{two} \textbf{Beth}\INDEX[]{Beth} \chapter{three} \textbf{Carla}\INDEX[]{Carla} \chapter`{four}` \DEX[]{Andy} \DEX[]{Beth} .


\clearpage
\hypersetup{%
  linkcolor=backwardlinkcolor
}
\printindex


\end{document}

相关内容