0

I am making multiple scripts for a project. There are basically 10 or so roles, and in each document, I want to have an index in the front of the document, after a table of contents, that has the page numbers for each role next to the name. Ideally, this would be formatted like a table:

Role Description Pages
Role 1 Foo 1, 5, 10-11
Role 2 Bar 2, 5, 10-11
Role3 Baz 1, 7, 10-11, 21

I have tried using the imakeidx package with \makeindex and \printindex, and this works as expected with \index{term}. But, printing the index will only list the page numbers before \printindex. Like I said, I need it at the beginning because that is where the roles are. Further, I don't think there are the formatting options I am looking for in it.

Example of what I have currently:

\documentclass{article}
\usepackage{imakeidx}
\makeindex

\begin{document}

% PARTICIPANTS {as commands so I can reference them easily}
\newcommand{\celebrant}{\index{CEL}CEL }
\newcommand{\cantor}{\index{CANTOR}CANTOR }
\newcommand{\mco}{\index{MC1}MC1 }
\newcommand{\mct}{\index{MC2}MC2 }
\newcommand{\serveri}{\index{S1}S1 }
\newcommand{\serverii}{\index{S2}S2 }
\newcommand{\serveriii}{\index{S3}S3 }
\newcommand{\serveriv}{\index{S4}S4 }
\newcommand{\serverv}{\index{S5}S5 }
\newcommand{\servervi}{\index{S6}S6 }

\section{Participants}
% This is where I want the index-- I want page numbers of where these roles are in the document

        Celebrant (\celebrant): \underline{\makebox[1.8in][l]{}}\\
        Mast. of Cer. (\mco): \underline{\makebox[1.8in][l]{}}\\
        Cantor (\cantor): \underline{\makebox[1.8in][l]{}}\\
        Mast. of Cer. (\mct): \underline{\makebox[1.8in][l]{}}\\
        Server 1 (\serveri): \underline{\makebox[1.8in][l]{}}\\
        Crucifix, book\\
        Server 2 (\serverii): \underline{\makebox[1.8in][l]{}}\\
        Candle, paten\\
        Server 3 (\serveriii): \underline{\makebox[1.8in][l]{}}\\
        Candle, paten\\
        Server 4 (\serveriv): \underline{\makebox[1.8in][l]{}}\\
        Altar setup, bells\\
        \\
        Server 5 (\serverv): \underline{\makebox[1.8in][l]{}}\\
        Altar setup\\
        Server 6 (\servervi): \underline{\makebox[1.8in][l]{}}\\

\section{Begin}
        Form the procession line with the help of \mct.
            The order is:
            \begin{enumerate}
                \item \servervi with Thurible
                \item \serveri with Cross, \serverii and \serveriii with torches on each side
                \item All other altar servers
                \item \mct
                \item Gospel
                \item \mco
                \item \celebrant
            \end{enumerate}

\section{Section Two}
Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.

\printindex

\end{document}

Any ideas? I was referencing the Overleaf page for indices and did not find a solution.

Thanks!

Edit: fixed table format and added MWE

