我想知道,当使用“歌曲”包生成 pdf 文件时,是否可以按字母顺序排列歌曲,即使它们在源代码中以另一种顺序编写。作为源代码的最小工作示例,您可以考虑以下示例
http://songs.sourceforge.net/docs.html
在 songs.sbd 文件中,歌曲的书写顺序并不固定,但我希望最终的 pdf 文件中的歌曲是按字母顺序排列的。提前致谢。
弗拉维奥
答案1
概念证明
我从来没有用过songs
软件包,所以这是学习新知识的好机会。它部分安装在 TeX Live 发行版中。我已经安装了exe
来自http://songs.sourceforge.net/downloads.html在 Windows 上复制到一个临时文件夹(我没有使用 MiKTeX)。我已将其复制songidx.exe
到 Windows 可以找到的文件夹中,并将五个文件can
复制到我的工作目录(尤其是bible.can
对于该示例很重要),我还在其中添加了示例tex
文件(chordbook.tex
、lyricbook.tex
和)和文件。这些文件也可以从slidebook.tex
transparencies.tex
songs.sbd
http://songs.sourceforge.net/docs.html。
我准备了一个独立的 Lua 脚本(mal-sort-songs.lua
),用于重新排列歌曲。但是,完整的歌曲索引(chordbook.pdf
和中的第 1 页lyricbook.pdf
)使用的是index
参数,而不是\beginsong
命令后的第一个参数。它还考虑了二合字母和Ch
冠词。我不确定它是否考虑了冠词(我正在探索源代码),它没有。我们可以准备自己的歌曲索引,或者,我们可以相应地修改参数。好吧,内容按照 OP 的要求排序了。A
The
An
titleidx.c
index
我运行以下几行来获取所有四个版本(在 TeX 文件中我仅更改songs.sbd
为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
Lua 脚本向终端写入以下注释:
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
我附上了源代码和几页的预览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()
答案2
(想添加此评论但回复不够)
@Malipivo 的回答很棒,我发现第 41 行的轻微调整很有帮助:
table.sort(songs, function(a, b) return a:gsub('The ', ''):lower() < b:gsub('The ', ''):lower() end)
这会根据索引中的排序忽略“The”。