1

How does \expandafter works here? The following code needs to be completed. I assume that some of you might do this with ease, hopefully.

The intention is: to propagate the letter n (or any other, for Notes of pagenote into the index) with the pagenotenumber for \index[]{} within \pagenote{}.

The code is derived from \index inside footnote: How to propagate the footnote number correctly with index package?

\documentclass{report}

\usepackage{index}
\usepackage[texindy,splitindex]{imakeidx}
\usepackage[hidelinks]{hyperref}
\usepackage[page]{pagenote}

\newcommand\pn[2]{\hyperpage{#2}n#1}

\makeatletter
\let\if@nopnote\iffalse % just for the example
\newcommand{\is}[2]{%
    \if@nopnote
    \index[#1]{#2}%
    \else
    \expandafter\pagenoteindex\expandafter{\tempnumber}{#2}%
    \fi
}
\renewcommand{\notenuminnotes}[1]{%
    \edef\tempnumber{#1}%
    {\normalfont #1.} }
\makeatother
\newcommand{\pagenoteindex}[3]{\index[#1]{#3|pn{#2}}}

\makeindex[program=texindy,name=Names,title=Names]
\makeindex[program=texindy,name=Something,title=Something]
\makepagenote

\begin{document}
    
First index.\index[Something]{First index} 
    
Sentence.\pagenote{First pagenote without index.}
    
\clearpage
    
Emph index.\index[Something]{Emph index@\emph{Emph index}} 
    
pagenote and index.\pagenote{Second pagenote with index.\is[Something]{Index inside second pagenote}}
    
\clearpage
    
Third pagenote.\pagenote{\emph{Emph index word}.\is[Something]{Emph index@\emph{Emph index}}}
    
Namenindex\index[Names]{Namenindex}
    
\printnotes
    
\printindex[Something]
\printindex[Names]
\end{document}

This functionality seems a standard function for indexing in modern book typesetting but not yet recognized with LaTeX. It would be great if someone can support this and present a solution.

1 Answer 1

1

Not altogether sure what you want. (I vaguely remember from earlier questions, but there's not much to refresh my memory in this one.)

The following generates note and page numbers in the indices without altering the contents of the document environment. Whether that's the target behaviour, I'm not sure.

The initial problem with your code is that you have defined \pagenoteindex to require 3 arguments, but are passing it only 2. I'm guessing you want an optional argument here, because you're passing one to \is, so we need 4 cases rather than 2:

  1. we're inside a page note and have an optional argument;
  2. we're inside a page note without one;
  3. we're outside with one;
  4. we're outside without one.

expl3 makes expansion (relatively) straightforward. It is, at least, a lot less unwieldy than juggling chains of \expandafters which threaten to regress to infinity. We define 2 functions for cases (1) and (2):

\cs_new_protected:Nn \thomkrates_pagenoteindex:nnnn
{
  \index [#1] { #3 | pn {#2}{#4} }
}
\cs_new_protected:Nn \thomkrates_pagenoteindex:nnn
{
  \index  { #2 | pn {#1}{#3} }
}

If using this code, do note this is not the correct way to do this. As explained below, we're bypassing the standard mechanisms here.

Then we tell LaTeX we'd like variants which should receive a pre-expanded argument in second or first place:

\cs_generate_variant:Nn \thomkrates_pagenoteindex:nnnn { nene }
\cs_generate_variant:Nn \thomkrates_pagenoteindex:nnn { ene }

This means LaTeX will expand those arguments before passing them to our base functions, so we don't have to. So now we can define a version of \is to handle our 4 cases:

\NewDocumentCommand {\is} { om }{%
  \if@nopnote
    \IfValueTF { #1 } {
      \index[#1]{#2}
    }{
      \index{#2}
    }
  \else
    \hypertarget{pn:\tempnumber}{}%
    \IfValueTF { #1 } {
      \thomkrates_pagenoteindex:nene { #1 } { \tempnumber } { #2 } { \temppgnumber }
    }{
      \thomkrates_pagenoteindex:ene { \tempnumber } { #2 } { \temppgnumber }
    }
  \fi
}

Because I don't understand the format xindy wants (as opposed to makeindex) and couldn't find the relevant documentation (which I assume exists somewhere), I gave up trying to figure out how to get the page number to show up when \hyperindexformat is used in the .idx rather than \hyperpage.

So at this point we use a brute-force-and-ignorance approach, simply adding the page number directly and bypassing the usual mechanisms:

\renewcommand{\notenuminnotes}[1]{%
  \edef\tempnumber{#1}%
  \edef\temppgnumber{\thepage}%
  {\normalfont #1.} }

We also modify our definition \pn to utilise the target we added in \is:

\newcommand\pn[2]{\hyperpage{#2}\hyperlink{pn:#1}{n#1}}

numbers in indices with plausible-looking behaviour

Complete code:

\documentclass{report}
% ateb: https://tex.stackexchange.com/a/705581/ addaswyd o gwestiwn Thomkrates: https://tex.stackexchange.com/q/705565/
\usepackage{index}
\usepackage[texindy,splitindex]{imakeidx}
\usepackage[hidelinks]{hyperref}
\usepackage[page]{pagenote}

\newcommand\pn[2]{\hyperpage{#2}\hyperlink{pn:#1}{n#1}}

\makeatletter
\let\if@nopnote\iffalse % just for the example
\renewcommand{\notenuminnotes}[1]{%
  \edef\tempnumber{#1}%
  \edef\temppgnumber{\thepage}%
  {\normalfont #1.} }
\ExplSyntaxOn
\NewDocumentCommand {\is} { om }{%
  \if@nopnote
    \IfValueTF { #1 } {
      \index[#1]{#2}
    }{
      \index{#2}
    }
  \else
    \hypertarget{pn:\tempnumber}{}%
    \IfValueTF { #1 } {
      \thomkrates_pagenoteindex:nene { #1 } { \tempnumber } { #2 } { \temppgnumber }
    }{
      \thomkrates_pagenoteindex:ene { \tempnumber } { #2 } { \temppgnumber }
    }
  \fi
}
\cs_new_protected:Nn \thomkrates_pagenoteindex:nnnn
{
  \index [#1] { #3 | pn {#2}{#4} }
}
\cs_generate_variant:Nn \thomkrates_pagenoteindex:nnnn { nene }
\cs_new_protected:Nn \thomkrates_pagenoteindex:nnn
{
  \index  { #2 | pn {#1}{#3} }
}
\cs_generate_variant:Nn \thomkrates_pagenoteindex:nnn { ene }
\ExplSyntaxOff
\makeatother

\makeindex[program=texindy,name=Names,title=Names]
\makeindex[program=texindy,name=Something,title=Something]
\makepagenote

\begin{document}
First index.\index[Something]{First index} 
    
Sentence.\pagenote{First pagenote without index.}
    
\clearpage
    
Emph index.\index[Something]{Emph index@\emph{Emph index}} 
    
pagenote and index.\pagenote{Second pagenote with index.\is[Something]{Index inside second pagenote}}
    
\clearpage
    
Third pagenote.\pagenote{\emph{Emph index word}.\is[Something]{Emph index@\emph{Emph index}}}
    
Namenindex\index[Names]{Namenindex}
    
\printnotes
    
\printindex[Something]
\printindex[Names]
  
\end{document}
19
  • Good friend, you seem to be a genius. I do not understand that code, I am just a user of LaTeX and like it's principles, typesetting quality and possibilities - which you have again expanded. Thank you so much!
    – Thomkrates
    Commented Dec 23, 2023 at 17:00
  • Thank you. When I try the code I find two differences to your output: 1. in my output the emph word is correctly put as one output entry in index. Your output shows two separate output entries for the emph word. 2. in my output the pagenumber a lacking and only show "n4" for example without the pagenumber. But I put the code of your written macros in a separate file and included it with \include{pagenote-index-usage.tex}
    – Thomkrates
    Commented Dec 23, 2023 at 17:16
  • Yes, I really tried \include and \input and used the exact code of yours in the preamble. Always the same output result. as said with these 2 differences with your output. Do you have the newest version of texindy installed? Since difference number 1. appearce to me that your version is not equal to mine, but the output on my system seems correct and as intended. Difference 2. I have no idea where the problem lies.
    – Thomkrates
    Commented Dec 24, 2023 at 13:11
  • Hyperlinks are not yet tested by me in this situation, but I plan to use tex4ebook for my current book projects, which worked in the past very well. But for this currect book projekt I have needed an advanced look of indexing, and there the problems have risen. I hope to get it work with hyperlink and tex4ebook. In former work I saw that I used xindy when compling it with tex4ebook - \ifdefined\HCode. So what I need seems 1. texindy/xindy and 2. tex4ebook and hyperlink support. Due to this emph problem with double output entries when not using texindy/xindy.
    – Thomkrates
    Commented Dec 25, 2023 at 7:54
  • Where did you find this \hyperindexformat? I have no idea where to look at the present definition.
    – Thomkrates
    Commented Dec 25, 2023 at 18:03

You must log in to answer this question.

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