With an ingenious very different implementation, in this answer egreg helped me to obtain a custom style of index with imakeidx package. Although the layout is the one desired, it sometimes does not seem to work, especially with long names: as seen in the photo, "Author9" is not positioned correctly.
Also, I'd like to be able to define the amount of indentation of the subitems, which I would prefer to decrease:
Here is an MWE:
\begin{filecontents*}{\jobname.mst}
item_0 "\n\n \\item "
item_1 " \\firstlevel "
item_2 " \\secondlevel "
item_01 " \\firstlevel "
item_x1 " \\firstlevel "
item_12 " \\secondlevel "
item_x2 " \\secondlevel "
delim_0 ":~"
delim_1 ":~"
delim_2 ":~"
\end{filecontents*}
\documentclass[a4paper, 11pt, twoside, openright]{article}
\usepackage[top=49.5mm,bottom=52.5mm,inner=39mm,outer=39mm, headheight=4mm, headsep=6mm, marginparwidth=14mm, marginparsep=2mm, nofoot]{geometry}
\usepackage[no-math]{fontspec}
\defaultfontfeatures{Ligatures={TeX, NoCommon}}
\usepackage{polyglossia}
\setmainlanguage{italian}
\setotherlanguage[variant=ancient]{greek}
\setotherlanguage{german}
\setotherlanguage{english}
\setotherlanguage{latin}
\usepackage{microtype}
\usepackage{imakeidx}
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \l_ale_index_item_seq
\seq_new:N \l_ale_index_subitem_seq
\tl_new:N \l_ale_index_author_tl
\tl_new:N \l_ale_index_work_tl
\dim_new:N \l_ale_index_indent_dim
\cs_set_protected:cpn { @idxitem } #1 \par
{
\tl_if_in:nnTF { #1 } { \firstlevel }
{
\tl_if_in:nnTF { #1 } { \secondlevel }
{
\ale_index_secondlevel:n { #1 }
}
{
\ale_index_firstlevel:n { #1 }
}
}
{
\ale_index_nolevel:n { #1 }
}
}
\cs_new_protected:Npn \ale_index_nolevel:n #1
{
\seq_set_split:NVn \l_ale_index_item_seq \c_colon_str { #1 }
\seq_pop_left:NN \l_ale_index_item_seq \l_ale_index_author_tl
\hbox_set:Nn \l_tmpa_box { \l_ale_index_author_tl, }
\dim_set:Nn \l_ale_index_indent_dim { \box_wd:N \l_tmpa_box }
\par\hangindent\l_ale_index_indent_dim
\l_ale_index_author_tl, \seq_use:Nn \l_ale_index_item_seq { }
}
\cs_generate_variant:Nn \seq_set_split:Nnn { NV }
\cs_new_protected:Npn \ale_index_secondlevel:n #1
{
\seq_set_split:Nnn \l_ale_index_item_seq { \firstlevel } { #1 }
\seq_pop_left:NN \l_ale_index_item_seq \l_ale_index_author_tl
\l_ale_index_author_tl\par\nobreak
\seq_map_function:NN \l_ale_index_item_seq \ale_index_subitem:n
}
\cs_new_protected:Npn \ale_index_subitem:n #1
{
\seq_set_split:Nnn \l_ale_index_subitem_seq { \secondlevel } { #1 }
\seq_pop_left:NN \l_ale_index_subitem_seq \l_ale_index_work_tl
\hbox_set:Nn \l_tmpa_box { \qquad \l_ale_index_work_tl,~ }
\dim_set:Nn \l_ale_index_indent_dim { \box_wd:N \l_tmpa_box }
\par\hangindent\l_ale_index_indent_dim % edited with intended indentation: \par\hangindent=2em
\qquad\l_ale_index_work_tl,~\seq_use:Nn \l_ale_index_subitem_seq { ~---~ } % edited with intended indentation: \quad\l_ale_index_work_tl,~\seq_use:Nn \l_ale_index_subitem_seq { ~---~ }
}
\cs_new_protected:Npn \ale_index_firstlevel:n #1
{
\seq_set_split:Nnn \l_ale_index_item_seq { \firstlevel } { #1 }
\seq_pop_left:NN \l_ale_index_item_seq \l_ale_index_author_tl
\hbox_set:Nn \l_tmpa_box { \l_ale_index_author_tl,~ }
\dim_set:Nn \l_ale_index_indent_dim { \box_wd:N \l_tmpa_box }
\par\hangindent\l_ale_index_indent_dim % edited with intended indentation: \par\hangindent=1em
\l_ale_index_author_tl,~\seq_use:Nn \l_ale_index_item_seq { ~---~ }
}
\ExplSyntaxOff
\makeindex[name=1]
\makeindex[name=2]
\makeindex[name=3]
\makeindex[name=4,options=-s \jobname.mst]
\newcommand*{\cose}[1]{\index[1]{#1}\ignorespaces}
\newcommand*{\parole}[1]{\index[2]{#1}\ignorespaces}
\newcommand*{\autori}[1]{\index[4]{#1}\ignorespaces}
\begin{document}
text
\autori{Author!Work1!1,15}
\autori{Author!Work1!9,32}
\autori{Author!Work2!2,24}
\autori{Author!Work2!5,11}
\autori{Author!Work2!2,25}
\autori{Author!Work2!5,12}
\autori{Author!Work1!11,42}
\autori{Author2!4,34}
\autori{Author3!6,12}
\autori{Author3!9,4}
\autori{Author4!9,4}
\autori{Author4!9,5}
\autori{Author4!2,6}
\autori{Author5!2,34}
\autori{Author6!2,4}
\autori{Author7!2,4}
\autori{Author8!2,12}
\autori{Author 123456!2,12}
\autori{Author 123456!2,13}
\autori{Author 123456!2,14}
\autori{Author 123456!2,15}
\autori{Author9 (ed. Author10)!Op. XYZ!2,45}
\autori{Author9 (ed. Author10)!Op. XYZ!2,46}
\autori{Author9 (ed. Author10)!Op. XYZ!2,47}
\autori{Author9 (ed. Author10)!Op. XYZ!2,48}
\autori{Author9 (ed. Author10)!Op. XYZ!2,49}
\autori{Author9 (ed. Author10)!Op. XYZ!2,50}
\printindex[4]
\end{document}
EDIT 1: I managed to obtain the desired indentation writing \par\hangindent=2em and \par\hangindent=1em instead of \par\hangindent\l_ale_index_indent_dim in sections \ale_index_subitem:n #1 and \ale_index_firstlevel:n #1 respectively and \quad\l_ale_index_work_tl,~\seq_use:Nn \l_ale_index_subitem_seq { ~---~ } instead of \qquad\l_ale_index_work_tl,~\seq_use:Nn \l_ale_index_subitem_seq { ~---~ } in section \ale_index_subitem:n #1 (see the MWE).
EDIT 2: The solution proposed by Andrew Swann worked for me. I would like to ask if it is possible to get justified text in the index, because it is currently left aligned, as the following image shows:
EDIT 3: Loading the package idxlayout I obtained a justified, but now the distance between the reference and the page number is too big: How can I do in order to set a fixed distance between reference and page number?





article.cls. Look for\newcommand\@idxitem,\subitemand\subsubitem. Redefining those dimensions should be relatively easy to do in your preamble. – barbara beeton Apr 19 '20 at 21:01\parto the end ofale_index_nolevel:n,ale_index_secondlevel:nandale_index_firstlevel:nsolves the run-in problem in your example and in other situations. I have no idea if this in the spirit of the original code. – Andrew Swann Apr 22 '20 at 09:37\printindexdefinition to remove that instruction. However, all spaces will then expand at the same rate, making the association between reference and page number less clear. It would be better to allow more stretch to just the spaces around the dash (the real "separator"). – barbara beeton Apr 22 '20 at 14:52\hspace{.3em plus 5em}, the.3embeing close to the width of an ordinary interword space. Stretch of5emshould be enough to overpower the default stretchiness, but if it isn't, you can easily increase that. – barbara beeton Apr 22 '20 at 15:33