1

I've figured out how to make an second index, and I've found a reference on how to include the section number in the index, and I've found a reference on how to markup different indexes differently, but I'm not seeing how to combine all three.

What I'm trying to get is a "default" index that just does the totally normal thing that imakeidx does right out of the box and also a second index that indexes by document structure rather than page number (in my case specifically either the \subsection the item is in or even better; a custom string I set at the top of the section). If it makes any difference the items in the index will be interleaved in the document.


Because someone asked for a MWE, I grabbed, from the first two questions, the first things that looked like they might work that I even remotely understood and copy pasted them together. I don't understand what the third question is doing enough to even try to merge it in but I think it's doing something only distantly related?

I have zero reason to expect anything here related to indexing is in any way a reasonable approach... or that it isn't. So please don't get fixated on fixing anything below, the question above here is the question I'm trying to solve.

\documentclass[12pt]{article}

\usepackage{imakeidx}

\makeatletter
\renewcommand{\imki@wrindexentrysplit}[3]{%
 \expandafter\protected@write\csname#1@idxfile\endcsname{}%
    {\string\indexentry{#2}{s\arabic{section},p\thepage}}%
}
\makeatother

\makeindex
\makeindex[name=X,title={X Index}]

\begin{document}

Something to make the page and section different
\clearpage

\section{Foo} \index{foo} \index[X]{foo-x}
...
\clearpage

\section{Bar} \index{bar} \index[X]{bar-x}
...

\clearpage
\printindex
\printindex[X]

\end{document}

This results in two indexes that both show both types of information.

enter image description here enter image description here

What I want one index to show only one and the other to show only the other.

Index

bar p3 foo p2

X Index

bar-x s2 foo-x s1


Almost working dead end

The following happens to generate the typeset result I'd like (while failing to generate correct links in PDFs), but does so in a way that is fundamentally flawed: it depends on authors never doing things the normal way. I don't see how this problem can be removed from this approach.

\documentclass[12pt]{article}

\usepackage{imakeidx}

\makeindex
\makeindex[name=X,title={X Index}]

\begin{document}

\makeatletter
\newcommand{\myIndex}[1]{{
    \renewcommand{\imki@wrindexentrysplit}[3]{%
     \expandafter\protected@write\csname##1@idxfile\endcsname{}%
        {\string\indexentry{##2}{S\arabic{section}}}%
    }
    \index[X]{#1}
}}
\makeatother

Something to make the page and section different
\clearpage

\section{Foo} \index{foo} \myIndex{foo-x}
...
\clearpage

\section{Bar} \index{bar} \myIndex{bar-x}
...

\clearpage
\printindex
\printindex[X]

\end{document}
12
  • There's not enough information here to know what you're doing or what's not working about it. Can you please expand and give a minimal example that shows what you've tried and why it's not meeting your needs?
    – Don Hosek
    Aug 10, 2021 at 4:25
  • @DonHosek I don't even have a minimal example, or at least not one any more useful that the answers in the linked questions. I haven't even figured out how those parts actually work well enough to try and put them together. -- The first two are orthogonal enough that I'd guess a straight merge would work, but that would be worse than useless without the third part and I'm no clue how to do that, even working from the answers in linked question. Is that question even remotely close to the right approach?
    – BCS
    Aug 10, 2021 at 4:32
  • Given that the first of the linked questions had multiple answers, it's impossible to say. I would suggest starting with a document that has the second index, then see if you can figure out the second step and if not, take that as a mwe to get to an answerable question.
    – Don Hosek
    Aug 10, 2021 at 4:40
  • 1
    What you've provided actually is far more likely to get an answer than the initial query.
    – Don Hosek
    Aug 10, 2021 at 5:18
  • 1
    @BCS thanks for updating. So, why don't you just modify the \renewcommand, i.e. delete what you don't need as output?
    – MS-SPO
    Aug 10, 2021 at 6:28

1 Answer 1

0

Redefine the indexing output command locally, instead of globally, so that it outputs whatever you give it instead of \thepage, while all other indexes are unaffected.

A combination reference:

idxa

a plain reference

idxb

or, more helpfully:

idxc

or even a mixed hierarchy perhaps:

idxd

Here we are using imakeidx and xindy, and running with shell-escape.

Anything outputting as a structure (e.g., s\thesection) would need a matching Xindy format-parser defined (what Xindy calls define-location-class) for Xindy to be able to interpret it (example maps to: alphabet, null separator, arabic/roman numbers).

MWE

%---
\begin{filecontents*}[overwrite]{mal-style.xdy}
;;; Load a predefined style:
;(require "makeindex.xdy")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(define-attributes ("squarebr"))
(markup-attribute-group-list :sep "; ")


(markup-locref 
        :attr "squarebr" 
        :open "[\colorbox{yellow!40}{\hyperpage{" 
        :close "}}]")



;(markup-locref :class "page-numbers" :attr "definition"
;               :open  "{\bf " :close "}")
;(markup-locref :class "page-numbers" :attr "definition"
;               :open  "\textbf{" :close "}")



;(define-letter-groups
;  ("a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m"
;   "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"))
;
;(markup-letter-group-list :sep "~n\indexspace")

;(define-letter-group "ly" :after "l" :before "m")
;(define-letter-group "ny" :after "n" :before "o")
;
;(markup-letter-group :open-head "~n {\bf " :close-head "}"
;                     :capitalize)

;(define-location-class "appendices"
;                       ("ALPHA" :sep "-" "arabic-numbers")
;                       :hierdepth 2)
;(markup-locref-list            :sep "; " :depth 0 :class "appendices")
;(markup-locref-list :open "~~" :sep ", " :depth 1 :class "appendices")

;(define-alphabet "bible-chapters"
;    ("Genesis" "Exodus" "Leviticus" "Numbers" "Deuteronomy"
;     ... ))
;(define-location-class "bible-verses"
;    ("bible-chapters" :sep " " "arabic-numbers" :sep "," "arabic-numbers"))




; (define-letter-groups (""))
;(define-letter-group "z")
(markup-locclass-list :open ": ")
(define-letter-group "Α" ; Greek alpha
            :prefixes ("α");\x0391") 
            :after "Z"
            )
;(define-letter-groups ("Num"  "Dtn" "Ps"))
;(define-letter-group "Greek"
;   :prefixes ("α" "ω")
;   :after "Z")
;%(markup-letter-group  [:open string] [:close string] [:group group-name]
;%                      [:open-head string] [:close-head string]
;%                      [:upcase | :downcase | :capitalize])
;%                      
;  \\begingroup
;    \\setlength{\\fboxsep}{5pt}%
;    \\colorbox{blue}{%
;      \\color{white}\\large\\sffamily\\bfseries
;      \\hbox to \\dimexpr\\linewidth-2\\fboxsep{%  
;        \\hfill 
(markup-letter-group 
           :open-head "~n {\setlength{\fboxsep}{5pt}\sffamily\bfseries\large\colorbox{blue!30}{\textcolor{yellow!90!red!5}{\hbox to \dimexpr\linewidth-2\fboxsep{
        \hfill " ; space is needed
           :close-head "\hfill}}\nopagebreak\vspace{2mm}}}"
           )
;(markup-letter-group 
;           :open-head "~n {\sffamily\bfseries\colorbox{blue};;;;{\textcolor{yellow}{"
;           :close-head "}}}"
;           )


(markup-locclass-list :open "\dotfill" :sep "")

(define-attributes (("texttt" "textsc" "myown")) )
(markup-locref :attr "textbf" :open "\textbf{\hyperpage{" :close "}}")
(markup-locref :attr "textit" :open "\textit{\hyperpage{" :close "}}")
(markup-locref :attr "texttt" :open "\texttt{\hyperpage{" :close "}}")
(markup-locref :attr "textsc" :open "\textsc{\hyperpage{" :close "}}")
(markup-locref :attr "myown" :open "\myown{\hyperpage{" :close "}}")
(markup-locref :attr "hyperpage" :open "\hyperpage{" :close "}")
(markup-locref :attr "default" :open "\hyperpage{" :close "}")
(merge-rule "n" "X" :again)

\end{filecontents*}


%---
\begin{filecontents*}[overwrite]{general-aindex.xdy}
(define-alphabet "secalpha" ("s" "p"))
(define-location-class "section-index" :var
  ("secalpha"
  :sep ""
    "arabic-numbers" 
    :sep "-"
    "secalpha" :sep ""
        "arabic-numbers" :sep ", " 
    ))
(markup-locclass-list :open "\dotfill " :sep ", " :close "")

(define-attributes (("sectionmark" "sectionmarky")) )
(markup-locref 
            :attr "sectionmark" 
            :open "\textbf{\S}" 
            :close "")

(markup-locref 
            :attr "sectionmarky" 
            :open "\colorbox{yellow}{\textbf{\S}" 
            :close "}")



\end{filecontents*}





\documentclass{article}
\usepackage{xcolor}
\usepackage{fontspec}
\setmainfont{Noto Serif}

\usepackage[hyperindex=false,colorlinks]{hyperref}

%\usepackage{splitidx}
\usepackage{imakeidx}
\usepackage{xparse}

        
\ExplSyntaxOn

\makeatletter

\NewDocumentCommand { \generalindex } { o m m } {
        \IfValueTF {#1}
        {
                    {

% Local redefinition of indexentry to #3 instead of page%
\renewcommand{\imki@wrindexentrysplit}[3]{%
 \expandafter\protected@write\csname##1@idxfile\endcsname{}%
    {\string\indexentry{##2}{#3}}%
}%

    \index[#1]{#2}
                    }
        }
        {   }
}
\makeatother                


\NewDocumentCommand { \indexsec } { m } {
                \generalindex[sec]{#1}{s\thesection-p\thepage}
}

\NewDocumentCommand { \indexsecb } { m } {
                \generalindex[secb]{#1}{\thesection}
}

\NewDocumentCommand { \indexsecc } { m } {
                \generalindex[secc]{#1|sectionmark}{\thesection}
}

\NewDocumentCommand { \genindex } { m m m } {
                \generalindex[#1]{#2}{#3}
}



\ExplSyntaxOff



\makeindex[title=Xindy--demo,
program=texindy,
options=
        -M mal-style.xdy 
        -C utf8
        -t secondary.ilg
        ,
columnseprule,
]
\makeindex[name=s,title=Statutes,
program=texindy,
options=
        -M mal-style.xdy 
        -C utf8
        ,
columnseprule,
]

\makeindex[name=sec,
title={IndexA Xindy {\small\\ by section}},
program=texindy,
options=-M general-aindex.xdy 
        -C utf8
        -t \jobname aidx.ilg
        ,
]

\makeindex[name=secb,
title={IndexB Xindy {\small\\ by section only}},
program=texindy,
options=-M general-aindex.xdy 
        -C utf8
        -t secb.ilg
        ,
]

\makeindex[name=secc,
title={IndexC Xindy {\small\\ by section, with section-mark applied}},
program=texindy,
options=-M general-aindex.xdy 
        -C utf8
        -t secc.ilg
        ,
]

\makeindex[name=secd,
title={IndexD Xindy {\small\\ mixture of reference types}},
program=texindy,
options=-M general-aindex.xdy 
        -C utf8
        -t secd.ilg
        ,
columns=1,
]



%=====
\newcommand{\secindex}[1]{\textbf{#1}\indexsec{#1}}
\newcommand{\secbindex}[1]{\indexsecb{#1}}
\newcommand{\seccindex}[1]{\indexsecc{#1}}

\newcommand\yellowcommand[1]{{\sffamily\textbf{#1}}}


\begin{document}
% from tex.se:
\def\myshift#1{\makebox[0pt][r]{\texttt{--}}\texttt{#1}}
\def\myown#1{{\Huge#1}}
\def\mycommand#1{{\color{green}#1}}
\def\mydef#1{\texttt{\makebox[0pt][r]{\char"5C}#1}}
\def\mymath#1{{\color{blue}$#1$}}
\def\myfavorite#1{{\color{cyan}\makebox[0pt][r]{I $\heartsuit$\ }{\color{cyan}#1}}}


\section{My section}\label{mysection}
I refer to my section~\ref{mysection}.
Start. Nunc ligula faucibus\index{xindy|textbf}. 
\newpage
\index{vector@Vector}
\index{runme@\mycommand{runme}}
\newpage
\index{asquared@\mymath{a^2}}
\index{bsquared@\mymath{b^2}}
\newpage
x
\newpage
\index{csquared@\mymath{c^2}}
\index{roof|myown}
\newpage
\index{mal@\myshift{mal}}
\index{nature@\textbf{Nature}}
\newpage
\index{home!my own 1}
\index{home!my own 2}
\newpage
\index{definition@\mydef{definition}}
\index{Velocity}
Nullarutrum porttitor...
\newpage
\index{alcohol}\index{beauty}\index{älf}\index{zero}
\newpage
x\index{vectors@\myfavorite{vectors}}
x\index{roof|myown}
\newpage
x\index{roof}
% additions:
x\index{αβγ}
\newpage
x\index{10.2.250.1}
x\index{10.12.250.12}
\newpage
x\index{60.12.250.12}
x\index{128.12.250.12}
\newpage
x\index{128.12.250.12}
x\index[s]{s 7}x\index[s]{s 7B}x\index[s]{s 50}x\index[s]{s 5}x\index[s]{s 3(2)(a)}
\newpage
x\index[s]{SOGA!s 7}x\index[s]{SOGA!s 7B}x\index[s]{SOGA!s 50}x\index[s]{SOGA!s 5}x\index[s]{SOGA!s 3(2)(a)}
x\index[s]{Vsort!V-sort!V128}
x\index[s]{Vsort!V-sort!V64}
x\index[s]{SOGA!s 3(12)(a)}

x\index{first level!second level!third level}
x\index{xfirst level!xsecond level!xthird level|textbf}

\newpage
x\index{xfirst level!xsecond level|textit}
\newpage

x\index{xfirst level|squarebr}

%---------------------------------------------------
\section{Model}
x\secindex{model}
x\secbindex{model}
x\seccindex{model}
y\genindex{secd}{model!amain section@\yellowcommand{main section}|sectionmarky}{\thesection}
y\genindex{secd}{model!details!intro}{p\thepage}
\section{Hypothesis}
x\secindex{hypothesis}
x\secbindex{hypothesis}
x\seccindex{hypothesis}
\section{Testing}
x\secindex{testing}
x\secbindex{testing}
\newpage
x\seccindex{testing}
\section{Methodology}
x\secindex{methodology}
\newpage
x\secbindex{methodology}
x\seccindex{methodology}
y\genindex{secd}{model!details!causative}{p\thepage}
\newpage
\section{Experiment}
x\secindex{experiment}
x\secbindex{experiment}
x\seccindex{experiment}
\newpage
y\genindex{secd}{model!details!error margin}{p\thepage}
\newpage
\section{Results}
x\secindex{results}
x\secbindex{results}
x\seccindex{results}
y\genindex{secd}{model!details!error margin}{p\thepage}
\newpage
y\genindex{secd}{model!details!error margin}{p\thepage}
\section{Conclusions}
x\secindex{conclusions}
x\secbindex{conclusions}
x\seccindex{conclusions}
\section{Acknowledgements}
x\secindex{acknowledgements}
x\secbindex{acknowledgements}
x\seccindex{acknowledgements}





%---------------------------------------------------
  \printindex
    \printindex[s]
    \printindex[sec]
    \printindex[secb]
    \printindex[secc]
    \printindex[secd]
        
\end{document}
2
  • This eventually does exactly what the last section of my question explicitly rejects as non-viable: using non-standard $\index$ commands.
    – BCS
    Sep 13, 2022 at 5:13
  • Correct. The two-outputs can be embedded in some sort of parser (which will still need a user-level, therefore non-standard, two-choice approach/decision (e.g., index name?)), but I don't know how the parsing should work (yet). So therefore not an answer to the question. Apologies for not making that clear earlier (no time at 3am! ;) ). The design question becomes" How can it be automated?", as I see it. Is it solvable at all? It's a type of uniqueness problem. Perhaps via keywords from the editor stage??
    – Cicada
    Sep 13, 2022 at 10:39

You must log in to answer this question.

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