2

It's hard to find where exactly an indexed term is defined in my article, when reading the PDF version. When you click on the link in the index table, say for the keyword "Laplace distribution", rather than pointing to where \index{Laplace distribution} is exactly located (say in the middle or bottom of page 17), it points to the top of page 17, making it hard to find where the term is actually used, especially if it is at the bottom of the page, and each page has dense content.

Is there a good solution to this problem? I get a little improvement by changing 10pt to 12pt in \documentclass[10pt]{article}, at the top of the preambule, and by making margins much larger, because then pages contain much less content and it is easier to visually find the actual location as there is less text to browse on each page. But I assume that there are much more efficient solutions. Another way to improve the situation is to make the keyword in question (Laplace distribution) appear in italic or in red. What do you recommend? Ideally I'd like the index to work just as well as when you reference a theorem or equation using \ref and \label: in that case, clicking on the reference link brings you exactly where the theorem or equation is located.

2
  • Ad "clicking on the reference link brings you exactly where the theorem or equation is located": Assume the keyword "Laplace distribution" occurs several times/in several locations on page 17. Then page 17 is listed only once in the index-entry for "Laplace distribution". Which of the locations on page 17 to scroll to/to navigate to when clicking on the index's list-entry for page 17? Dec 17, 2021 at 10:10
  • Yes, "Laplace distribution" may occur 3 times on page 17 (and on other pages too, though for some keyword it's only one time). But the command \index{Laplace distribution} occur only once in the entire document, and that's where I'd like the index link to point at when checking the index table. Dec 17, 2021 at 10:19

1 Answer 1

1

If it is ensured "by hand" that each index-entry occurs only once per page within an \index-command, then as a starting-point I can offer a macro \Autodestinationindex which takes an optional argument and a mandatory argument.

The mandatory argument is the phrase for the index-entry. The optional argument can hold a key-value list with the keys Up=... and Left=....

It is just a starting-point because if I get the way in which makeindex works right, it does not take into account the case of an index-entry not specifying a list of pages like 1, 7, 15 but specifying a range of pages like 7-9. (If you need a more sophisticated mechanism you are probably better off not attempting to accomplish it on TeX-macro-level but by means of an index-style-file tailored to your needs.)

\Autodestinationindex internally uses the counter-macro \countallindexentries for creating a named destination which in relation to the reference-point of the leftmost item of the phrase for the index-entry in the text is shifted upwards by Up=... and is shifted to the left by Left=....
\Autodestinationindex also creates an index-entry for the phrase for the index-entry where the page-number forms a hyperlink to the destination.

If the optional argument is not specified, hyperref's defaults for \Hy@raisedlink are used.

enter image description here

Compile with an engine based on pdfTeX, then run makeindex on the resulting .idx-file, then compile again.

View the resulting pdf-file with a large zoom-factor to see both the horizontal and the vertical scrolling-behavior depending on Up=... and Left=... after clicking the link in an index-entry.

The \fboxes/framed boxes can be omitted as they are only for easing up visualizing the scrolling behavior: The bottom left corner of the \fbox is close to the reference-point of the leftmost item of the textual phrase, thus you can more easily judge where the named destination/target is placed in relation to that reference-point.

The phrase forming the index-entry should not start with a break-point as otherwise a break might occur between the (invisible) point in the pdf-file forming the named destination which the pdf-viewer will scroll to the top-left-corner of the window where the pdf-file is displayed and the leftmost item of the phrase that shall be seen in the top-left corner of the window where the pdf-file is displayed when clicking the hyperlink.

\documentclass{article}
%\usepackage[hyperindex=false]{hyperref}
\usepackage{hyperref}
\usepackage{makeidx}

\makeatletter
\newcommand*\countallindexentries{1}
\newcommand\globalstepcountallindexentries{%
  \xdef\countallindexentries{\number\numexpr\countallindexentries+1\relax}%
}%
\NewDocumentCommand\CreateAutodestinationindexentry{mmm}{%
  % #1 name of destination/target
  % #2 phrase forming the index-entry
  % #3 key-val-list for shifting the destination/target up/left
  \RaisedHypertarget{#1}{#3}#2\index{#2|hyperlinkwithhyperpageneutralized{#1}}%
}%
\newcommand\hyperlinkwithhyperpageneutralized[2]{%
  {\let\HyOrg@hyperpage=\@firstofone\hyperlink{#1}{#2}}%
}%
\ExplSyntaxOn
% key=value-interface by means of package l3keys:
%-------------------------------------------------------
% Macros holding default-values of keys
%   \cs_new:Nn here instead of Up.initialize etc
%   within \keys_define:nn in order to get an error-message
%   in case of overriding/redefining an already existing
%   control sequence.
\cs_new:Nn \__COUNTALLINDEXENTRIES_MOVEDESTINATIONUP: {\q_no_value}
\cs_new:Nn \__COUNTALLINDEXENTRIES_MOVEDESTINATIONLEFT: {\q_no_value}
\keys_define:nn { COUNTALLINDEXENTRIES } {
  Up.cs_set:Np =  \__COUNTALLINDEXENTRIES_MOVEDESTINATIONUP: {},
  Up.default:n = \q_no_value,
  Left.cs_set:Np =  \__COUNTALLINDEXENTRIES_MOVEDESTINATIONLEFT: {},
  Left.default:n = \q_no_value,
}
\cs_new:Npn \RaisedHypertarget #1#2 {%
  % #1 name of destination/target
  % #2 keyval-list 
  \group_begin:
  \keys_set:nn{ COUNTALLINDEXENTRIES }{#2}
  \exp_args:No 
  \quark_if_no_value:nTF{\__COUNTALLINDEXENTRIES_MOVEDESTINATIONUP:}
                        {}
                        {
                           \exp_args:NNo
                           \cs_set:Npn \HyperRaiseLinkDefault{\__COUNTALLINDEXENTRIES_MOVEDESTINATIONUP:}
                        }
  \exp_args:No 
  \quark_if_no_value:nTF{\__COUNTALLINDEXENTRIES_MOVEDESTINATIONLEFT:}
                        {\Hy@raisedlink{\hypertarget{#1}{}}}
                        {
                          \Hy@raisedlink{
                             \kern\dimexpr-\__COUNTALLINDEXENTRIES_MOVEDESTINATIONLEFT:\relax
                             \hypertarget{#1}{}
                             \kern\dimexpr\__COUNTALLINDEXENTRIES_MOVEDESTINATIONLEFT: \relax
                          }
                        }
  \group_end:
}
\NewDocumentCommand\Autodestinationindex{O{}m}{
  % #1 optional key-val-list for shifting the destination/target up/left
  % #2 phrase forming the index-entry
  \token_if_macro:NTF \hypertarget 
                      {
                        \legacy_if:nTF {Hy@hyperindex} 
                                       {
                                         \exp_args:Nx
                                         \CreateAutodestinationindexentry{IndexEntry\countallindexentries}{#2}{#1}
                                         \globalstepcountallindexentries
                                       } 
                      }
                      { \use:n }
                      {
                        #2\index{#2}
                      }
}
\ExplSyntaxOff
\makeatother

\makeindex

\begin{document}

Some text Some text Some text

\vspace{2cm}

In probability theory and statistics, the
\fbox{\Autodestinationindex{Laplace distribution}} is a continuous
probability distribution named after Pierre-Simon Laplace. 

\vspace{2cm}

In
\fbox{%
  \Autodestinationindex[Up=\dimexpr\baselineskip+\fboxrule+\fboxsep\relax,
                        Left=\dimexpr\fboxrule+\fboxsep\relax]{probability theory}%
} and statistics, the Laplace distribution
is a continuous probability distribution named after
Pierre-Simon Laplace. 

\vspace{2cm}

In probability theory and statistics, the Laplace distribution
is a continuous probability distribution named after
\fbox{\Autodestinationindex[Up=2cm, Left=1cm]{Pierre-Simon Laplace}}. 

\printindex
\end{document}

enter image description here

2
  • Thanks a lot! I will try your solution. In the meanwhile, I changed the color of the indexed keyword (in the text) to a nice dark orange (I use red for other stuff such as theorem or book referencing, and blue for external links), and it helps. Of course your solution is much better: indexed terms, in my article, at this moment can be orange (default) or blue (if it's within an external link) or black (if it is in a section title). Dec 17, 2021 at 14:01
  • (continued) At least the term is noticeable even though you still need to scroll down a bit to see it. Your solution will fix this chaotic coloring problem and answer my original question. Of course I still have to think how to best index a same term multiple times. I like the text you chose for your illustration! Dec 17, 2021 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .