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


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





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


You need

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