0

I'm using the packages imakeidx and idxlayout (the latter with "singlepar" layout) and I'm trying to customize the default delimiters. My aim is to obtain different delimiters for subitem and subsubitem. For example, my idea is that the following entries within the text

\index{Author!Work1!1,15} 
\index{Author!Work1!9,32}
\index{Author!Work2!2,24} 
\index{Author!Work2!5,11}

should give this entry in the index (I added random page numbers):

Author, Work1, 1,15: 9 — 9,32: 16 — 1,15: 27 — Work2, 2,24: 57 — 5,11: 98

However, I added these lines in order to substitute the default idxlayout delimiters (which is semicolon)

\renewcommand{\indexsubsdelim}{, }
\newcommand*{\indexsubsdelimb}{ — }
\renewcommand{\subitem}{\unskip\indexsubsdelim}%
\renewcommand{\subsubitem}{\unskip\indexsubsdelimb}%

but the result is as follows (what is wrong is the line just after the subitem and the comma after the subsubitem):

Author, Work1 — 1,15: 9 — 9,32: 16 — 1,15: 27, Work2 — 2,24: 57 — 5,11: 98

Is it possible to obtain the desired layout? Thank you for your help.

Here is the MWE:

\begin{filecontents*}{colon.ist}
delim_2 ": "
\end{filecontents*}
\documentclass{article}
\usepackage{lipsum}
\usepackage{imakeidx}
\usepackage[itemlayout=singlepar]{idxlayout}

   \renewcommand{\indexsubsdelim}{, }
    \newcommand*{\indexsubsdelimb}{ — }
    \renewcommand{\subitem}{\unskip\indexsubsdelim}%
    \renewcommand{\subsubitem}{\unskip\indexsubsdelimb}%


\makeindex[options=-s colon]

\begin{document}
\lipsum[1]
\index{Author!Work1!1,15} 
\index{Author!Work1!9,32}
\index{Author!Work2!2,24} 
\index{Author!Work2!5,11}

\printindex
\end{document}
qwertxyz
  • 542

1 Answers1

1

You can use something like the following. The idea is to distinguish the first subsubitem and the rest of them. So, the item defines \firstsubsubitemtrue, and the subsubitem turns it into false to toggle its behavior for the rest of subsubitems.

\begin{filecontents*}{colon.ist}
delim_2 ": "
\end{filecontents*}
\documentclass{article}
\usepackage{lipsum}
\usepackage{imakeidx}
\usepackage[itemlayout=singlepar]{idxlayout}

\makeatletter
\newif\iffirstsubitem
\newif\iffirstsubsubitem
\renewcommand{\indexsubsdelim}{, }
\newcommand*{\indexsubsdelimb}{\iffirstsubsubitem\unskip\firstsubsubitemfalse, \else --- \fi}
\renewcommand{\subitem}{\iffirstsubitem\unskip\firstsubitemfalse, \else --- \fi\firstsubsubitemtrue}%
\renewcommand{\subsubitem}{\indexsubsdelimb}%
\renewcommand{\@idxitem}{\par\setlength{\hangindent}{\ila@hangindent}\firstsubitemtrue}
\makeatother


\makeindex[options=-s colon]

\begin{document}
\lipsum[1]
\index{Author!Work1!1,15} 
\index{Author!Work1!9,32}
\index{Author!Work2!2,24} 
\index{Author!Work2!5,11}

\printindex
\end{document}

The result (the second page of it):

enter image description here

  • This is perfect! One thing: is it possible to use the line even after the second subitem (i.e. before work2: Author, Work1, 1,15: 9 — 9,32: 16 — 1,15: 27 — Work2, 2,24: 57 — 5,11: 98) Thank you!! – qwertxyz Mar 03 '19 at 10:20
  • 1
    I see. The idea could be the same, define some \iffirstsubitedm and use it. I'll try to adapt the answer. – Sergei Golovan Mar 03 '19 at 10:28
  • Thank you, it worked perfectly as intended. Just a little thing: is it possible to suppress the comma even after the work title (e.g. Work1 1,15: 9 instead of Work1, 1,15: 9)? – qwertxyz Mar 03 '19 at 11:07
  • 1
    It is possible. Hint: the comma is added in one of the comma-dash choice. – Sergei Golovan Mar 03 '19 at 11:17
  • Sorry, I cannot manage to do it. If I leave a blank space in \renewcommand{\indexsubsdelim}{ } the result is the same. Could you write the solution? – qwertxyz Mar 03 '19 at 11:26
  • 1
    Then try to remove another comma. Only two of them left. – Sergei Golovan Mar 03 '19 at 11:30
  • Thank you! I manged to do it by changing \newcommand*{\indexsubsdelimb}{\iffirstsubsubitem\unskip\firstsubsubitemfalse\ \else --- \fi} – qwertxyz Mar 03 '19 at 11:39
  • is it possible to avoid that the line is printed at the beginning of a line? Or, changing with "\else;\quad", to eliminate the space just after the number of the page? – qwertxyz Apr 22 '20 at 22:21