5

I was wondering if it's possible to order the songs in alphabetical when generate the pdf file using the package "songs" also if they was written in another order in source code. As a Minimal Working Example of source code, you can consider the sample at

http://songs.sourceforge.net/docs.html

In the songs.sbd file the songs are written without a aspecific order, but i would like that in the final pdf file the song are in alpabetical order. Thanks in advance.

Flavio

4
  • Are you using the songs package? I don't think this is possible.
    – Alan Munn
    Commented May 19, 2014 at 18:27
  • If you are willing to put each song in a file of its own this is relatively easy. Commented May 19, 2014 at 18:31
  • 2
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Commented May 19, 2014 at 19:19
  • My first impression is that you need to rearrange sbd file by the first argument of the \beginsong command which starts by this command and ends by this command of the next song or by the end of the file. All this with the respect to the English sorting rules.
    – Malipivo
    Commented May 22, 2014 at 11:40

2 Answers 2

7

A proof of concept

I've never used songs package before, so it was a great opportunity to learn something new. It's partly installed in the TeX Live distribution. I've installed exe file from http://songs.sourceforge.net/downloads.html on Windows to a temporary folder (I'm not using MiKTeX). I've copied songidx.exe to a folder where Windows can find it and five can files to my working directory (esp. bible.can is important for that example) where I've also added example tex files (chordbook.tex, lyricbook.tex, slidebook.tex and transparencies.tex) and songs.sbd file in addition to that. Those files are downloadable also from http://songs.sourceforge.net/docs.html.

I've prepared a standalone Lua script (mal-sort-songs.lua) which is rearranging songs. However, a complete index of songs (page 1 in chordbook.pdf and lyricbook.pdf) is using index parameter instead of the first parameter after the \beginsong command. It also takes digraph Ch and articles A and The into consideration. I'm not sure if it considers An article (I'm exploring the titleidx.c source code), it is not. We could prepare our own index of songs, or, we could modify index parameters accordingly. Well, the content is sorted as the OP asked for.

I'm running the following lines to get all four versions (in TeX files I've only changed songs.sbd to songs-sorted.sbd):

texlua mal-sort-songs.lua songs.sbd songs-sorted.sbd

pdflatex chordbook.tex
songidx cbauth.sxd
songidx cbscrip.sxd
songidx cbtitle.sxd
pdflatex chordbook.tex

pdflatex lyricbook.tex
songidx lbauth.sxd
songidx lbscrip.sxd
songidx lbtitle.sxd
pdflatex lyricbook.tex

pdflatex slidebook.tex

pdflatex transparencies.tex

The Lua script writes to the terminal these notes:

Processing song from songs.sbd...
  1 Doxology
  2 And Can It Be
  3 My Jesus I Love Thee
  4 Amazing Grace
  5 It Is Well With My Soul
  6 What A Friend We Have In Jesus
  7 O Love That Will Not Let Me Go
  8 Come Thou Fount
  9 Holy Holy Holy
  10 How Great Thou Art
  11 Be Thou My Vision
  12 In The Garden
  13 Great Is Thy Faithfulness
  14 O The Deep Deep Love of Jesus
  15 Jesus Lover Of My Soul
  16 When I Survey The Wondrous Cross
  17 Crown Him With Many Crowns
  18 Fairest Lord Jesus
  19 Turn Your Eyes Upon Jesus
  20 Joyful Joyful We Adore Thee
  21 O For A Thousand Tongues To Sing
  22 All Glory, Laud, And Honor
  23 All Hail The Power Of Jesus Name
  24 Immortal Invisible
  25 Take My Life And Let It Be
  26 Christ The Lord Is Risen Today
  27 I Sing The Mighty Power Of God
  28 All Creatures Of Our God And King
  29 A Mighty Fortress Is Our God
Sorting songs...
Saving songs to songs-sorted.sbd...
  1 A Mighty Fortress Is Our God
  2 All Creatures Of Our God And King
  3 All Glory, Laud, And Honor
  4 All Hail The Power Of Jesus Name
  5 Amazing Grace
  6 And Can It Be
  7 Be Thou My Vision
  8 Christ The Lord Is Risen Today
  9 Come Thou Fount
  10 Crown Him With Many Crowns
  11 Doxology
  12 Fairest Lord Jesus
  13 Great Is Thy Faithfulness
  14 Holy Holy Holy
  15 How Great Thou Art
  16 I Sing The Mighty Power Of God
  17 Immortal Invisible
  18 In The Garden
  19 It Is Well With My Soul
  20 Jesus Lover Of My Soul
  21 Joyful Joyful We Adore Thee
  22 My Jesus I Love Thee
  23 O For A Thousand Tongues To Sing
  24 O Love That Will Not Let Me Go
  25 O The Deep Deep Love of Jesus
  26 Take My Life And Let It Be
  27 Turn Your Eyes Upon Jesus
  28 What A Friend We Have In Jesus
  29 When I Survey The Wondrous Cross

I enclose the source code and a preview of several pages from chordbook.pdf.

-- I am mal-sort-songs...
-- I sort songs in the sbd file...

local testing=arg[1] -- Where are my songs? 
-- For instance: 
-- texlua mal-sort-songs.lua songs.sbd songs-sorted.sbd

local l="" -- a variable for lines
local bs="" -- is what I am searching for \beginsong?
local s="" -- a variable for storing a single song
local songs={} -- a table with songs
local c=0 -- a song counter
function minsert()
  table.insert(songs,s)
end -- of function minsert

print("Processing song from "..arg[1].."...")
for line in io.lines(testing) do
l=line:sub(1,1)
bs=line:sub(1,10)
-- skip empty lines and lines starting with per cent sign
if l~="%" and l~="" then
  if bs==[[\beginsong]] then
    c=c+1
    ct=line:match("\\beginsong{(.*)}")
    print("  "..c.." "..ct)
    if c~=1 then minsert() end -- save a song to a table
    s=""
  end -- of if \beginsong...
  s=s..line.."\n"
end -- of skipping lines
end -- of for cycle (lines from the sbd file)
minsert() -- save last song to a table

whereto=io.open(arg[2],"w") -- The songs will be saved in the file.
print("Sorting songs...")
table.sort(songs) -- Sorting text fields...
print("Saving songs to "..arg[2].."...")
for i,song in ipairs(songs) do
  --print(song)
  whereto:write(song.."\n")
  ct=song:match("\\beginsong{(.-)}")
  print("  "..i.." "..ct)
end -- of for cycle for songs

-- Close the file and exit the script...
whereto:close()

mwe, close-up, page 1

mwe, part 1

mwe, part 2

0
2

(Would add this as comment but not enough Rep)

@Malipivo's answer is great, I found a slight adjustment at line 41 helpful: table.sort(songs, function(a, b) return a:gsub('The ', ''):lower() < b:gsub('The ', ''):lower() end) This ignores "The " as per the sorting in the indexes.

You must log in to answer this question.

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