# Problems with multiple instances of see or seealso in index

Suspect I'm missing something obvious here. I have multiple identical instances of see or seealso index entries. This cannot be avoided.

Sometimes they are appropriately compacted, but other times they are not. I am using imakeidx, but the problem is identical with makeidx, and is also resistant to deleting temporary files and the number of runs. It manifests in various ways, and it is hard to produce a single MWE that shows all of the effects.

However this MWE:

\documentclass{article}
%\usepackage{imakeidx}
%\indexsetup{othercode=\footnotesize}
%\makeindex[intoc=true,title=My Index,columnsep=25pt]

\usepackage{makeidx}
\makeindex

\begin{document}
text\index{cat}text\index{cat}
text\index{dog}text\index{dog}
text\index{cat|see{pig}}text\index{cat|see{pig}}
text\index{dog|see{pig}}text\index{dog|see{pig}}
text\index{Smith John|see{Smith Jack}} text\index{Smith John|see{Smith Jack}}
text\index{Blundell!Jack|see{Blundell John}} text\index{Blundell!Jack|see{Blundell John}}
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}\index{Fuchs!Annie|see{Blogs Annie}}
\clearpage
text\index{cat}text\index{cat}
text\index{dog}text\index{dog}
text\index{cat|see{pig}}text\index{cat|see{pig}}
text\index{dog|see{pig}}text\index{dog|see{pig}}
text\index{Smith John|see{Smith Jack}}text\index{Smith John|see{Smith Jack}}
text\index{Blundell!Jack|see{Blundell John}} text\index{Blundell!Jack|see{Blundell John}}
text\index{Blundell!Peter} text\index{Blundell!Peter}
text\index{Blundell!Aubrey} text\index{Blundell!Aubrey}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}\index{Fuchs!Annie|see{Blogs Annie}}
\clearpage
text\index{cat}text\index{cat}
text\index{cat|see{pig}}text\index{cat|see{pig}}
text\index{dog|see{pig}}text\index{dog|see{pig}}
text\index{Smith John|see{Smith Jack}}text\index{Smith John|see{Smith Jack}}
text\index{Blundell!Aubrey} text\index{Blundell!Aubrey}
text\index{Blundell!Jack|see{Blundell John}} text\index{Blundell!Jack|see{Blundell John}}
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}\index{Fuchs!Annie|see{Blogs Annie}}
\clearpage
text\index{cat}text\index{cat}
text\index{cat|see{pig}}text\index{cat|see{pig}}
text\index{dog|see{pig}}text\index{dog|see{pig}}
text\index{Blundell!Aubrey} text\index{Blundell!Aubrey}
text\index{Smith John|see{Smith Jack}}text\index{Smith John|see{Smith Jack}}
text\index{Blundell!Jack|see{Blundell John}} text\index{Blundell!Jack|see{Blundell John}}
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}\index{Fuchs!Annie|see{Blogs Annie}}
\clearpage
text\index{cat}text\index{cat}
text\index{cat|see{pig}}text\index{cat|see{pig}}
text\index{dog|see{pig}}text\index{dog|see{pig}}
text\index{Blundell!Aubrey} text\index{Blundell!Aubrey}
text\index{Smith John|see{Smith Jack}}text\index{Smith John|see{Smith Jack}}
text\index{Blundell!Jack|see{Blundell John}} text\index{Blundell!Jack|see{Blundell John}}
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}\index{Fuchs!Annie|see{Blogs Annie}}
\printindex
\end{document}


Produces

As seen, the see is replicated for cat and also for dog (but only partially for the latter where there are intervening actual index entries). Actual index entries for the same item are not always necessary to produce the effect, for example I see things like the below in a larger document, but not in the MWE:

What am I doing wrong?

