0

I am trying to rearrange @Alan Munn answer to place semicolon before "see also" reference in the index. But no result is visible. What I am doing wrong?

Code:

\documentclass{article}

\begin{filecontents}{\jobname.mst}
    delim_0 "\\idxdelim"
    delim_1 "\\idxdelim"
    delim_2 "\\idxdelim"
\end{filecontents}
\usepackage{imakeidx}
\makeindex
\makeatletter
\def\idxdelim{\@ifnextchar{\see}{\space}{\@ifnextchar{\seealso}{;\space}{\space}}}
\makeatother


\begin{document}
Some words. A test.
\index{words}
\index{phrases|see{words}}
\newpage
Words and sentences.\index{words}\index{sentences}
\newpage
Sentences.\index{sentences}

\index{sentences|seealso{words}}    
\printindex
\end{document}

enter image description here

2 Answers 2

2

In the .mst file,

  • delim_0 refers to the delimiter between the 0-th level index entry and its first page number.
  • delim_1 refers to the same for the 1st level index entry
  • delim_2 refers to the same for the 2nd level index entry

This means that your code explicitly does not configure what happens between the different "page numbers" associated to each index entry. What you want, I think, is to set the key delim_n instead; this controls the delimiters between two page numbers.


The following code should satisfy the requirements of this comment.

\documentclass{article}

\begin{filecontents}{\jobname.mst}
        delim_0 "\\space"
        delim_1 "\\space"
        delim_2 "\\space"
        delim_n "\\idxBdelim"
\end{filecontents}
\usepackage{imakeidx}
\makeindex
\makeatletter
\def\idxBdelim{\@ifnextchar{\seealso}{;\space}{,\space}}
\makeatother


\begin{document}
Some words. A test.
\index{words}
\index{phrases|see{words}}
\newpage
Words and sentences.\index{words}\index{sentences}
\newpage
Sentences.\index{sentences}

\index{sentences|seealso{words}}
\printindex
\end{document}
5
  • In accordance with the editor's guides which I have the semicolon should be before "see also". Before "see" should be only the space. Between numbers of pages should be commas (it is OK). Between the referred word and the first page number should be only the space.
    – forrest
    Feb 22, 2021 at 7:17
  • @forrest: to give you a complete solution, I need to know the following cases. (1) What should * be in Key 1, 2 * see Key2; if the see comes after a list of pages? (2) what should * be in Key * see also Key 2; if the "see also" comes without any pages. Are you just assuming that these two cases will not happen in practice? Feb 22, 2021 at 13:19
  • Willie Wong, in my use only "see also" reference is followed by page number(s). "see" reference can be immediately after the other entry or subentry. But I wonder how to keep your solution working while hyperref package is called...
    – forrest
    Feb 24, 2021 at 10:29
  • 1
    Yeah, I don't see how it could possibly work with hyperref. (a) the package makes certain implicit assumptions on what is the separator between pages and (b) you would need to also unwrap \hyperindexformat to see the \see or \seealso. If it were just problem (b) this could be solved with a bit more work. But I don't know how to solve (a). Feb 24, 2021 at 17:36
  • 1
    I asked a new question specifically about point (a) in my previous comment. Feb 24, 2021 at 17:48
2

The OP asked also asked about hyperref. I can make it work in a test case, thanks to this answer, but I worry very much potential side effects. Use it in a production document at your own risk!

\documentclass{article}

\begin{filecontents}{\jobname.mst}
        delim_0 "\\space"
        delim_1 "\\space"
        delim_2 "\\space"
        delim_n "\\idxBdelim"
\end{filecontents}
\usepackage{imakeidx}
\usepackage{hyperref}
\makeindex
\makeatletter
\def\idxBdelim{\@ifnextchar{\hyperindexformat}{;\space}{,\space}}
\def\@commahyperpage#1{\@@commahyperpage#1\idxBdelim{}\idxBdelim{}\\}
\def\@@commahyperpage#1\idxBdelim#2\idxBdelim#3\\{%
  \ifx\\#2\\%
    \HyInd@pagelink{#1}%
  \else
    \HyInd@pagelink{#1}\idxBdelim\HyInd@pagelink{#2}%
  \fi
}
\makeatother

\begin{document}
Some words. A test.
\index{words}
\index{phrases|see{words}}
\newpage
Words and sentences.\index{words}\index{sentences}
\newpage
Sentences.\index{sentences}

\index{sentences|seealso{words}}


\printindex
\end{document}

Possible side-effects:

  • This essentially blows up how \hyperpage works with comma-separated list of elements.
  • I cannot detect \seealso; I only detect the wrapper that \hyperref puts around \seealso; but the same wrapper also gets placed around \see, so use it at your own peril.

A potentially better solution would be to try to fully expand what (say) 1\idxBdelim2 would expand to before passing it into \hyperpage; this would not require modifying the underlying mechanics of \hyperpage. Unfortunately I couldn't make it work.

You must log in to answer this question.

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