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}
  • 1
    Please, add a minimal compilable example. – egreg Mar 2 at 18:51
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!! – Εὐφορίων Mar 3 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 3 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)? – Εὐφορίων Mar 3 at 11:07
  • 1
    It is possible. Hint: the comma is added in one of the comma-dash choice. – Sergei Golovan Mar 3 at 11:17
  • 1
    Then try to remove another comma. Only two of them left. – Sergei Golovan Mar 3 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.