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 Answers1

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}

cfr
  • 198,882
  • 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 Dec 23 '23 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 Dec 23 '23 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 Dec 24 '23 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 Dec 25 '23 at 07:54
  • Where did you find this \hyperindexformat? I have no idea where to look at the present definition. – Thomkrates Dec 25 '23 at 18:03
  • \hyperindexformat is neither to find in index package nor in hyperref package. I do not know how to see its definition or possibility of changing. – Thomkrates Dec 25 '23 at 18:44
  • I found the definiton in hyperref.sty: \def\hyperindexformat#1#2{% \let\HyOrg@hyperpage\hyperpage \let\hyperpage@firstofone #1{\HyOrg@hyperpage{#2}}% \let\hyperpage\HyOrg@hyperpage }% -- I still do not understand that Code. Are you able to understand it? – Thomkrates Dec 25 '23 at 18:56
  • Beside that definition of \hyperindexformat the wording \hyperindexformat is to be found 5 more time in hyperref package. Just to change hyperindexformat seems not sufficient for me, since these other five usages would be needed to change or completed with it. - We have in this problem 2 numbers (instead of one for a single index-entry) that need to go into index (the pagnote-number and the page-number of the pagenote). hyperindex-entry would have a link for 1. the pagenote-number, and 2. the page-number of the pagenote (which do not link to the same area in the hypertext document). – Thomkrates Dec 25 '23 at 19:33
  • The present solution (provided by you) gives on my system the note-number in the index-output (working with splitindex). And does not give an error message. Which should be sufficient to me at the moment. But I asked for the page-number of the page-note-indexing, since I saw this in an american book from 2015, which I have just recently translated into German. And my first intension was to have the index in pagenote exaxtly that way. But if I need to have a compromise, I would skip the page-number and only set the pagenote-number. What do you think? Is there a chance for the page-number? – Thomkrates Dec 26 '23 at 02:02
  • I think both are possible, but I don't think both are possible for me :(. The note number might be, though. I thought I had that bit almost working earlier, but then I undid it .... – cfr Dec 26 '23 at 03:26
  • @Thomkrates See edited answer above. This gets the printed output right, I think, at the cost of some violence to LaTeX's sensibilities. It also links to the note. The page is also a link, but it links to the same target. If you use this, you should be aware that I don't know what I'm doing when I bypass the standard mechanisms. Independently of that, you probably want to put the index command earlier in the note as the target for the hyperlink is created only when \is is called. That is, it isn't, strictly speaking, linking to the note but to an invisible target created by \is. – cfr Dec 26 '23 at 04:44
  • @Thomkrates We should tidy up the comments a bit so the mods don't have it all to do on Boxing Day ;). – cfr Dec 26 '23 at 04:47
  • Great. As a ordinary user I can say: It works as intended. Thanks so much. There is the following in the log file: LaTeX Warning: Command \markboth has changed. Check if current package is valid. LaTeX Warning: Command \markright has changed. Check if current package is valid. Without yet trying tex4ebook it seems not to be an error complaining, only a warning. So I will see. - Have a nice boxing day and winter time. – Thomkrates Dec 26 '23 at 08:47
  • Just a last reflection (from my source): The index-opening and closings for page ranges seem not to work with your code. I get the following warning (indeed plenty of them which where working before and absence): WARNING: Found no :close-range matching an already opened one! Location-reference is 72 in keyword (lange aufbleiben). Maybe I lost some of the regular location-references. WARNING: Found a :close-range in the index that wasn't opened before! Location-reference is 73 in keyword (lange aufbleiben) I'll continue and ignore this. - And in the index no pagenumbers/ranges are given. – Thomkrates Dec 26 '23 at 10:18
  • @Thomkrates I'm not really surprised by that. My brute-force-and-ignorance approach is absolutely unsophisticated. Moreover, even the doc package breaks that functionality. (Though I think that's because it repurposes the encap character.) – cfr Dec 26 '23 at 16:52
  • @Thomkrates I don't know why it would change \markboth etc. That sounds rather odd. Those are used to set the 'marks' used in headers. The code I used does violence to the indexing mechanism, but it shouldn't alter the way headers get set etc. At least, I don't think so unless you're using dictionary-style headers or similar in the indices? – cfr Dec 26 '23 at 17:02
  • The headers in my source and output file are fine, no problem, only these two warnings. - the open-range and close-range problems is more servere, since in index output there are no page-numbers given and therefore I would not be able to use your code, since my source is 16 pages indices large and I need it complete. - What are "dictionary-style headers"? My headers work correctly as said initially. – Thomkrates Dec 26 '23 at 19:31
  • @Thomkrates Perhaps a followup re. the range issue. I tried to figure out how to get that working with doc but couldn't. Or I can delete this answer if you'd prefer. Dictionary-style headers are headers which indicate the first and last entries on each page, so the header content depends on which glossary, dictionary or index entries end up on the page. It wouldn't surprise me if my code broke those, since those obviously depend on the indexing code. Otherwise, the marks mechanism shouldn't be affected, I wouldn't think. – cfr Dec 26 '23 at 21:06
  • Ok, thank you, no, I do not use dictionary headers in that index. – Thomkrates Dec 26 '23 at 22:49