# Assign categories to subsection, the create list of categories (like list of figures)

Basically I'm using LaTeX to organize my sheet music. Each piece of music is a subsection, which shows up in the ToC.

What I would like to do is assign a category to each piece of music and then organize these categories in a "List of Categories" at the front of the document much like the "List of Figures".

I haven't found a way to do this, and am wondering if this community could help. I'm competent with LaTeX but am not experienced with making my own commands. Hoping there's a package that could help with this.

EDIT: Per Sean's request, here's an example of what I'd like to see:

# List of Levels

## Level 6

.1 Prelude in C Minor, Op. 28, no. 20 ........ page #

.2 Sonata in G Major, L 84, K 63 ................ page #

## Level 8

.1 Consolations ......................................... page #

Where "Prelude in C Minor, Op. 28, no. 20" is the subsection title. It would be nice to include the section title (which is the composer), but that's not necessary.

What I'd like to do is add a \level{x} command at the end of the \subsection{} command in order to generate this list, where "Level" is the category I'd like to sort by.

EDIT2: Here's a MWE to show the structure of the document. I'd like to add a sort level to each of the subsections to get the "List of Levels." A PDF is added at the end of each subsection using \includepdf{}.

\documentclass[twoside]{book}
\begin{document}

\pagenumbering{roman}
\tableofcontents
\pagenumbering{arabic}

\part{Sheet Music}
\chapter{Classical}
%%%%%%%%%%%%%%  Sections by composer
\section{Bach, Johann Sebastian}
\clearpage

\section{Beethoven, Ludwig van}
\clearpage
\chapter{Romantic}
\section{Debussy, Claude}
\clearpage
\section{Schumann, Robert}

\end{document}

• I would suggest using the titlesec package (or etoolbox) to add a hook to \subsection, and then try to emulate toc's behavior. (I'm sure there's a question on that bit somewhere on this site...) – Sean Allred Aug 26 '13 at 16:56
• Can you mock-up what you would like to see as the list of categories? Maybe a minimal use case? – Sean Allred Aug 26 '13 at 17:04
• I'd better see an index for this. – egreg Aug 26 '13 at 17:27
• @egreg as in incorporate this into the index itself? I think they would serve two separate goals; one sorting alphabetically to find something quickly (as an index) and one sorting by level (semantically). But that begs the question, chasley: how is your document organized? Can you add an MWE with an idealized use case? – Sean Allred Aug 26 '13 at 17:33
• @SeanAllred is correct. I want to have the TOC, which is organized by composer in alphabetical order, as well as this "List of Levels", which allows me to quickly pull up a piece based on its difficulty. I'll pull together an MWE for this. – chasely Aug 26 '13 at 17:45

To solve this problem I had to create a new index on my own, closely following book's table of contents. We will name her table of levels and her basic functioning is the same as her mother. This works at least for class book so far:

\documentclass{book}

\makeatletter

\newcommand{\levelsname}[1]{\def\@levelsname{#1}}

\newcommand{\levelsorder}[1]{%
\let\@levels\empty
\change@order #1,\@end
}

\def\change@order#1,{%
\@ifnextchar\@end{}{\change@order}
}

\let\@end\empty
\let\@levels\empty

\newcommand\tableoflevels{%
\@levels
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\@levelsname
\@mkboth{%
\MakeUppercase\@levelsname}{\MakeUppercase\@levelsname}}%
\@starttoc{lvl}%
\if@restonecol\twocolumn\fi
}

\def\check@level#1{%
\@ifnextchar[{\set@level{#1}}{}
}

\def\set@level#1[#2]{%
\expandafter\protected@edef\csname @#1@pag\endcsname{\thepage}
\@ifundefined{@#2}{%
\expandafter\gdef\csname @#2\endcsname{%
{\protect\contentsline{section}{#1}{\csname @#1@pag\endcsname}}}
\csname @#2\endcsname
}{%
{\protect\contentsline{section}{#1}{\csname @#1@pag\endcsname}}}
}
}

\let\@@subsection\subsection

\def\subsection{%
\@ifnextchar[{\get@optional}{\get@subsection}
}

\def\get@optional[#1]#2{%
\@@subsection[#1]{#2}
\check@level{#1}
}

\def\get@subsection#1{%
\@@subsection{#1}
\check@level{#1}
}

\makeatother

\begin{document}

\tableofcontents

\chapter{Classical}
\section{Bach, Johann Sebastian}
\clearpage
\subsection[Prelude]{Prelude BWV 846}[Level 1]
\clearpage
\section{H\"andel, Georg Friedrich}
\clearpage
\subsection{Dettingen Te Deum}
\clearpage
\section{Beethoven, Ludwig van}
\clearpage
\subsection{Pathetique}[Level 2]
\chapter{Romantic}
\section{Debussy, Claude}
\clearpage
\subsection{Suite Bergamasque}[Level 1]
\clearpage
\section{Brahms, Johannes}
\clearpage
\subsection{Schicksalslied}

\levelsname{Levels}            %   Sets table's title
\levelsorder{Level 1,Level 2}  %   [OPTIONAL] Changes levels' order in the table
\tableoflevels                 %   Prints the table

\end{document}


List of commands:

• \levelsname{<title of the table>} just gives your new index a title

• \tableoflevels just prints the index

• \levelsorder{<titles included>} sets the correct order of inserted level (default is the order of appearance). They have to be inserted separated by commas and they have to be exactly spelled.

To put items and levels in the index is enough to put an extra optional argument after subsection's title, adding his index section, thus resulting in
\subsection[<index-shown title>]{<title>}[<index section>].
It is all shown in exaple code.