GeneralCreate alphabetical list of sections?

LaTeX specific issues not fitting into one of the other forums of this category.
bombcar
Posts: 22
Joined: Tue Oct 28, 2008 10:18 pm

Create alphabetical list of sections?

Post by bombcar »

I am using LaTeX and lilypond-book to make a hymnal, and I'm using \section commands to title each song. I'd like to make a list, similar to a table of contents (and an index) that lists each song alphabetically and then instead of the page number, shows the section number.

So instead of:

Code: Select all

1   Last of the Songs ........... 1
2   Song of the Lasts ........... 1
3   Example Song ................ 2
It would be more like an Index (but with TOC formatting), but with section numbers instead of pages:

Code: Select all

Example Song .................... 3
Last of the Songs ............... 1
Song of the Lasts ............... 2
Is it possible to do this? I'd really rather not manually do it in a table because I need to be able to move the songs around to make them fit correctly and having an automatically generated "List of Songs" would help considerably.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Create alphabetical list of sections?

Post by nlct »

This assumes that your toc file only contains lines in the form

Code: Select all

\contentsline {section}{\numberline {1}Last of the Songs}{1}
(That is, you don't have any \chapters or \addcontentsline etc)

The following is a minimal example:

Code: Select all

\documentclass{article}

\makeatletter
\newcommand{\makesongs}{%
  \makeindex
  \bgroup
     \def\indexsection\numberline##1##2\endindexsection{%
       \protected@write\@indexfile{}{\string\indexentry{##2}{##1}}}%
     \def\contentsline##1##2##3{\indexsection##2\endindexsection}%
     \@input@{\jobname.toc}%
  \egroup
}

\newcommand{\listofsongs}{\@input@{\jobname.ind}}
\makeatother

\makesongs

\begin{document}
\tableofcontents
\listofsongs

\section{Last of the Songs}

\section{Song of the Lasts}

\newpage

\section{Example Song}

\end{document}
If this file is called, say, mydoc.tex then \makesongs reads through mydoc.toc and extracts the sectioning information and adds the corresponding line to mydoc.ind. You can then use makeindex to sort this file into alphabetical order, but you need to use a customised makeindex style file to set it in the same style as the table of contents.

To do this create a file called, say, songs.ist that contains the following:

Code: Select all

preamble "\\section*{List of Songs}"
postamble ""
group_skip ""
item_0 "\n\\contentsline\{section\}\{"
delim_0 "\}\{"
delim_t "\}"
headings_flag 0
In order to get the complete document, you need to run latex twice followed by makeindex and then latex again:

Code: Select all

latex mydoc
latex mydoc
makeindex -s songs.ist mydoc.idx
latex mydoc
If the toc file contains lines that don't start with \contentsline{section} then \makesongs would need to be modified so that it checked the start of each line which is a little more complicated.

Hope that helps

Regards
Nicola Talbot
bombcar
Posts: 22
Joined: Tue Oct 28, 2008 10:18 pm

Re: Create alphabetical list of sections?

Post by bombcar »

This looks wonderful. I'll give it a go and see what I can do.
bombcar
Posts: 22
Joined: Tue Oct 28, 2008 10:18 pm

Create alphabetical list of sections?

Post by bombcar »

This works almost perfectly, however, I've run into one error. One of my songs has an exclamation point in the title, and this causes the indexing to break because an exclamation point is used by the index to mark a subtopic.

Any idea on how I could escape out the exclamation point in this case?

Code: Select all

\section{Alas! It Worketh Not}
throws the error.
bombcar
Posts: 22
Joined: Tue Oct 28, 2008 10:18 pm

Re: Create alphabetical list of sections?

Post by bombcar »

Also, I cannot get the \listofsongs to show without the \tableofcontents line - in which case it prints Contents and List of Songs, whereas I only want the List of Songs. I assume there's someway to run the TOC work without printing the TOC.
bombcar
Posts: 22
Joined: Tue Oct 28, 2008 10:18 pm

Create alphabetical list of sections?

Post by bombcar »

The only other thing I'd like to do is modify the
\section{Last of the Songs}
command such that I could do both:

Code: Select all

\songsection{Last of the Songs}

\songsection{The Last Song}{Last Song}
This would then allow the index to ignore the "The"s in the songtitles.

I think I can figure this out (perhaps).
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Create alphabetical list of sections?

Post by nlct »

bombcar wrote:This works almost perfectly, however, I've run into one error. One of my songs has an exclamation point in the title, and this causes the indexing to break because an exclamation point is used by the index to mark a subtopic.

Any idea on how I could escape out the exclamation point in this case?

Code: Select all

\section{Alas! It Worketh Not}
throws the error.
You can escape it with the double quote character. For example:

Code: Select all

\section[Alas"! It Worketh Not]{Alas! It Worketh Not}
Alternatively, you can modify the makeindex style file to use a different special character for the level indicator. For example, add the following line to songs.ist (or whatever you called it):

Code: Select all

level '>'
Make sure you choose a character that won't appear in any of the song titles.

Regards
Nicola Talbot
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Create alphabetical list of sections?

Post by nlct »

bombcar wrote:Also, I cannot get the \listofsongs to show without the \tableofcontents line - in which case it prints Contents and List of Songs, whereas I only want the List of Songs. I assume there's someway to run the TOC work without printing the TOC.
The following modification to \listofsongs uses some of the code used by \tableofcontents, so it should fix it:

Code: Select all

\newcommand{\listofsongs}{%
\begingroup
  \@input@{\jobname.ind}%
  \if@filesw
    \expandafter\newwrite\csname tf@toc\endcsname
    \immediate\openout\csname tf@toc\endcsname\jobname.toc\relax
  \fi
  \@nobreakfalse
\endgroup}
Regards
Nicola Talbot
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Create alphabetical list of sections?

Post by nlct »

bombcar wrote:The only other thing I'd like to do is modify the
\section{Last of the Songs}
command such that I could do both:

Code: Select all

\songsection{Last of the Songs}

\songsection{The Last Song}{Last Song}
This would then allow the index to ignore the "The"s in the songtitles.

I think I can figure this out (perhaps).

Perhaps something like:

Code: Select all

\makeatletter
\newcommand*{\songsection}[2][]{%
  \def\thesongtitle{#1}%
  \ifx\thesongtitle\@empty
    \section{#2}%
  \else
    \section[#1@#2]{#2}%
  \fi
}
\makeatother
Then do

Code: Select all

\songsection[Last Song]{The Last Song}
Regards
Nicola Talbot
bombcar
Posts: 22
Joined: Tue Oct 28, 2008 10:18 pm

Re: Create alphabetical list of sections?

Post by bombcar »

Thank you very much! That does exactly what I need.

When I'm done I'll post the bare minimum template used for lilypond-book.
Post Reply