# How to send the present value of a counter to the index

The following is a simplification of the behavior of pagnote package, when you send all the notes to the end of the book.

When some name in a note has to be indexed, it's better to make the number of the paragraph or the line in the page to follow the page number.

With the naïve code below, I get three times the value of the counter at the time when printindex is called (that is, 3). I tried \expandafter and \edef, but I managed to get no result. Thanks for any help, André

\documentclass{book}
\usepackage{makeidx}
\makeindex
\begin{document}

\newcounter{np}
\setcounter{np}{0}
\def\pageline#1{page #1, line \arabic{np}}

\newenvironment{noteline}{\refstepcounter{np}\noindent\textbf{\arabic{np}.}\space}{\medskip\par}

\begin{noteline} Lion. \index{lion|pageline}\end{noteline}

\begin{noteline} Elephant. \index{elephant|pageline}\end{noteline}

\begin{noteline} Gazelle. \index{gazelle|pageline}\end{noteline}

\printindex

\end{document}


Here is a solution that supplies the expanded value of the counter np as the first argument to \pageline and the index term as the second one. The command \indexpl{term} replaces \index{term|pageline}.

Replace your definition of \pageline by the following lines:

\newcommand\pageline[2]{page #2, line #1}
\newcommand\indexpl[1]{%
\edef\indexplx{\noexpand\index{#1|pageline{\arabic{np}}}}%
\indexplx
}


Here is the complete example and its output.

\documentclass{book}
\usepackage{makeidx}
\makeindex
\newcounter{np}
\setcounter{np}{0}
\newcommand\pageline[2]{page #2, line #1}
\newcommand\indexpl[1]{%
\edef\indexplx{\noexpand\index{#1|pageline{\arabic{np}}}}%
\indexplx
}
\newenvironment{noteline}{\refstepcounter{np}\noindent\textbf{\arabic{np}.}\space}{\medskip\par}
\begin{document}
\begin{noteline} Lion. \indexpl{lion}\end{noteline}
\begin{noteline} Elephant. \indexpl{elephant}\end{noteline}
\begin{noteline} Gazelle. \indexpl{gazelle}\end{noteline}
\printindex
\end{document}