-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloze.sty
381 lines (380 loc) · 9.26 KB
/
cloze.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
%% cloze.sty
%% Copyright 2015-2023 Josef Friedrich
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Josef Friedrich.
%
% This work consists of the files cloze.lua, cloze.tex,
% and cloze.sty.
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{cloze}
[2020/06/30 v1.6 Package to typeset cloze worksheets or cloze tests]
%
% \subsection{The file \tt{cloze.sty}}
%
% \begin{macrocode}
\input{cloze.tex}
% \end{macrocode}
%
% \begin{macrocode}
\ExplSyntaxOn
% \end{macrocode}
%
% This packages are used to build \emph{cloze}:
%
% \subsubsection{Dependencies}
%
% The package \href{https://www.ctan.org/pkg/setspace}{setspace} is used
% by the environment |clozespace|.
% \begin{macrocode}
\RequirePackage{setspace}
% \end{macrocode}
%
% The package \href{https://www.ctan.org/pkg/xcolor}{xcolor} is required
% to colorize the text and the line of a gap.
% \begin{macrocode}
\RequirePackage{xcolor}
% \end{macrocode}
%
% The package \href{https://www.ctan.org/pkg/stackengine}{stackengine}
% is required by the command |\clozestrike{}{}|.
% \begin{macrocode}
\RequirePackage{stackengine}
% \end{macrocode}
%
% The package \href{https://www.ctan.org/pkg/ulem}{ulem} is required by
% the command |\clozestrike{}{}|.
% \begin{macrocode}
\RequirePackage{ulem}
\normalem
% \end{macrocode}
%
% \begin{macrocode}
\RequirePackage{transparent}
% \end{macrocode}
%
% \begin{macrocode}
\DeclareOption*{} % suppresses the error message: ! LaTeX Error: Unknown option
\ProcessOptions\relax
\RequirePackage{luakeys}
\directlua{
cloze.parse_options('\LuakeysGetPackageOptions', 'global')
}
% \end{macrocode}
%
% Load the cloze lua module and put all return values in the variable
% |cloze|.
%
% \begin{macro}{\clozesetoption}
% \cmd{\clozesetoption} is a wrapper for the Lua function
% |registry.set_option|. \cmd{\clozesetoption}\marg{key}\marg{value}
% sets a key \meta{key} to the value \meta{value}. The plain Lua\TeX{}
% version always sets to the global options. The Lua\LaTeX-version
% can set values both to the local and the global options store.
% \begin{macrocode}
\let\clozesetoption=\undefined
\newcommand{\clozesetoption}[2]
{
\directlua{cloze.set_option('#1', '#2')}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeSetLocalOptions}
% This macro is used in all cloze commands to handle the optional
% arguments.
% \begin{macrocode}
\def\ClozeSetLocalOptions#1
{
\directlua
{
cloze.parse_options('#1', 'local')
}
}
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Options}
%
% At start all values are declared as global options. At the Lua side
% all values are now stored in the |registry.global_options| table.
%
% \begin{macrocode}
\ClozeSetToGlobal
% \end{macrocode}
%
% \subsubsection{Public macros}
%
% All public macros are prefixed with |\cloze|.
%
% \begin{macro}{\clozeset}
% The usage of the command \cmd{\clozeset} is described in detail in
% section \TmpSecRef{sec:command-clozeset}.
% \begin{macrocode}
\newcommand{\clozeset}[1]
{
\directlua
{
cloze.parse_options('#1', 'global')
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozeshow}
% The usage of the command \cmd{\clozeshow} is described in detail in
% section \TmpSecRef{sec:command-clozeshow}.
% \begin{macrocode}
\let\clozeshow=\undefined
\newcommand{\clozeshow}{%
\clozeset{show}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozehide}
% The usage of the command \cmd{\clozehide} is described in detail in
% section \TmpSecRef{sec:command-clozehide}.
% \begin{macrocode}
\let\clozehide=\undefined
\newcommand{\clozehide}
{
\clozeset{hide}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cloze}
% The usage of the command \cmd{\cloze} is described in detail in
% section \TmpSecRef{sec:command-cloze}.
% \begin{macrocode}
\let\clozePLAIN=\cloze
\let\cloze=\undefined
\newcommand{\cloze}[2][]
{
\ClozeSetLocalOptions{#1}
\clozePLAIN{#2}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozefix}
% The usage of the command \cmd{\clozefix} is described in detail in
% section \TmpSecRef{sec:command-clozefix}.
% \begin{macrocode}
\let\clozefixPLAIN=\clozefix
\let\clozefix=\undefined
\newcommand{\clozefix}[2][]
{
\ClozeSetLocalOptions{#1}
\clozefixPLAIN{#2}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozenol}
% The usage of the command \cmd{\clozenol} is described in detail in
% section \TmpSecRef{sec:command-clozenol}.
% \begin{macrocode}
\let\clozenolPLAIN=\clozenol
\let\clozenol=\undefined
\newcommand{\clozenol}[2][]
{
\ClozeSetLocalOptions{#1}
\clozenolPLAIN{#2}
}
% \end{macrocode}
% \end{macro}
%
% \begin{environment}{clozepar}
% The usage of the environment |clozepar| is described in detail in
% section \TmpSecRef{sec:command-clozepar}.
% \begin{macrocode}
\newenvironment{clozepar}[1][]
{
\par
\ClozeSetLocalOptions{#1}
\ClozeStartMarker{par}
\clozefont\relax
}
{
\ClozeStopMarker{par}
\par
\directlua{cloze.unregister_callback('par')}
}
% \end{macrocode}
% \end{environment}
%
%
% \begin{macro}{\clozeparplain}
% The usage of the command \cmd{\clozeparplain} is described in detail in
% section \TmpSecRef{sec:command-clozeparplain}.
% \begin{macrocode}
\let\clozeparplainPLAIN=\clozeparplain
\let\clozeparplain=\undefined
\newcommand{\clozeparplain}[2][]
{
\ClozeSetLocalOptions{#1}
\clozeparplainPLAIN{#2}
}
% \end{macrocode}
% \end{macro}
%
% \begin{environment}{clozebox}
% The usage of the environment |clozebox| is described in detail in
% section \TmpSecRef{sec:command-clozebox}.
% For the box rule a \cmd{\fbox} is used. We store the original rule
% thickness and restore the dimension after the clozebox is set.
% The dimension |fboxrule| have to be set before the environment
% |lrbox|.
% \begin{macrocode}
\newsavebox{\ClozeBox}
\NewDocumentEnvironment{clozebox}{ s O{} +b }
{
\directlua
{
cloze.print_box('\luaescapestring{\unexpanded{#3}}','\luaescapestring{\unexpanded{#2}}','\luaescapestring{\unexpanded{#1}}')
}
}
{
\directlua
{
cloze.restore_fboxrule()
}
}
% \end{macrocode}
% \end{environment}
%
% \begin{environment}{clozespace}
% The usage of the environment |clozespace| is described in detail in
% section \TmpSecRef{sec:command-clozespace}.
% TODO: Realization without setspace package.
% \begin{macrocode}
\NewDocumentEnvironment { clozespace } { O {} }
{
\directlua{cloze.print_space('#1')}
}
{
\end{spacing}
}
% \end{macrocode}
% \end{environment}
%
% \begin{macro}{\clozeline}
% The usage of the command \cmd{\clozeline} is described in detail in
% section \TmpSecRef{sec:command-clozeline}.
% \begin{macrocode}
\let\clozelinePLAIN=\clozeline
\let\clozeline=\undefined
\newcommand{\clozeline}[1][]
{
\ClozeSetLocalOptions{#1}
\clozelinePLAIN
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozelinefil}
% The usage of the command \cmd{\clozelinefil} is described in detail in
% section \TmpSecRef{sec:command-clozelinefil}.
% \begin{macrocode}
\let\clozelinefilPLAIN=\clozelinefil
\let\clozelinefil=\undefined
\newcommand{\clozelinefil}[1][]
{
\ClozeSetLocalOptions{#1}
\clozelinefilPLAIN
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozefil}
% The usage of the command \cmd{\clozefil} is described in detail in
% section \TmpSecRef{sec:command-clozefil}.
% \begin{macrocode}
\let\clozefil=\undefined
\newcommand{\clozefil}[2][]
{
\cloze[#1]{#2}\clozelinefil[#1]
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozeextend}
% TODO: Use node library to create kern nodes.
% \begin{macrocode}
\newcommand{\clozeextend}[1][1]
{
\directlua
{
cloze.print_extension('#1')
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeTextColor}
% \begin{macrocode}
\newcommand{\ClozeTextColor}[1]
{
\FarbeTextColor{blue}{#1}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ClozeStrikeLine}
% \begin{macrocode}
\newcommand\ClozeStrikeLine
{
\bgroup
\markoverwith{
\ClozeTextColor{
\rule[0.5ex]{2pt}{1pt}
}
}
\ULon
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clozestrike}
% \begin{macrocode}
\newcommand{\clozestrike}[3][]{%
\ClozeSetLocalOptions{#1}%
\ifclozeshow%
\stackengine%
{\Sstackgap}% \Sstackgap or \Lstackgap or \stackgap or stacklength
{\ClozeStrikeLine{#2}}% anchor
{\ClozeTextColor{\clozefont{}#3}}% item
{O}% O or U
{c}% \stackalignment or l or c or r
{\quietstack}% \quietstack or T or F
{T}% \useanchorwidth or T or F
{\stacktype}% \stacktype or S or L
\else%
\stackengine%
{\Sstackgap}% \Sstackgap or \Lstackgap or \stackgap or stacklength
{#2}% anchor
{\texttransparent{0}{\clozefont{}#3}}% item
{O}% O or U
{c}% \stackalignment or l or c or r
{\quietstack}% \quietstack or T or F
{T}% \useanchorwidth or T or F
{\stacktype}% \stacktype or S or L
\fi%
}
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
\ExplSyntaxOff
% \end{macrocode}