1

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.

enter image description here

Also, I'd like to be able to define the amount of indentation of the subitems, which I would prefer to decrease:

enter image description here

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).

enter image description here

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:

enter image description here

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?

enter image description here

qwertxyz
  • 542
  • Can't help with the tun-together items, but regarding the indentations, these are defined in article.cls. Look for \newcommand\@idxitem, \subitem and \subsubitem. Redefining those dimensions should be relatively easy to do in your preamble. – barbara beeton Apr 19 '20 at 21:01
  • 1
    Adding \par to the end of ale_index_nolevel:n, ale_index_secondlevel:n and ale_index_firstlevel:n solves 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
  • @AndrewSwann thanks, this solved the problem! Is it possible to stretch horizontally the text in order that each line fill all the paragraph? – qwertxyz Apr 22 '20 at 09:42
  • @ALE Glad that helps. For the stretching I am not really clear what you are asking for. May be you could add some more explanation to the end of your question. – Andrew Swann Apr 22 '20 at 10:10
  • @AndrewSwann thank you, I inserted an image to explain what I mean. – qwertxyz Apr 22 '20 at 10:21
  • Indexes are usually set ragged right. If you instead want it set justified, it should be sufficient to patch the \printindex definition 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
  • @barbarabeeton Thank you, I loaded package idxlayout and now the text is justified, but it gives the effect that you said (see image above). How can I do in order to set a fixed distance between reference and page number? Currently the "separator" is ,---~, but I can change it. – qwertxyz Apr 22 '20 at 15:16
  • The space between reference and page number is generated by the writing out of the index terms, so changing that would not only be a pain, but would likely compromise the sort. What you want to do instead is make the space after the dash more stretchable than any other space. I would try \hspace{.3em plus 5em}, the .3em being close to the width of an ordinary interword space. Stretch of 5em should 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
  • @barbarabeeton thank you very much. ,--\hspace{.3em plus 5em} could be well. However, I'm not entirely satisfied with the result and I think I'll use ;\hspace{.3em plus 5em} as a separator – qwertxyz Apr 22 '20 at 15:50
  • Good accommodation (in my opinion); I think that will be much clearer for somebody looking up something in the index. (But that's always subjective, and the author is generally right unless the result defies a publisher's stated specs.) – barbara beeton Apr 22 '20 at 16:00
  • Is it possible to break an entry between two lines (the reference in a line and page number in another)? I see that it never happens, causing big white spaces – qwertxyz Apr 22 '20 at 16:08

0 Answers0