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?).

2
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.
    – user11232
    Oct 9, 2013 at 23:24
  • perhaps processing it like a csv database? If so, check the docsvlist and forcsvlist commands from the etoolbox package (see also this answer)
    – henrique
    Oct 9, 2013 at 23:25

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.