• As far as I know, the see entry should appear only once. – egreg Aug 7 '15 at 13:43
• But usually when there are multiple instances it IS compacted (as seen in the MWE). In my case it is not possible for each see too appear only once (there are multiple chapters not all of which appear in the final document -- and collecting all see's uniquely in one place will result in some see index items that refer to items that do not exist) – Aubrey Blumsohn Aug 7 '15 at 13:47
• In my opinion, doing \index{cat}\index{cat|see{dog}} doesn't make sense; it should be seealso. And index{cat|seealso{dog}} should appear just once. – egreg Aug 7 '15 at 13:48
• Yes that is correct - but not the issue here as seealso is variably compacted and sometimes not compacted in the same way (in the above MWE you just get replicated displays of see also instead of see...). I have not managed to work out when it gets compacted and when it does not. – Aubrey Blumsohn Aug 7 '15 at 13:54
• @egreg Makeindex is agnostic to see. There is no difference to textbf or other page formatting commands for Makeindex. – Heiko Oberdiek Aug 7 '15 at 13:56

The see feature is nothing special, it's just a page formatting command for Makeindex, from the generated .ind file:

\item dog, 1, \see{pig}{1}, 2, \see{pig}{2--5}

\item Smith John, \see{Smith Jack}{1--5}


It is only accidental that the see in the entry for "Smith John" appears only once. The reason is that the entries are merged to a page range. The pages are not visible in the output, because macro \see throws the second argument for the page away.

The issue can be fixed by post-processing the .idx file. The following Perl filter script replaces the page number of each see index entry by $seepagenumber with default 9999, thus that the see entry is added at the end of the page list (increase 9999, if you have more pages). The perl script fix-see.pl acts as filter. That means, it reads from standard input and writes to standard output: #!/usr/bin/env perl use strict; use warnings; my$seepagenumber = 9999;

while (<>) {
s/{[^{}*]}$/{$seepagenumber}/ if /\|see{/;
print;
}
__END__


The command sequence is then for file test.tex exemparily:

pdflatex test
./fix-see.pl <test.idx >test.idx-fixed
makeindex test.idx-fixed
pdflatex test


The result:

## Solution in LaTeX for package makeindex

The Perl script can be avoided by changing the page number for see entries in TeX, when it writes the index entry. Example for the standard definition of the index writing command (latex.ltx/package makeidx):

\usepackage{makeidx}
\makeindex

\newcommand*{\seepagenumber}{9999}

\makeatletter
\CheckCommand*{\@wrindex}[1]{%
\protected@write\@indexfile{}{%
\string\indexentry{#1}{\thepage}%
}%
\endgroup
\@esphack
}
\renewcommand*{\@wrindex}[1]{%
\protected@edef\idx@text{#1}%
\expandafter\idx@test@see\idx@text|see\@nil{#1}%
}
\def\idx@test@see#1|see#2\@nil#3{%
\protected@write\@indexfile{%
\ifx\\#2\\%
\else
\let\thepage\seepagenumber
\fi
}{%
\string\indexentry{#3}{\thepage}%
}%
\endgroup
\@esphack
}
\makeatother


## Version for package imakeidx

\usepackage{imakeidx}
\indexsetup{othercode=\footnotesize}
\makeindex[intoc=true,title=My Index,columnsep=25pt]

\newcommand*{\seepagenumber}{9999}

\makeatletter
\CheckCommand\imki@wrindexentrysplit[3]{%
\expandafter\protected@write\csname#1@idxfile\endcsname{}%
{\string\indexentry{#2}{#3}}%
}
\CheckCommand\imki@wrindexentryunique[3]{%
\protected@write\@indexfile{}%
{\string\indexentry[#1]{#2}{#3}}%
}

\newif\if@IndexEntryWithSee
\renewcommand\imki@wrindexentrysplit[3]{%
\@DoesEntryContainsSee{#2}%
\expandafter\protected@write\csname#1@idxfile\endcsname{%
\if@IndexEntryWithSee
\let\thepage\seepagenumber
\fi
}{%
\string\indexentry{#2}{#3}%
}%
}
\renewcommand\imki@wrindexentryunique[3]{%
\@DoesEntryContainsSee{#2}%
\protected@write\@indexfile{%
\if@IndexEntryWithSee
\let\thepage\seepagenumber
\fi
}{%
\string\indexentry[#1]{#2}{#3}%
}%
}
\newcommand*{\@DoesEntryContainsSee}[1]{%
\protected@edef\@IndexEntryText{#1}%
\expandafter\@CheckForSee\@IndexEntryText|see\@nil
}
\def\@CheckForSee#1|see#2\@nil{%
\ifx\\#2\\%
\@IndexEntryWithSeefalse
\else
\@IndexEntryWithSeetrue
\fi
}
\makeatother


• This looks to be a workable solution Heiko, but going back to the cause, I cannot follow when you say that "It is only accidental that the see in the entry for "Smith John" appears only once -- because the entries are merged to a page range. The Smith John see actually does appear on every page, and should not therefore be compacted if this is the reason. – Aubrey Blumsohn Aug 7 '15 at 13:59
• @AubreyBlumsohn "accidental" here means, it depends on the page numbers and configuration, which gets merged to a page range. The entry for "Smith John" appears on all pages 1 to 5, therefore makeindex merges them to the page range "1--5", see the .ind file. – Heiko Oberdiek Aug 7 '15 at 14:01
• Ah, I see... No wonder I was having such problems tying the problem down. I am going to accept your answer over egreg despite the fact that both produce excellent diagnoses. It seems to me that the solution cannot be to be forced to collect all see's in one place and to de-duplicate them, because this is not always feasible (it makes adding or subtracting text difficult without ending up with orphan sees that point to nonexistent entries, and makes to hard to add or subtract whole chapters without a complex editing exercise on the see-list). – Aubrey Blumsohn Aug 7 '15 at 14:08

Entries with see or seealso should appear only once and at the end of the document, just before \printindex:

\documentclass{article}
%\usepackage{imakeidx}
%\indexsetup{othercode=\footnotesize}
%\makeindex[intoc=true,title=My Index,columnsep=25pt]

\usepackage{makeidx}
\makeindex

\begin{document}

text\index{cat}text\index{cat}
text\index{dog}text\index{dog}
text
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}
\clearpage

text\index{cat}text\index{cat}
text\index{dog}text\index{dog}
text
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}
\clearpage

text\index{cat}text\index{cat}
text\index{dog}text\index{dog}
text
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}
\clearpage

text\index{cat}text\index{cat}
text\index{dog}text\index{dog}
text
text\index{Blundell!Peter} text\index{Blundell!Peter}
\index{Blogs!Peter!results}\index{Blogs!Annie!results}
\clearpage

\index{cat|seealso{pig}}
\index{dog|seealso{pig}}
\index{Smith John|see{Smith Jack}}
\index{Fuchs!Annie|see{Blogs Annie}}
\index{Blundell!Jack|see{Blundell John}}

\printindex
\end{document}