由于 \thepage 给出错误的页码,“手动”索引方案遇到问题

由于 \thepage 给出错误的页码,“手动”索引方案遇到问题

我希望创建一个索引方案,其中的条目可以分别打印在不同的位置,以便允许在条目之间添加各种文本和注释,或者允许条目出现在这样的注释中,例如在括号之间。(因此,我所追求的不同于每个条目包含多个数据的索引,如问题“手动添加附加数据的索引”。)

因此我不能(据我所知!)使用通常的索引工具,因为它们不允许这种“传播”。

我写了以下三个命令,一个用于索引条目的第一次出现,一个用于后续出现,一个用于打印索引,其逻辑在注释掉的行中进行了解释。

我遇到的问题如下所示:\thepage 有时会给出错误的值(页面的开始出现在段落中的,而不是实际的当前的在某些情况下,索引条目会在其本身出现的页面上丢失,这会导致一些索引条目在跨越多页的段落中出现时丢失。

\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}

在此示例中,第 3 页上的条目未被索引,因为命令“thepage”获取了第开始使用该值的段落的值(即此处的第 2 页),而不是人们可能期望的打印页的值(第 3 页)。因此,我的“ifthenelse”检查无法在第 3 页按预期运行。

所以我的问题是:我可以用什么来替换 \thepage 以防止这种情况发生?标签乍一看似乎是解决方案,但我无法用我的参数 #1 定义(还)另一个计数器,无法为每次出现创建(还)另一个标签,也无法使用类似 \pageref{#1-\the#1othercounter} 的内容:这样的引用不起作用。

相关内容