3

For a given work, I have a continuous numerotation throughout all the document using a counter. I need the index entries refer to the paragraph numbers and not to the pages. This is the same issue than this thread, except I am not using memoir but book. Unfortunately, the solution presented does not work anymore switching from one document class to an other.

I have been looking at this other thread. The solution looks really good, except it displays the counter's title instead of the counter's number.

Here is the code :

\begin{filecontents*}{raphink.ist}
delim_0 ""
\end{filecontents*}
\documentclass[a4paper]{report}
\usepackage{etoolbox}
\newcounter{subpara}
\newcommand\numsubpara[1]{%
  \stepcounter{subpara}%
  \expandafter\gdef\csname cursec\thesubpara\endcsname{#1}%
  \thesubpara. \textbf{#1}}

\usepackage{imakeidx}
\makeindex[options=-s raphink]

\makeatletter
\let\@Index\@index
\patchcmd{\@Index}{\@wrindex}{\@Wrindex}{}{}
\let\@Wrindex\@wrindex
\patchcmd{\@Wrindex}{\thepage}{\thesubpara}{}{}
\let\xIndex\index
\patchcmd{\xIndex}{\@index}{\@Index}{}{}
\patchcmd{\xIndex}{\@index}{\@Index}{}{}
\let\protected@iwrite\protected@write
\patchcmd{\protected@iwrite}{\write}{\immediate\write}{}{}
\patchcmd{\@Wrindex}{\protected@write}{\protected@iwrite}{}{}
\makeatother

\newcommand{\Index}[1]{\xIndex{#1|transform}}
\newcommand{\transform}[1]{\forcsvlist\decodesec{#1}}
\newcommand{\decodesec}[1]{, \csname cursec#1\endcsname}

\begin{document}

\chapter{Test1}

\numsubpara{title1}

\Index{Test}

\chapter{Test2}

\numsubpara{title2}
\Index{Test}


\printindex

\end{document}

And the result :

enter image description here

If I have well understood, I would have to switch this :

\newcommand\numsubpara[1]{%
  \stepcounter{subpara}%
  \expandafter\gdef\csname cursec\thesubpara\endcsname{#1}%
  \thesubpara. \textbf{#1}}

For that :

\newcommand\numsubpara[1]{%
  \stepcounter{subpara}%
  \expandafter\gdef\csname cursec\thesubpara\endcsname{\thesubpara}%
  \thesubpara. \textbf{#1}}

But when I do this, I get this result :

enter image description here

Instead of "Test, 1,2".

Does anyone know how to have the index entries refer to the paragraph numbers and not to the pages ?

12
  • Why not try using memoir as it encompasses all that book does, and much more? Just replace book with memoir and perhaps change some of the options in your \documentclass command. Apr 24, 2020 at 16:56
  • Hello @PeterWilson ! It is a solution indeed... but I have worked a lot on this template, and I would mean doing again a lot of other stuff...
    – Guliup
    Apr 24, 2020 at 17:02
  • @PeterWilson : I have checked using memoir : a lot of different formatting do not work anymore. Here are some examples : I have used \usepackage[newparttoc]{titlesec} to format \part title in my toc, doesn't work anymore; I have used \usepackage[cam,width=17.5truecm,height=25.54truecm,center,dvips,noinfo]{crop} for printing marks, doesn't work anymore; \setstretch{1.1}; etc... A solution working with book would be really appreciated ! Thanks :)
    – Guliup
    Apr 27, 2020 at 8:12
  • I have not looked at it in uttermost detail but it seems to me that \thesubpara always refers to the very last value the counter takes. If you make a test with 4 indexed subparas rather than two in your examples, you always get values of 4, indicating that \thesubpara value in your index refers to the variable at its 'last state' in the document, not at the value it had in the place where it was set (which is what you want to get) - so it does not refer to the pages in you current example. If that's what you wanted to know (not sure) Apr 27, 2020 at 12:42
  • @SLang, I didn't know \thesubpara was working this way, thank you very much. It explains why it is not working... Do you have any idea how to build something working? By the way, I have tried making a test with 4 indexed subpara, and strangely it displays nothing..
    – Guliup
    Apr 27, 2020 at 13:01

1 Answer 1

3
+50

Understanding the value outputted by \thesubpara

In your example, \thesubpara always refers to the very last value the counter takes. If you make a test with 4 indexed \subparas rather than two in your examples, you always get values of 4, indicating that \thesubpara value in your index refers to the variable at its 'last state' in the document, not at the value it had in the place where it was set (which is what you want to get) - so it does not refer to the pages in your current example (which, it seems, is what you thought initially).

By the way

With your current config, you can only have the same \Index tag max. twice, so it only works if you switch it out after that, like like \Index{Bla} - for example, if you add

\newpage bla \newpage bla 

\chapter{Test3} 
\numsubpara{title3} 
\Index{Bla} 

\chapter{Test4} 
\numsubpara{title4} 
\Index{Bla} 

to your document, you can see that even with the \newpages, you still get the value 4, not 6 which is the current last page.

A quick fix solution

Have you looked at this (question: Using different counter for Index (makeidx)) yet? It seems to provide the wanted output. Add:

\newcounter{NewCounter}

\makeatletter
\newcommand{\Indextest}[1]{\imki@wrindexentry{MyIndex}{#1}{\theNewCounter}}
\makeatother

\makeindex[name=MyIndex]

before \begin{document} and this after the \printindex of your current example, add (for testing):

\stepcounter{NewCounter}\chapter{Test}\Indextest{Test}
\arabic{NewCounter}

\stepcounter{NewCounter}\chapter{Test}\Indextest{Test}
\arabic{NewCounter}

\newpage 

\stepcounter{NewCounter}\chapter{Test}\Indextest{blurb}
\arabic{NewCounter}

\stepcounter{NewCounter}\chapter{Test}\Indextest{Test}
\arabic{NewCounter}

\printindex[MyIndex]

This should produce the desired behaviour, like so:

the resulting output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .