# Self-referencing index entry

I want to generate an index that entry that points ONLY to itself, not any other place in the text. That is, I want a command \phoneyindex{Smith, John} which when put in the preamble generates an index entry to John Smith pointing ONLY to the page in the index on which it occurs.

Any ideas?

The question is quite Knuthian! Index entries may be written in the form

 \index{John Smith|cmd}


where \cmd is a command that is applied to the page number value. A first approach would be to use

 \newcommand{\mypageout}[1]{\thepage}


and issue

 \index{John Smith|mypageout}


somewhere in your document so the printed page number x is replaced by \mypageout{x}, which is just current output of \thepage. However, as you point out, at page boundaries \thepage is often off by 1, due to the way page building works. Alternatively, you can use the \label/\pageref mechanism as follows: define a \mypageout macro that discards its second argument and makes a label from the first, and use this with a first argument in the command part of the \index

\newcommand{\phoneyindex}[2]{\index{#1|mypageout{#2}}}
\newcommand{\mypageout}[2]{\label{#1}\pageref{#1}}


You then call this with \phoneyindex{John Smith}{phoneyj}, where the phoneyj is the label you wish to use. This is now correct on entries just after page boundaries.

Page 5

Page 6

\documentclass{article}

\usepackage{lipsum}
\usepackage{makeidx}
\makeindex

\newcommand{\phoneyindex}[2]{\index{#1|mypageout{#2}}}
\newcommand{\mypageout}[2]{\label{#1}\pageref{#1}}

\begin{document}

\phoneyindex{Allan Smith}{phoneya}
\phoneyindex{John Smith}{phoneyj}

\lipsum[1-20]
\index{Start}Start.

\lipsum[21]
\index{F}\index{G}\index{H}\index{I}\index{J}\index{B}\index{L}\index{Ba}\index{Ca}\index{Da}\index{Ea}\index{Fa}\index{Ga}\index{Ha}\index{Ia}\index{Ja}\index{Ba}\index{La}\index{Baa}\index{Caa}\index{Daa}\index{Eaa}\index{Faa}\index{Gaa}\index{Haa}\index{Iaa}\index{Jaa}\index{Baa}\index{Laa}\index{Bab}\index{Cab}\index{Dab}\index{Eab}\index{Fab}\index{Gab}\index{Hab}\index{Iab}\index{Jab}\index{Bab}\index{Lab}\index{Bb}\index{Cb}\index{Db}\index{Eb}\index{Fb}\index{Gb}\index{Hb}\index{Ib}\index{Jb}\index{Bb}\index{Lb}\index{Bc}\index{Cc}\index{Dc}\index{Ec}\index{Fc}\index{Gc}\index{Hc}\index{Ic}\index{Jc}\index{Bc}\index{Lc}\index{Bd}\index{Cd}\index{Dd}\index{Ed}\index{Fd}\index{Gd}\index{Hd}\index{Id}\index{Jd}\index{Bd}\index{Ld}\index{Be}\index{Ce}\index{De}\index{Ee}\index{Fe}\index{Ge}\index{He}\index{Ie}\index{Je}\index{Be}\index{Le}\index{Bf}\index{Cf}\index{Df}\index{Ef}\index{Ff}\index{Gf}\index{Hf}\index{If}\index{Jf}\index{Bf}\index{Lf}

\printindex

\end{document}

• This worked perfectly in one instance, but in another (where the item was near the top of the page) the page number was off by one (it had the previous page number). Is there any way around this? May 3, 2017 at 21:29
• Indeed you are right. I have now updated my answer with a better method - the cost: you have to specify a label name for each entry. May 4, 2017 at 6:48