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 ?

Guliup
  • 96
  • 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. – Peter Wilson Apr 24 '20 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 '20 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 '20 at 08: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) – LaTeX Ninja Apr 27 '20 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 '20 at 13:01
  • with your current config, you apparently can only have the same Index-Tag max. twice, it works if you switch it out after than 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 5 which is the current latest page

    – LaTeX Ninja Apr 27 '20 at 13:19
  • @SLang I have tried what you suggested, and you are right I get 4. Does it mean the issue is even more complicated? – Guliup Apr 27 '20 at 13:26
  • Looked at this yet? Add \newcounter{NewCounter} \makeatletter \newcommand{\Indextest}[1]{\imki@wrindexentry{MyIndex}{#1}{\theNewCounter}} \makeatother \makeindex[name=MyIndex] before \begin{document} and this after your \printindex: ```\stepcounter{NewCounter}\chapter{xx}\Indextest{xx} \arabic{NewCounter}

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

    \printindex[MyIndex]``` - seems to be working to me (although not sure if it's the desired behaviour)

    – LaTeX Ninja Apr 27 '20 at 13:41
  • It almost works perfectly ! Thank you so much ! I say almost because a strange vertical line has appeared between the two columns of my index. Any idea why ? I have used makeindex option columns : \makeindex[name=MyIndex, columns=2, options= -s tablealphab.ist] – Guliup Apr 27 '20 at 14:12
  • Edit : forget about my last comment, it was my mistake. Thanks again ! – Guliup Apr 27 '20 at 14:46
  • I added the solution as an answer ;) would be happy to get an upvote ;) – LaTeX Ninja Apr 27 '20 at 17:20
  • @Guliup Fair enough. memoir provides its own methods for the packages you mentioned. – Peter Wilson Apr 27 '20 at 18:05

1 Answers1

3

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