Debian Codegraph @

texmf-dist/tex/luatex/lyluatex/lyluatex.sty

raw
1 %Lyluatex LaTeX style.
2 %
3 % Copyright (C) 2015-2026 jperon and others (see CONTRIBUTORS.md)
4 % License: MIT
5 % This file is part of lyluatex.
6
7 \NeedsTeXFormat{LaTeX2e}%
8 \ProvidesPackage{lyluatex}[2026/05/26 v1.1.6] %%LYLUATEX_DATE LYLUATEX_VERSION
9
10 % Dependencies
11 \RequirePackage{graphicx}
12 \RequirePackage{minibox}
13 \RequirePackage{environ}
14 \RequirePackage{currfile}
15 \RequirePackage{pdfpages}
16 \IfFileExists{varwidth.sty}{\RequirePackage{varwidth}}{}
17 \RequirePackage{luaoptions}
18
19 \RequirePackage{metalogo}
20 \newcommand{\lyluatex}{\textit{ly}\LuaTeX}
21
22 \edef\ly@false{false}\def\ly@istwosided{\if@twoside\else\ly@false\fi}
23 \savecatcodetable 40
24
25 % Copied from ifnextok.sty.
26 % We use \providecommand instead of \newcommand and \def in order
27 % to avoid overriding ifnextok if it is already loaded.
28 \providecommand{\IfNextToken}[3]{%
29 \let\nextok@match= #1%
30 \def\nextok@if{#2}\def\nextok@else{#3}%
31 \futurelet\@let@token\nextok@decide%
32 }
33 \providecommand\nextok@decide{%
34 \ifx\@let@token\nextok@match
35 \expandafter\nextok@if
36 \else
37 \expandafter\nextok@else
38 \fi%
39 }
40
41 % Options
42 \catcode`-=11
43 \directlua{
44 local _opt = lua_options
45 lua_options.register('ly', {
46 ['addversion'] = {'false', 'true', ''},
47 ['autoindent'] = {'true', 'false', ''},
48 ['cleantmp'] = {'false', 'true', ''},
49 ['currfiledir'] = {},
50 ['debug'] = {'false', 'true', ''},
51 ['extra-bottom-margin'] = {'0', _opt.is_dim},
52 ['extra-top-margin'] = {'0', _opt.is_dim},
53 ['fix_badly_cropped_staffgroup_brackets'] = {'false', 'true', ''},
54 ['nofix_badly_cropped_staffgroup_brackets'] = {'default', _opt.is_neg},
55 ['force-compilation'] = {'false', 'true', ''},
56 ['fragment'] = {'', 'false', 'true'},
57 ['nofragment'] = {'default', _opt.is_neg},
58 ['fullpagealign'] = {'crop', 'staffline'},
59 ['fullpagestyle'] = {''},
60 ['gutter'] = {'.4in', _opt.is_dim},
61 ['exampleindent'] = {'gutter', _opt.is_alias},
62 ['leftgutter'] = {'', _opt.is_dim}, ['rightgutter'] = {'', _opt.is_dim},
63 ['hpadding'] = {'0.75ex', _opt.is_dim},
64 ['include_after_body'] = {'false'},
65 ['include_before_body'] = {'false'},
66 ['include_footer'] = {'false'},
67 ['include_header'] = {'false'},
68 ['includepaths'] = {'./'},
69 ['indent'] = {'', _opt.is_dim},
70 ['noindent'] = {'default', _opt.is_neg},
71 ['insert'] = {'', 'systems', 'fullpage', 'inline', 'bare-inline'},
72 ['intertext'] = {''},
73 ['label'] = {'false'}, ['labelprefix'] = {'ly_'},
74 ['line-width'] = {[[\linewidth]], _opt.is_dim},
75 ['ly-version'] = {'2.18.2'},
76 ['max-protrusion'] = {[[\maxdimen]], _opt.is_dim},
77 ['max-left-protrusion'] = {'', _opt.is_dim},
78 ['max-right-protrusion'] = {'', _opt.is_dim},
79 ['noclef'] = {'false', 'true', ''},
80 ['nostaff'] = {'false', 'true', ''},
81 ['nostaffsymbol'] = {'false', 'true', ''},
82 ['notime'] = {'false', 'true', ''},
83 ['notiming'] = {'false', 'true', ''},
84 ['notimesig'] = {'false', 'true', ''},
85 ['optimize-pdf'] = {'false', 'true', ''},
86 ['paperwidth'] = {[[\paperwidth]], _opt.is_dim},
87 ['paperheight'] = {[[\paperheight]], _opt.is_dim},
88 ['papersize'] = {'false'},
89 ['pass-fonts'] = {'false', 'true', ''},
90 ['current-font'] = {}, ['current-font-as-main'] = {'false', 'true', ''},
91 ['rmfamily'] = {}, ['sffamily'] = {}, ['ttfamily'] = {},
92 ['print-page-number'] = {'false', 'true', ''},
93 ['first-page-number'] = {'false', ''},
94 ['print-first-page-number'] = {'true', 'false', ''},
95 ['print-only'] = {''},
96 ['do-not-print'] = {''},
97 ['printfilename'] = {'false', 'true', ''},
98 ['program'] = {'lilypond'},
99 ['protrusion'] = {'', _opt.is_dim},
100 ['noprotrusion'] = {'default', _opt.is_neg},
101 ['raw-pdf'] = {'false', 'true', ''},
102 ['quote'] = {'false', 'true', ''},
103 ['ragged-right'] = {'default', 'true', 'false', ''},
104 ['noragged-right'] = {'default', _opt.is_neg},
105 ['relative'] = {'false', _opt.is_num},
106 ['norelative'] = {'default', _opt.is_neg},
107 ['showfailed'] = {'false', 'true' ,''},
108 ['staffsize'] = {'0', _opt.is_dim},
109 ['inline-staffsize'] = {'0', _opt.is_dim},
110 ['system-count'] = {'0', _opt.is_dim},
111 ['tmpdir'] = {'tmp-ly'},
112 ['twoside'] = {'\ly@istwosided', 'false', 'true', ''},
113 ['verbatim'] = {'false', 'true', ''},
114 ['voffset'] = {'0pt', _opt.is_dim},
115 ['valign'] = {'center', 'top', 'bottom'},
116 ['write-headers'] = {'false'},
117 % MusicXML options
118 ['absolute'] = {'false', 'true', ''},
119 ['language'] = {'false'},
120 ['lxml'] = {'false', 'true'},
121 ['no-articulation-directions'] = {'true', 'false', ''},
122 ['no-beaming'] = {'true', 'false', ''},
123 ['no-page-layout'] = {'true', 'false', ''},
124 ['no-rest-positions'] = {'true', 'false', ''},
125 ['verbose'] = {'false', 'true', ''},
126 ['xml2ly'] = {'musicxml2ly'},
127 })
128 }
129 \directlua{
130 ly = require(kpse.find_file("lyluatex.lua") or "lyluatex.lua")
131 ly.make_list_file()
132 if lua_options.client('ly').cleantmp then
133 luatexbase.add_to_callback('stop_run', ly.clean_tmp_dir, 'lyluatex cleantmp')
134 luatexbase.add_to_callback('stop_run', ly.conclusion_text, 'lyluatex conclusion')
135 end
136 }
137 \catcode`-=12
138
139 %\directlua{ly.TWOSIDE = 'f'}
140
141 \newcommand{\ly@setunits}{%
142 \let\ly@old@in\in\protected\def\in{in}%
143 \let\ly@old@pt\pt\protected\def\pt{pt}%
144 \let\ly@old@mm\mm\protected\def\mm{mm}%
145 \let\ly@old@cm\cm\protected\def\cm{cm}%
146 \let\ly@old@hfuzz\hfuzz\setlength{\hfuzz}{\maxdimen}%
147 }
148 \newcommand{\ly@resetunits}{%
149 \let\in\ly@old@in%
150 \let\pt\ly@old@pt%
151 \let\mm\ly@old@mm%
152 \let\cm\ly@old@cm%
153 \setlength{\hfuzz}{\ly@old@hfuzz}%
154 }
155
156 % How the filename of a score will look like (if printed)
157 \newcommand{\lyFilename}[1]{\noindent #1\par\bigskip}
158
159 % Appearance of verbatim 'intertext' (if printed)
160 \newcommand{\lyIntertext}[1]{\noindent #1\par\bigskip}
161
162 % Appearance of LilyPond version (if printed)
163 \newcommand{\lyVersion}[1]{\noindent {\footnotesize\emph{(GNU LilyPond #1)}\par}\bigskip}
164
165 % Retrieve the three main font families (rm, sf, tt)
166 % and store them as options. Additionally store the
167 % *current* font for optional use.
168 \newcommand{\ly@currentfonts}{%
169 \begingroup%
170 \setluaoption{ly}{current-font}{%
171 \directlua{ly.get_font_family(font.current())}%
172 }%
173 \rmfamily \edef\rmfamilyid{\fontid\font}%
174 \sffamily \edef\sffamilyid{\fontid\font}%
175 \ttfamily \edef\ttfamilyid{\fontid\font}%
176 % Set font families to those of the document
177 % that haven't been set explicitly as options.
178 \directlua{ly.set_fonts(\rmfamilyid, \sffamilyid, \ttfamilyid)}%
179 \endgroup%
180 }
181
182 % Main commands
183 % Score processing
184 \newcommand*{\ly@compilescore}[1]{%
185 \ly@setunits%
186 \setluaoption{ly}{currfiledir}{\currfiledir}%
187 \setluaoption{ly}{twoside}{\ly@istwosided}%
188 \directlua{
189 #1
190 ly.newpage_if_fullpage()
191 }%
192 \ly@resetunits%
193 \ly@currentfonts%
194 \directlua{ly.score:process()}%
195 }
196
197 % Inclusion of a .ly file
198 \newcommand*\includely[2][]{%
199 \directlua{ly.state = 'file'}%
200 \ly@compilescore{ly.file(
201 '\luatexluaescapestring{#2}', [[#1]]
202 )}%
203 }
204
205 % Inclusion of a musicxml file
206 \newcommand*\musicxmlfile[2][]{%
207 \directlua{ly.state = 'file'}%
208 \ly@compilescore{ly.file_musicxml(
209 '\luatexluaescapestring{#2}', [[#1]]
210 )}
211 }
212
213 % Base environments to include a LilyPond fragment integrated into
214 % the document.
215 \newcommand\lyscorebegin{\directlua{ly.buffenv_begin()}}
216 \newcommand\lyscoreend{\directlua{ly.buffenv_end()}}
217 \newenvironment{ly@bufferenv}{%
218 \directlua{
219 ly.insert_inline = string.match([[\options]], 'insert.*inline')
220 if ly.insert_inline then
221 if ly.varwidth_available then
222 tex.print([[
223 \string\begin{varwidth}{\string\linewidth}
224 ]])
225 else
226 ly.insert_inline = false
227 ly.err(
228 [[You have required 'insert=inline' with lilypond environment,
229 but package 'varwidth' wasn't found; either install it, or disable
230 this option.]]
231 )
232 end
233 end
234 }
235 \lyscorebegin%
236 }{%
237 \lyscoreend%
238 \ly@compilescore{ly.fragment(ly.score_content, [[\options]])}%
239 \hspace{0pt}\\
240 \directlua{
241 if ly.insert_inline then tex.print([[\string\end{varwidth}]]) end
242 }%
243 }
244
245 \NewEnviron{ly@compilely}{%
246 \ly@compilescore{ly.fragment(
247 '\luatexluaescapestring{\unexpanded\expandafter{\BODY}}',
248 [[\options]]
249 )}%
250 }
251
252 % Commands to print verbatim content of the score
253 \newcommand\lysetverbenv[2]{%
254 \directlua{ly.verbenv = {
255 '\luatexluaescapestring{\detokenize{#1}}',
256 '\luatexluaescapestring{\detokenize{#2}}'
257 }}%
258 }
259
260 % Environments to record custom headers and footers to be included in fragments
261 \newenvironment{lysavefrag}[1]{%
262 \edef\filename{#1}
263 \lyscorebegin%
264 }{%
265 \lyscoreend%
266 \directlua{ly.write_to_file('\filename'..'.ly', table.concat(ly.score_content,'\string\n'))}%
267 }
268
269 % Commands to transform or define lilypond environments so that it isn't necessary to add empty [].
270 \def\lyenv#1{%
271 \expandafter\let\csname ly@env@#1\expandafter\endcsname\csname #1\endcsname%
272 \expandafter\let\csname ly@env@end#1\expandafter\endcsname\csname end#1\endcsname%
273 \expandafter\def\csname #1\endcsname{\IfNextToken[{\csname ly@env@#1\endcsname}{\csname ly@env@#1\endcsname[]}}%
274 }
275 \long\def\lynewenvironment#1{\@ifnextchar[{\ly@newenv@a{#1}}{\ly@newenv@a{#1}[0]}}
276 \long\def\ly@newenv@a#1[#2]{\@ifnextchar[{\ly@newenv@b{#1}{#2}}{\ly@newenv@b{#1}{#2}[]}}
277 \long\def\ly@newenv@b#1#2[#3]#4#5{%
278 \newenvironment{#1}[#2][#3]{#4}{#5}
279 \lyenv{#1}
280 }
281
282 % Parametrized command and environment for included LilyPond fragment
283 \lynewenvironment{ly}[1][noarg]{%
284 \edef\options{#1}%
285 \directlua{ly.state = 'env'}%
286 \ly@bufferenv%
287 }{%
288 \endly@bufferenv%
289 }
290
291
292 \newcommand{\lily}[2][]{%
293 \edef\options{#1}%
294 \let\ly@oldrepeat\repeat\def\repeat{}% Fix #51
295 \directlua{ly.state = 'cmd'}%
296 \begin{ly@compilely}%
297 #2
298 \end{ly@compilely}%
299 \let\repeat\ly@oldrepeat%
300 }
301
302 \newcommand{\lyscore}[1]{\directlua{
303 local i = tonumber('#1') or '#1'
304 if i == '' then i = 1 end
305 tex.sprint(ly.score[i] or '')
306 }}
307
308 % Commands for compatibility with lilypond-book
309 \let\lilypondfile\includely%
310 \protected\def\lilypond{%
311 \def\reserved@a{lilypond}%
312 \ifx\reserved@a\@currenvir\expandafter\ly%
313 \else\expandafter\lily\fi%
314 }%
315 \let\endlilypond\endly