2

I am trying to write a macro that creates hyperlinks between words in the MainText and corresponding entries in the Index, so that one can jump from word-in-text to entry-in-index by clicking on the former, and back by clicking on the page number in the index. This is accomplished by the MWE given below. However, in the resulting index, grouped entries (all words beginning with a, or all beginning with b) are then treated as units that do not spill over to the next index column. Instead, the line spacing within a given group is stretched so that the a-group fills the page to the bottom, and the b-group starts in the next column. This leads to awkward-looking line spacings, illustrated below the MWE. How can one get the b-group to split across two columns? (I should add that I need to stick to the NewDocumentCommand functionality of the xparse package, since my actual code is much more complex, with \xindex having numerous optional arguments.)

 \documentclass{book}                 
 \usepackage{xparse}     
 \usepackage{makeidx}
 \makeindex

 \NewDocumentCommand{\xindex}{m}% 
 {\protect\hyperlink{ind:#1}{#1}%             write #1 to MainText
 \index{#1@\protect\hypertarget{ind:#1}{#1}}% write #1 to Index
 }%

 \usepackage[hyperindex]{hyperref}
 \hypersetup{linktocpage=true,colorlinks}
 \begin{document}

 \xindex{aa}
 \xindex{ab}
 \xindex{ab}
 \xindex{ac}
 \xindex{ad}
 \xindex{af}
 \xindex{af} 
 \xindex{ag}
 \xindex{ah}
 \xindex{ai}
 \xindex{aj}
 \xindex{ak}
 \xindex{al}
 \xindex{am}
 \xindex{an}
 \xindex{ao}
 \xindex{ap}
 \xindex{aq}
 \xindex{ar}
 \xindex{as}
 \xindex{at}
 \xindex{au}
 \xindex{av}
 \xindex{aw}
 \xindex{ay}
 \xindex{az}

 \xindex{ba}
 \xindex{bb}
 \xindex{bc}
 \xindex{bd}
 \xindex{be}
 \xindex{bb}
 \xindex{bb}
 \xindex{bb}
 \xindex{bc}
 \xindex{bd}
 \xindex{bf}
 \xindex{bf}
 \xindex{bg}
 \xindex{bh}
 \xindex{bi}
 \xindex{bj}
 \xindex{bk}
 \xindex{bl}
 \xindex{bm}
 \xindex{bn}
 \xindex{bo}
 \xindex{bp}
 \xindex{bq}
 \xindex{br}
 \xindex{bs}
 \xindex{bt}
 \xindex{bu}
 \xindex{bv}
 \xindex{bw}
 \xindex{by}
 \xindex{bz}

 \printindex
 \end{document}

The first picture below gives the output of the above code for the Index, with working hyperlinks, but awkward line spacings for the a-grou. The second picture gives the output obtained by replacing all \xindex{} commands by \index, leading to proper line spacings, but no hyperlinks. I'd like to get a hyperlinked Index, but formatted as for the second case.

First picture

Second picture

4

You need

 \NewDocumentCommand{\xindex}{m}% 
 {\protect\hyperlink{ind:#1}{#1}%             write #1 to MainText
 \index{#1@\protect\leavevmode\protect\hypertarget{ind:#1}{#1}}% write #1 to Index
 }%
| improve this answer | |
  • That indeed works perfectly, thank you so much! \leavevmode was outside my horizon, but now I understand what it does here. – jvd May 15 '18 at 5:54

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.