7

I'm working on a song book using LaTeX. It's early in the project, and the songs aren't all picked out yet, but I thought I'd fix the inner workings of it in the meantime.
Since many people will (hopefully) be involved, and many songs included, I want to save all songs separately and \input them.
The dream would be to have a text file where the songs are listed like

Thrift shop-macLemore
Two worlds, one family-from Tarzan
Gagnam Style-Psy

and for me to be able to loop through the list and say (Sorry for mixing languages):

for(line l : index.txt) {
    String[] k = l.split('-');
    \includesong{k[0]}{k[1]}
}

Is there any good way to do this?

EDIT: I found a package called parselines that does the job fairly well. Especially combined with a little regex-fu (Why is there so little information on the internet about \def\foo#1, #2?).

Gustaphe
  • 299
  • 2
  • 13

1 Answers1

5
\documentclass{article}
\newread\sngs
\def\splitdash#1-#2\relax{%
\def\thissong{#1}%
\def\thissource{#2}%
}
\begin{document}
\def\zpar{\par}
\openin\sngs=songs.txt
\loop
\read\sngs to \tmp
\ifx\tmp\zpar\else
\expandafter\splitdash\tmp\relax
\input{\thissong}
\fi
\ifeof\sngs
\else
\repeat


\end{document}

songs.txt:

Thrift shop-macLemore
Two worlds, one family-from Tarzan
Gagnam Style-Psy

This tries to input the three titles (you might want to normalise the filenames, removing spaces or whatever)

! LaTeX Error: File `Thrift shop.tex' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)

Enter file name: 

! LaTeX Error: File `Two worlds, one family.tex' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)

Enter file name: 

! LaTeX Error: File `Gagnam Style.tex' not found.
David Carlisle
  • 757,742