# Distinguish forward cross-references from backward cross-references

Andrew gave an answer to the question in How to refer to indexed words (which is working very, very nicely). As it happens, it allows referring forward to where the word will be defined as well as the usual backward. This is actually very helpful as when explaining at the beginning of a chapter what will be done, but would be even more helpful if one could differentiate forward from backward, say with a different hyperref color. Here is the MWF that Andrew answered modified to use hyperref color instead of boxes and with a reference 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
\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
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},%
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}


So, my question is: how can forward cross-references be made made to look different from backward cross-references?

• Is there a question in here, or is this a neat feature that you've added to a different answer? If the latter, I like that this example exists, but I don't think a "question" is the proper place for it. – Teepeemm Jan 14 '18 at 20:25
• @Teepeemm I had thought that by saying that _ it would be even more helpful if one could differentiate ahead from back_ I had acknowledged that I had not been able to implement this "neat feature". Also, it is not in the MWE. I have edited my post to specifically ask for help in adding the feature. – schremmer Jan 14 '18 at 21:14
• @schremmer: By the way, you have some questions with unaccepted answers... – user31729 Jan 14 '18 at 21:17
• @Hupfer I will try to find them because I don't like to do that. Any hint would be helpful though. – schremmer Jan 14 '18 at 21:19

As long as only the \DEX macro is used in order to generate references to the linked words, there is a quite simple way to distinguish between forward and backward links:

Check for the current page number and compare with the page reference of the linked word with \ifnum\value{page} > \getpagerefnumber{ind:#2}...\else...\fi.

In the first case we have a back reference, in the second case we have a forward reference, for references on the same page this will not work (yet), I will try to update later on (i.e. tomorrow) with a better version)

I've provided two new colors: forwardlinkcolor and backwardlinkcolor, change their definitions at will.

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


The full code:

\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
\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
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][]{%
\begingroup
\ifnum\value{page} > \getpagerefnumber{ind:#2}%
\hypersetup{%
}%
\else
\hypersetup{%
}%
\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},%
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{%

• @schremmer: I don't understand what you mean? Which last command? \printindex? I see the index in my output file – user31729 Jan 15 '18 at 12:47
• I spoke too soon. The above works fine but in the magnum opus it does not distinguish forward and backward. Also, it seems it does not like it when a referred word is in a table. Not sure though, no time to really check. If you want to, Text-contents > 1.tex, available at the same place, has a mini example and a table at the top and Preamble.sty has a fork. – schremmer Feb 1 '18 at 17:01
• It does not seem to like it either when the referred word is to be formatted as, e.g., \DEX[zap]{\emph{zap}} which works fine in the original DEX. – schremmer Feb 3 '18 at 23:12