1

Given a text with an indexed word, say minimum, is it possible to have a command, call it \DEX{}, that would print a later occurrence of that word and link it to the original occurrence? For an on-screen pdf, this would allow the reader to bypass the index by just clicking on the word s/he wants to check while \Cpageref{minimum}(\Cpageref{index:minimum}?) would allow the reader similarly to bypass the index on a printed copy.

Could this work with a word defined in a tcolorbox? In my case, though, this would be icing on the cake because, the magnum opus being written for raw beginners, most words are not formally defined, only a very few are. Besides, I already use cleveref for that. So, just asking.

Here is a MWE (Not quite minimum because I need the ``quick and dirty hack'' due to Heiko Oberdiek which loads showidx and I included a tcolorbox just in case someone wants to try it.)

    \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

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

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

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


\newpage

A function can have more than one $\backslash$DEX\{minimum\} ($\backslash$Cpageref\{minimum\}.)


(The command $\backslash$DEX would print  the word minimum and link it to the above occurrence. Hopefully, $\backslash$Cpageref would still work too.)

\printindex
    \end{document}

and here is the pdf (without \newpage and without the index page). Note the swhowidx on the upper right:

enter image description here

  • 2
    Isn't this something for a glossary? – user31729 Sep 29 '17 at 18:08
  • @ChristianHupfer Not sure what you mean. The idea is to bypass the index: instead of 1. looking up minimum in the index and then 2. clicking on the page number 1, why not just click on the later occurrence of minimum? – schremmer Oct 2 '17 at 13:57
3

Perhaps I don't understand what you want to do but it seems to me that all you need to do is to make your \INDEX command add a \label{...} for your index entry and then your \DEX command refers back to it. As you want to make a link in the PDF file you should use \hyperref from the hyperref package to create this link, rather than \ref. For good measure, the \DEX should also insert the \Cpageref command.

Putting this together, your \INDEX and \DEX commands become:

\newcommand{\INDEX}[1]{\index{#1}\label{ind:#1}}
\newcommand\DEX[1]{\hyperref[ind:#1]{#1} (\Cpageref{ind:#1})}

I have made the automatically generated index \label's of the form ind:... so that they are less likely to conflict with other labels.

With this is place, here is the output on page 2, showing the mouse-over that's over the word minimum:

enter image description here

(I use skim to view PDFs and one of its many great features is this mouse-over. Acrobat, for example, doesn't do this.)

EDIT

As noted in the comments, this approach goes wrong when indexing mathematics, such as \INDEX{$\pm$}. This really isn't surprising because having mathematics inside an index or label key is almost certain to cause problems but this means that to cope with cases like these we need to be able to manually override the label that is being used. This is most easily done using the xparse package that allows use to define \INDEX and \DEX as

\NewDocumentCommand\INDEX{ O{#2} m}{\index{#2}\label{ind:#1}}
\NewDocumentCommand\DEX{ O{#2} m }{\hyperref[ind:#1]{#2} (\Cpageref{ind:#1})}

What this means is that \INDEX now has an optional argument, which defaults to the value of the second argument, that is used in the labels. So, to index $\pm$ we can now type

\INDEX[pm]{$\pm$}

Similarly, the \DEX also accepts an optional argument, so to refer to $\pm$ we would use \DEX[pm]{$\pm$}. We only need top use the optional arguments to \INDEX and \DEX if what we are indexing is "complicated" in some way, such as in mathematics mode, and otherwise we use these two macros as before.

[Incidentally, \newcommand also caters for optional arguments (see Defining macros with arguments). The only reason that have used \NewDocumentCommand is that is has an easier mechanism for dealing with default values.]

Here is 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
  \@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

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

\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} \DEX[]{Carla}.

\printindex


    \end{document}
  • I agree but isn't using \label superfluous? After all, the reference is already in the .idx file, namely \indexentry{minimum|hyperpage}{1}. So, DEX ought to be able to use it like a sort of nameref. (But I don't know how to do that.) – schremmer Oct 2 '17 at 13:53
  • @schremmer nI theory, yes, one could extract the reference from the index but doing this would be slightly painful because I'm fairy sure that no one has provided a mechanism for doing this. More importantly, the hyperlinks from the index point only to page numbers rather than to the actual reference on the page whereas \label{...} and \ref{...} give fairly precise hyperlinks. Given this, I think that constructing an extra \ref{...} is worth doing. – Andrew Oct 10 '17 at 6:47
  • Now I understand why you wrote "Perhaps I don't understand what you want to do ...": modifying my \INDEXcommand to include a \label was of course the obvious way to go. Which goes to show that I don't know anything about LaTeX--which I well know. But I like to understand which is why I asked the question you just answered. I am most grateful for the explanation. Still, in these days of live links, someone ought to suggest to Heiko Oberdieck to upgrade the index. But I am happy. – schremmer Oct 11 '17 at 15:07
  • A curio: \INDEX{$x_{0}$} and \label{ind:$x_{0}$} both work but while \INDEX{$\pm$} works, \label{ind:$\pm$} causes Missing \endcsname inserted. <to be read again> \pm l.120 ...$\pm $}{{1.2.3}{4}{}{subsection.1.2.3}{}} ? . – schremmer Oct 12 '17 at 16:52
  • @schremmer I would never use math-mode inside a reference, which is why this is failing here, but I guess with your use-case this is problematic. I would make \INDEX and \DEX both take an optional argument that allows you to set the label "manually" in cases like these where you are indexing mathematics. I'll edit the post to do this. – Andrew Oct 12 '17 at 17:11

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.