3

I have a book with large amount of numbered short stories, and at the end of the book I would like to have the subject index looking somewhat like this:

funny ... 100 (222-225, 227), 200 (555)

In the brackets are the story numbers.

How can I do it with xindy or some other tool?

MWE may be like this:

\documentclass[twoside]{book}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}
% \usepackage[T2A]{fontenc}
\usepackage{fix-cm,anyfontsize}
\setmainfont[Mapping=tex-text, FakeStretch=0.97]{Cambria}
\newfontfamily\cyrillicfont[Mapping=tex-text, FakeStretch=0.97]{Cambria}
\usepackage[protrusion=all]{microtype}
\usepackage{polyglossia}
\setdefaultlanguage{russian}
\usepackage{makeidx}
\usepackage{multind}
\makeindex{subj}
\makeindex{name}
\newcommand{\subj}[1]{\index{subj}{#1}}
\newcommand{\name}[1]{\index{name}{#1}}
\newcounter{story}
\setcounter{story}{1}
\newcommand{\newstory}{%
    \pagebreak[3]%
    \vspace{1cm}%
    \noindent\textbf{\thestory. }\stepcounter{story}\ignorespaces
}


\begin{document}
\newstory
Была одна история…
\subj{funny}

\newstory
Была другая история…
\subj{funny}

\newstory
Была третья история…
\subj{sad}

\clearpage

\newstory
Была и ещё такая история…
\subj{funny}

\clearpage
\printindex{subj}{Тематический указатель}

\end{document}

Compiled by

xelatex mwe
xindy -M texindy -M page-ranges -L russian -C utf8 -o subj.ind subj.idx
xelatex mwe
2
  • Can you be a bit more specific? Where do the story numbers come from? Please add the code of a complete (=compiling) small document that shows the book setup, contains a sufficient number of (dummy) stories, etc.
    – gernot
    Jan 4, 2017 at 14:06
  • @gernot Added mwe. Story numbers come from a simple counter.
    – pantlmn
    Jan 4, 2017 at 16:16

1 Answer 1

4

Edit: I changed the definition of \thepagestory, because the old definition could sometimes give the wrong page number (around a page break).

I changed the document to use \usepackage{index} instead of \usepackage{multind} because index can have other numbers than page numbers in the index, and multind cannot do this. Note: with the index package, you must run LaTeX twice on your document before running xindy.

I also duplicated two stories to get a range in the story numbers. I also made the page numbers bold, but you can easily change that in the xdy file.

So here is the document:

\documentclass[twoside]{book}
\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{xltxtra}
% \usepackage[T2A]{fontenc}
\usepackage{fix-cm,anyfontsize}
\setmainfont[Mapping=tex-text, FakeStretch=0.97]{Cambria}
\newfontfamily\cyrillicfont[Mapping=tex-text, FakeStretch=0.97]{Cambria}
\usepackage[protrusion=all]{microtype}
\usepackage{polyglossia}
\setdefaultlanguage{russian}
\usepackage{makeidx}
\usepackage{index}
\makeindex
\newcommand\thepagestory{\noexpand\thepage-\thestory}
\newindex[thepagestory]{subj}{sidx}{sind}{Тематический указатель}
\newcommand{\subj}[1]{\index[subj]{#1}}
\newcounter{story}
\setcounter{story}{0}
\newcommand{\newstory}{%
    \pagebreak[3]%
    \vspace{1cm}%
    \stepcounter{story}\noindent\textbf{\thestory. }\ignorespaces
}

\begin{document}
\newstory
Была одна история…
\subj{funny}

\newstory
Была другая история…
\subj{funny}

\newstory
Была одна история…
\subj{funny}

\newstory
Была другая история…
\subj{funny}

\newstory
Была третья история…
\subj{sad}

\clearpage

\newstory
Была и ещё такая история…
\subj{funny}

\clearpage
\printindex[subj]%{Тематический указатель}

\end{document}

This is the xindy style file stories.xdy.

(require "latex.xdy") 
(require "latex-loc-fmts.xdy") 

(markup-locref :open "" :close "" :attr "hyperpage")


(define-location-class "stories" ("arabic-numbers" :sep "-" "arabic-numbers")
                      :hierdepth 2)

(markup-locref-list            :sep " "
                       :depth 0  :class "stories")
(markup-locref-list  :open " (" :sep ", " :close ")"
                       :depth 1  :class "stories")
;;
;; Put page numbers in bold
;;
(markup-locref-layer :open "\textbf{" :close "}" :layer 0
                       :depth 0  :class "stories")

(markup-range :sep "-" :class "stories")

The command to run is, where storyindex.tex is my document: (I don't have the russian file so I left out -L russian myself)

xindy -M stories -L russian -C utf8 -o storyindex.sind storyindex.sidx

And this is the resulting index: enter image description here

3
  • @pantlmn I made a small but important change, because sometimes the page number could be wrong in the original solution. Jan 4, 2017 at 22:30
  • Thanks, I managed to do almost the same, but by redefining \@wrindex in the multind package: \indexentry{#2}{\thepage-\thestory}
    – pantlmn
    Jan 6, 2017 at 13:16
  • Please note: In my solution I put a correction: \thepage should be preceded by \noexpand, otherwise you might occasionally get the wrong page number. I can give you an example where it goes wrong without it: Put \usepackage{lipsum} before \begin{document} and \lipsum[1-4] after the fourth story, so that story number 5 is at the top of the second page. You will see you get page 1 for the fifth story without \noexpand and page 2 with \noexpand. I hope this will also work with multind. Jan 7, 2017 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .