I wish to create an index scheme the entries of which I can print each separately at different locations, so as to allow for all sorts of text bits and commentaries in between entries, or to allow for an entry to appear within such a commentary, between parentheses for instance. (Hence what I'm after differs from an index with several data per entry, as in the question "Index with manually-added additional data".)
So I can't (as far as I could tell!) use the usual indexing tools, which do not allow such “spreading”.
I had written the following three commands, one for the first appearance of an indexed entry, one for the subsequent occurrences, one to print the index, the logic of which is explained in the commented out lines.
The problem I run into is illustrated below: \thepage sometimes gives a wrong value (that of the page at the beginning of the paragraph where it appears, and not of the actual current page where it appears itself), resulting in some index entries missing when they appear in a paragraph spread over several pages.
\documentclass[oneside,10pt]{article}
\usepackage{ifthen}
\newcommand\IDx[1]
{%
\newcounter{#1}%
%Not yet used: will be used in \INDEX (see explanation below)
\newcounter{#1page}%
%this counter is redefined with the index is incremented, so as to allow for a comparison of the indexed page with subsequent entries
%As this is the first entry, I redefine it:
\setcounter{#1page}{\thepage}%
%Then this counter:
\newcounter{#1index}%
%registers the number of pages that have been indexed,
%which we will need later on, in \INDEX, in order to know how many pages to look for
%As this is the first entry, we increment it:
\refstepcounter{#1index}%
%We put there a unique label which \INDEX will then look for to get the current page number
\label {#1=\csname the#1index\endcsname}%
}%
\newcommand\IDX[1]
{%
%Then we compare the value that #1page has had at the last point of index with the value of the current page
%If they are the same, we do nothing: the page is already indexed.
%If they differ, we repeat the scheme of above
\ifthenelse {%
\equal {\csname the#1page\endcsname}
{\thepage}%
}%
{}
{%
\refstepcounter{#1index}%
\label {#1=\csname the#1index\endcsname}%
\setcounter{#1page}{\thepage}%
}%
}%
\newcommand\INDEX[1]
{%
%We can now print the index:
%To avoid an unwanted coma before the first entry, we print this one first entry separately:
\stepcounter{#1}%
\pageref{#1=\csname the#1\endcsname}%
%Then we run the following as many times as required, i.e. till #1 has reached, here, the value that #1index has reached throughout the document
\whiledo{\NOT\value{#1}=\value{#1index}}%
{%
\stepcounter{#1}%
,
\pageref{#1=\csname the#1\endcsname}%
}%
\smallskip
}
\begin {document}
\IDx {Try}
Text
\newpage
%This paragraph to appear on two pages to illustrate the problem:
\vspace*{43\baselineskip}%
\IDX {Try}
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
Text textText text
\IDX {Try}
\newpage
\IDX {Try}
Text
Now I print the index for ‘Try’:
\INDEX{Try}
\end {document}
In this example, the entry on page 3 is not indexed, because the command ‘thepage’ takes the value of the page at the beginning of the paragraph where it is used (i.e, here, page 2), and not, as one might expect, the value of the page where it is printed (page 3). So my ‘ifthenelse’ check cannot operate as I mean it to on page 3.
So my question is: what can I remplace \thepage with so as to prevent this? Labels seem at first glance to be the solution, but I cannot define (yet) another counter with my argument #1, create (yet) another label with it for each occurrence and use something like \pageref{#1-\the#1othercounter}: such a reference won't work.
\newcounter
within the macro, there are a finite number of these and you are consuming one each time, You can not access\thepage
within main document code you need to use a non-\immediate
\write
so the value gets written after the page break is chosen. I suspect you could use an existing index macro but I could not follow your description exactly.\immediate\write
, but I'll look into it! Thanks a lot!\IDX{Try}\IDX{Try}\IDX{Try}
and allocate three separateTry
counters, the first two are lost and ove-written by the third, but not unallocated. classic tex only has 256 count register so you would have soon run out, etex has more but still conceptually a finite resource\protected@write
will defer expanding\thepage
. One can use it to defer other counters (mycount) by putting{\let\themycount=\relax}
in the normally empty argument.