6
  • 1
    Hi there, and welcome to TeX StackExchange! You mention you've tried a few things already—could you edit your question to share your code? If you could include a minimal working example that we could copy-paste, that could help somebody to address your question.
    – gz839918
    Mar 27 at 0:39
  • 1
    Don't have imakeidx generate the index as part of your compilation. Run makeindex or xindy or whatever separately. Then there's no problem printing the index first. All it does is input a file. You can do that whenever you want.
    – cfr
    Mar 27 at 2:03
  • 2
    You will have to provide your own .ist file to get the formatting the way you want. Indexing is structured in levels, in your case you only have main entries. Suggestion first generate an index in a normal fashion, once you happy about the structure then develop the .ist file. Dont worry about the description at first as this is constant for each role and can be saved in macros say rolei@description{foo} .
    – yannisl
    Mar 27 at 2:17
  • What's the relationship between Role1, Foo and Celebrant, \celebrant and CEL? Presumably Celebrant is Role1. Then is CEL Foo? Or is the bit in parentheses in the first column of the table, too? I have an .ist to make a table, but I don't know what I'm trying to get into which column.
    – cfr
    Mar 27 at 3:27
  • @cfr these are just in the table for an example of what it should look like. As to your question, Celebrant is the role title, ``\celebrant` is is a macro to add the abbreviation CEL to the document, with whatever formatting I need (bold, highlighted, index entry, etc.) Mar 27 at 17:18

1 Answer 1

3

imakeidx creates the index file as part of the compilation and then reads it back in. As such, it has to be located after all \index commands have been issued. But that's not the standard case. Typically you create the index separately, just as you do the bibliography. Typesetting the index is then done on the next run by simply including the generated file. This means you can print the index at any point, just as you can the bibliography.

Here's an example with makeidx rather than imakeidx. Compile normally, then run makeindex and then recompile. You may need to repeat this to get all the page numbers correct as inserting the index changes all subsequent page numbers in the document.

This shows how to generate a tabular-style index with the information (I think!) you want. I'm a bit confused about the relationship between the output you show and the input you have, so I'm not certain how many distinct pieces of information there are for each individual role. I've assumed three (when role name and macro name coincide) or four (otherwise) and provide two commands for defining them.

  • \participant[<role name>]{<macro without backslash>}{<shorthand>}{<description>}
  • \participants{<comma-separated list} where each item in the list consists of three or four parts separated by |: <macro name without backslash>|<shorthand>|<description> or <role name>|<macro name>|<shorthand>|<description>. Elements may include commas if the elements are enclosed in curly brackets.
  • tabindex is an environment used to produce the actual table. This currently has at least two problems.
    • First, it isn't the target output. That's just a question of formatting a table, so there are any number of questions you can use for that. Just adjust the definition of tabindex. You shouldn't need to fiddle with the .ist.
    • Second, it includes an ugly hack to eliminate a superfluous row. This may need adjusting if your font, spacing or tabular config changes.

The file will also spit out roleindex.ist, which is used to create the index with makeindex -s roleindex.ist <filename> before recompilation.

\begin{filecontents}[overwrite]{roleindex.ist}
%% ref.: imakeindex.pdf 16
preamble "\n\\\begin{tabindex}{LLL}{\\\bfseries Role & \\\bfseries Description & \\\bfseries  Pages}"
postamble "\\\\\n\\\end{tabindex}\n" 
headings_flag 0
item_0 "\\\\\n"
delim_0 " & "
group_skip ""
\end{filecontents}

\documentclass{article}
% ateb: https://tex.stackexchange.com/a/714141/ addaswyd o gwestiwn TheSaltyPug: https://tex.stackexchange.com/q/714132/
\usepackage{makeidx}
\usepackage{array}
\usepackage{tabularx}
\newcolumntype{L}{>{\sffamily}l}
\usepackage{booktabs}
\makeindex
\NewDocumentEnvironment{tabindex}{mm+b}
{%
  \section*{\indexname}%
  \begin{center}
    \begin{tabularx}{\linewidth}{#1}
      \toprule 
      #2 \\
      \midrule
      \\[-5ex]
      #3
      \bottomrule
    \end{tabularx}%
  \end{center}%
}{}
\ExplSyntaxOn
\seq_new:N \l__rolesindex_tmpa_seq
\tl_new:N \l__rolesindex_tmpa_tl
\tl_new:N \l__rolesindex_tmpb_tl
\tl_new:N \l__rolesindex_tmpc_tl
\tl_new:N \l__rolesindex_tmpd_tl
\cs_new_protected:Nn \rolesindex_participant:nnnn 
{
  \ExpandArgs {c} \NewDocumentCommand { #2 } {}  { \index {#1~(#3)~& ~#4} #3 }
}
\NewDocumentCommand \participant   { O {\text_titlecase_all:n {#2}} m m m }
{
  \ExpandArgs {c} \NewDocumentCommand { #2 } {}  { \index {#1~(#3)~& ~#4} #3 }
}
\cs_generate_variant:Nn \rolesindex_participant:nnnn { VVVV }
\NewDocumentCommand \participants { m }
{
  \clist_map_inline:nn { #1 }
  {
    \seq_set_split:Nnn \l__rolesindex_tmpa_seq { | } { ##1 }
    \seq_pop_right:NN \l__rolesindex_tmpa_seq \l__rolesindex_tmpd_tl
    \seq_pop_right:NN \l__rolesindex_tmpa_seq \l__rolesindex_tmpc_tl
    \seq_if_empty:NTF \l__rolesindex_tmpa_seq 
    {
      \tl_set_eq:NN \l__rolesindex_tmpb_tl \l__rolesindex_tmpd_tl
    }{
      \seq_pop_right:NN \l__rolesindex_tmpa_seq \l__rolesindex_tmpb_tl    
    }
    \seq_if_empty:NTF \l__rolesindex_tmpa_seq 
    {
      \tl_set:Ne \l__rolesindex_tmpa_tl { \text_titlecase_all:n { \l__rolesindex_tmpb_tl } }
    }{
      \seq_pop_right:NN \l__rolesindex_tmpa_seq \l__rolesindex_tmpa_tl
    }
    \rolesindex_participant:VVVV \l__rolesindex_tmpa_tl \l__rolesindex_tmpb_tl \l__rolesindex_tmpc_tl \l__rolesindex_tmpd_tl
  }
}
\ExplSyntaxOff
% PARTICIPANTS {as commands so I can reference them easily}
\participant{celebrant}{CEL}{Foo}
\participant[Mas.~of Cer.]{mco}{MC1}{Baz}
\participants{% equivalent to multiple uses of \participant
  cantor|CANTOR|Bar,
  Mas.~of Cer.|mct|MC2|Foo-Bar,
  Server 1|serveri|S1|{Crucifix, book},
  Server 2|serverii|S2|{Candle, paten},
  Server 3|serveriii|S3|,
  Server 4|serveriv|S4|{Altar setup, bells},
  Server 5|serverv|S5|{Altar setup},
  Server 6|servervi|S6|,
}
\renewcommand \indexname{Participants}
\begin{document}


\printindex

\section{Begin}
Form the procession line with the help of \mct.
The order is:
\begin{enumerate}
  \item \servervi{} with Thurible
  \item \serveri{} with Cross, \serverii{} and \serveriii{} with torches on each side
  \item All other altar servers
  \item \mct{}
  \item Gospel
  \item \mco{}
  \item \celebrant{}
\end{enumerate}

\cantor{} is on vacation.

\section{Section Two}
Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.

\end{document}

Images of my output currently suck due to an Okular/KDE bug. My apologies.

[Attempted image deleted as worse than useless.]

Things I can't figure out:

  • how to get rid of the spurious row inserted before the first index entry;
    • According to the manual page item_0 is used between two top level items. Fine. The only problem is, it isn't just used there. It is also used before the first top level item and I cannot find a way to get rid of it.
  • how to use any command starting with a t in the .ist file.

enter image description here

8
  • 1
    You will need a specal .ist file to get the formatting right, Also modify the commands to also store description. See my comments under the OP question.
    – yannisl
    Mar 27 at 2:22
  • 1
    @yannisl Yes, I know. However, I took the main point of this question to concern the timing. Anyway, the .ist isn't turning out to be as difficult as I expected. The manual page is pretty good ;).
    – cfr
    Mar 27 at 3:04
  • 1
    It does get somewhat tricky if you also use hyperref
    – yannisl
    Mar 27 at 3:55
  • 1
    Oh yes, is not a problem for Heiko! But for me I always scratch my head!
    – yannisl
    Mar 27 at 4:03
  • 1
    Added the image, works also miraculously with no extra work for hyperref I just added usepackage{hyperref}.
    – yannisl
    Mar 27 at 8:56

You must log in to answer this question.

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