I am writing a LaTeX document containing Python code, which I include using the Listings package through a macro called \example{}. It uses another macro called \code{} that also uses the Listings package to basically do \texttt{} but without the need to escape underscores (e.g. in file names). \example{base_name}{description} takes the base name of the file and a short description, then expands this to the full file name base_name_ex.py (for code examples; I have analogous macros to include other pieces of code), includes the file at Python/base_name_ex.py, creates the caption \code{base_name_ex.py}: description. and creates an index entry base_name_ex.py@\code{base_name_ex.py}. In addition, I want to create a (List of) Listings. This is shown in the minimal working example below and works: first_script_ex.py shows up in the General index and (List of) Listings.
\documentclass{article}
\usepackage{listings}
\usepackage{imakeidx}
\makeindex[title=General index, options=-s mwe] % Use Index style in mwe.ist
\makeindex[name=codidx, title=Index of Listings, options=-s mwe]
\newcommand{\code}[1]{\lstinline[basicstyle=\ttfamily]{#1}} % Print code inline
% Include code example, create caption with file name and create index entry:
\newcommand{\example}[2]{\lstinputlisting[caption={\code{#1_ex.py}:
#2.\index{\detokenize{#1_ex.py}@\code{#1_ex.py}}}]{Python/#1_ex.py}}
\begin{document}
\section{Test}
\example{first_script}{a first Python script}
Test entry 1\index{Test entry 1} % General index
Test entry 2\index[codidx]{Test entry 2} % Index of Listings
Test entry 3\index[codidx]{\code{Test entry 3}} % Index of Listing, but as Symbol
\printindex[codidx]
\lstlistoflistings
\printindex
\end{document}
mwe.ist shows index headings, which tells me whether an entry is listed under the correct letter of the alphabet, or under Symbols:
headings_flag 1
heading_prefix "\\Huge\\rmfamily\\noindent\\textbf{"heading_suffix "}\\nopagebreak"
item_0 "\n \\item \\normalsize "
The problem arises when I want the code listings to appear in a second Index (perhaps to replace the List of Listings, but I want both for now). Hence, I use the imakeidx package and create a General index as well as an Index of Listings, named codidx. However, as soon as I add [codidx] to the \index{} in the definition of \example, bad things start to happen. After a single compilation, the first_script_ex.py entry moves from the General to the Listings index, as desired, but the caption of the Listing in the document body gets first ̇script ̇ex.py@first_script_ex.py] added to it. After a second compilation, the entry in the (List of) Listings gets "codidx" added after "script.".
Clearly, my knowledge of (La)TeX isn't good enough to solve this puzzle, so my question is: is there a way to move the code entries into the Listings index without LaTeX source code appearing in the Listing caption, (List of) Listings (or any other side effects, for that matter)?
(PS: as a workaround, I switched the named and unnamed indices, but I'm looking for a proper solution).
\lstinlinewith\verb, but I haven't been able to come up with one.\texttt{}bolts at underscores (and my document seems to bolt at the underscore package). – AstroFloyd Dec 30 '21 at 19:23