Debian Codegraph @

texmf-dist/doc/latex/sdaps/html/_sources/sdapsbase.rst.txt

raw
1 .. _sdapsbase:
2
3 sdapsbase package
4 =================
5
6 This base package handles many of the core functionalities to make the SDAPS
7 class work (together with :environ:`sdapsarray`). It implements a number of features
8 which should however not be relevant for many end users. It should not be
9 neccessary to dive into the :ref:`sdapsbase <sdapsbase>` implementation unless you want to use
10 some of the more advanced features or even create completely custom layouts.
11
12
13
14 The context subsystem
15 ----------------------
16
17 SDAPS has a system to handle context for the questions. This context is managed
18 in the global TeX scope. This means that modifications done for example inside
19 a `multicols` environment will still be seen in the rest of the document. The
20 context system itself is also hirarchical, so that it is possible to define
21 different defaults for sections of the document. The most obvious use case for
22 this might be prefixing all variables within parts of the document with a
23 certain string. But one could also imagine changing the size of checkboxes
24 for parts of the document.
25
26 Handling context nesting
27 ^^^^^^^^^^^^^^^^^^^^^^^^
28
29 The following commands can be used to handle context nesting. Note that SDAPS
30 makes a best effort to detect errors where begin/end was not used in a balanced
31 fashion.
32
33 .. macro:: \sdaps_context_begin:n { context name }
34
35 Begins a context with the given name
36
37 .. macro:: \sdaps_context_end:n { context name }
38
39 Ends the context again, ensuring the name is correct
40
41 .. macro:: \sdaps_context_begin:
42
43 Begins a context with an empty name
44
45 .. macro:: \sdaps_context_end:
46
47 Ends a context started with :macro:`\\sdaps_context_begin:`
48
49 .. macro:: \sdaps_context_begin_local:
50
51 Begins a context which automatically ends together with the current TeX group.
52
53 .. macro:: \sdaps_context_enable_writing:
54
55 Enable metadata writing for the remainder of the current context. Note that
56 this package disables writing at start and you need to enable it before
57 calling :macro:`\\sdaps_begin:` (and :macro:`\\sdaps_end:`). Classes such as
58 the :ref:`sdapsclassic` will enable metadata writing for you.
59
60 .. macro:: \sdaps_context_disable_writing:
61
62 Disable metadata writing for the remainder of the current context. Note that
63 disabling metadata writing may have some side effects with regard to
64 variables and automatic numbering of questions and answers. The exact
65 behaviour is currently *not* well defined.
66
67 .. macro:: \sdaps_context_hook_end:n
68
69 Register a token list which will be executed at the end of the current
70 context. This is primarily useful as a context might be ended implicitly
71 in some circumstances.
72
73 .. macro:: \sdaps_context_hook_post_end:n
74
75 Register a token list which will be executed *after* the end of the current
76 context has ended (i.e. in the parent context). This is primarily useful
77 as a context might be ended implicitly in some circumstances.
78
79 Managing context variables
80 ^^^^^^^^^^^^^^^^^^^^^^^^^^
81
82 .. macro:: \sdaps_context_put:n { key }
83
84 Set the given variable (or key) to ``\\undefined``.
85
86 .. macro:: \sdaps_context_put:nn { key } { value }
87
88 Set the given variable to the given value (Variants: ``nV``)
89
90 .. macro:: \sdaps_context_remove:n { key }
91
92 Remove the given variable from the context.
93
94 .. macro:: \sdaps_context_set:n { key=value, key={a=b, c=d} }
95
96 Set context variables from the given ``key=value`` parameters
97
98 .. macro:: \sdaps_context_append:nn { key } { value }
99
100 The first argument being a variable to modify, append the given value to the
101 existing value. This can for example be used to change only one aspect of
102 checkbox drawing (e.g. ``form``) without affecting resetting another one
103 that was defined earlier (e.g. ``width`` or ``height``).
104
105 Generally it is a good idea to use this macro as nested options are common.
106
107 Identical to :macro:`\\sdaps_context_append:nnn` with "``,``" as the separator.
108
109 .. macro:: \sdaps_context_append:nnn { key } { value } { separator }
110
111 Append the value to the given ``key`` in the context. If ``key`` is set,
112 inserts ``separator`` between them. If key is not set, simply sets the
113 ``key`` to the given value.
114
115 :Variants: ``nVn``
116
117
118 Defining questions and headings
119 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120
121 .. macro:: \sdaps_qobject_begin:nnn { name } { type } { title }
122
123 Start a new qobject, giving the following arguments:
124
125 * The name of the context group to start
126 * The type of the qobject (to be consumed by the SDAPS main program)
127 * The title for the metadata
128
129 .. macro:: \sdaps_qobject_end:n { name }
130
131 Finish a qobject again, must pass the correct name to verify correct nesting.
132
133 .. macro:: \sdaps_qobject_begin:nn { type } { title }
134
135 Same as :macro:`\sdaps_qobject_begin:nnn` but without giving a context name.
136
137 .. macro:: \sdaps_qobject_begin_local:nn { type } { title }
138
139 Same as :macro:`\sdaps_qobject_begin:nnn` but will automatically end with
140 the current TeX group (using :macro:`\sdaps_context_begin_local:`).
141
142 .. macro:: \sdaps_qobject_end:
143
144 End a question which did not have a defined context name
145
146 You can write further metadata using the following macros:
147
148 .. macro:: \sdaps_qobject_append_var:n { var }
149
150 Appends the given string to the variable name of the question. An ``_``
151 will be used to separate the new variable name with any piece that was
152 defined earlier (either on the same question or on a surrounding question
153 object).
154
155 If ``var`` starts with an underscore (``_``) then an implicit variable name
156 for all surrounding question object (i.e. headings/sections) will be
157 generated based on their automatic numbering. This is similar to the
158 mechanism used by :environ:`choicearray` to ensure that the different questions
159 can always be told appart, even if the user did not specify a variable name
160 for all of them.
161
162 .. macro:: \sdaps_answer:n { answer text }
163
164 Write metadata for an answer which belongs to the current question (context)
165
166 .. macro:: \sdaps_range:nnn { lower|upper } { ID } { answer text }
167
168 Writes metadata for a range.
169
170 :arg lower|upper: Give either ``lower`` or ``upper`` for each end of the range.
171 :arg ID: The ID of the checkbox which corresponds to the first/lower or
172 last/upper box in the range. Other boxes are considered outside and need a
173 separate answer. Boxes are counted zero based and the given range is inclusive.
174 :arg answer text: The string for the metadata.
175
176 :Variants: ``nno``, ``nnf``, ``nnV``
177
178 Generic commands are also provided to write to
179
180 .. macro:: \sdaps_info_write:n { text }
181
182 Write given text to metadatafile at shipout. Some output may be reordered due
183 to this, but all SDAPS classes ensure that the metadata can still be decoded
184 correctly. As this macro leaves elements in the output stream it can affect
185 layouting in a few cases (e.g. row headers of :environ:`sdapsarray`).
186
187 The tokens **will not be expanded** again before writing. This implies that coordinates
188 cannot be written using this macro.
189
190 :arg text: Text to write to the metadata file.
191
192 :Variants: ``x``
193
194 .. macro:: \sdaps_info_write_x:n
195
196 Write given text to metadatafile at shipout. Some output may be reordered due
197 to this, but all SDAPS classes ensure that the metadata can still be decoded
198 correctly. As this macro leaves elements in the output stream it can affect
199 layouting in a few cases (e.g. row headers of :environ:`sdapsarray`).
200
201 The tokens **will be expanded** again before writing. This implies that coordinates
202 can be written using this macro if one takes care not to protect them from
203 being expanded at macro execution time.
204
205 :arg text: Text to write to the metadata file.
206
207 :Variants: ``x``
208
209 Overrides
210 ---------
211
212 Overrides allow setting defaults based on the questionnaire ID or based on the
213 variable name and value of items. Using this feature it would for example be
214 possible to fill in names into text fields for printing.
215
216 .. note::
217 Overrides are independent of the context itself.
218
219 Commands which adhere to overrides are currently:
220
221 * all ``multichoice`` or ``singlechoice`` checkboxes
222 * all ``textboxes``
223
224 There is only one command to set the overrides string:
225
226 .. macro:: \sdaps_set_questionnaire_id:n { ID }
227
228 Set the current questionnaire ID. This should generally not change unless
229 some sort of concatenation is done. It is only relevant for writing new
230 environments.
231
232 .. macro:: \sdaps_overrides_init:n { overrides }
233
234 :arg overrides: A key=value argument with all the override definitions.
235
236 Each of the override definitions will be appened to the items keys if it is
237 matching. Matching happens first based on the questionnaire ID with ``*``
238 being allowed as a wildcard, and then based on variable name and value. The
239 second level (name and value) is either just the variable name or the variable
240 name and value separated by an ``&`` character.
241
242 This gives six matches with increasing priority:
243
244 * wildcard questionnaire ID, wildcard target
245 * wildcard questionnaire ID, matching variable
246 * wildcard questionnaire ID, matching variable, matching value
247 * matching questionnaire ID, wildcard target
248 * matching questionnaire ID, matching variable
249 * matching questionnaire ID, matching variable, matching value
250
251 .. sdaps:: Overriding checkbox color and pre-filled value based on
252 questionnaire ID and variables.
253 :sdapsclassic:
254 :metadata:
255 :preamble:
256 \ExplSyntaxOn
257 \sdaps_overrides_init:n{
258 % For all questoinnaires independent of their ID
259 *={
260 % For all elements which use the overrides
261 *={fill=green},
262 % Specific element with that variable name
263 flower_bob_alice={draw_check=true},
264 % Specific element with variable "var" and value 1
265 var&1={draw_check=true},
266 },
267 % Specific questionnaire ID
268 testid={
269 % We need to explicitly unset it again!
270 flower_bob_alice={draw_check=false},
271 flower_adam_alice={draw_check=true},
272 },
273 }
274 \ExplSyntaxOff
275
276 \begin{choicegroup}[var=flower]{A group of questions with variable "flower"}
277 \groupaddchoice[var=alice]{Choice "alice"}
278 \groupaddchoice[var=eve]{Choice "eve"}
279 \choiceline[var=adam]{Question "adam"}
280 \choiceline[var=bob]{Question "bob"}
281 \end{choicegroup}
282
283 % Force a different questionnaire ID (never do this in a real document!)
284 \ExplSyntaxOn
285 \sdaps_set_questionnaire_id:n { testid }
286 \ExplSyntaxOff
287 \begin{choicegroup}[var=flower]{A group of questions with variable "flower"}
288 \groupaddchoice[var=alice]{Choice "alice"}
289 \groupaddchoice[var=eve]{Choice "eve"}
290 \choiceline[var=adam]{Question "adam"}
291 \choiceline[var=bob]{Question "bob"}
292 \end{choicegroup}
293
294
295 The rendering subsystem
296 -----------------------
297
298 Checkbox
299 ^^^^^^^^
300
301 =================== ===================================================================
302 Option Description
303 =================== ===================================================================
304 linewidth The width of the outline (default: 1bp)
305 width The width of the checkbox (default 3.5mm)
306 height The height of the checkbox (default: 3.5mm)
307 form The form, either `box` or `ellipse` (default: `box` for `multichoice` and `ellipse` for `singlechoice`)
308 value The value of the checkbox (for internal use)
309 fill The colour to fill the checkbox with (default: `white`)
310 draw The colour to draw the checkbox frame with (default: `.`, i.e. current text colour)
311 draw\_check Whether to draw a checkmark on top of the box (default: `false`)
312 centered\_text Text to overlay over the checkmark (default: `none`)
313 text Text to overlay over the checkmark using a minipage (default: `none`)
314 text\_align The minipage alignment of the overlay (default: `c`, valid are `c`, `t`, `b`, `s` for center, top, bottom or spread)
315 text\_padding The minipages padding from the outside border of the box (default: `2bp`)
316 ellipse Pass as a short form for `form=ellipse`
317 box Pass as a short form for `form=box`
318 =================== ===================================================================
319
320 There is only one user facing command to render a checkbox:
321
322 * `\\sdaps_checkbox:nn`
323 The following arguments can be given:
324
325 * Variable name
326 * Value
327
328 The behaviour of the checkbox should be changed through the context. On a first
329 level this works by using `\\sdaps_checkbox_set_type:n` and setting either
330 `singlechoice` or `multichoice`. Other than that you can also change the style
331 for `singlechoice` and `multichoice` through the context and overrides. The
332 following example demonstrates the use of context modification.
333
334 .. sdaps:: Example showing modification of the context for checkbox rendering
335
336 \noindent
337 \ExplSyntaxOn
338 \sdaps_checkbox:nn {} {}
339
340 \hspace{1em}
341 \sdaps_context_append:nn { * } { linewidth=2bp }
342 \sdaps_checkbox:nn {} {}
343
344 \hspace{1em}
345 \sdaps_context_append:nn { multichoice } { fill=red }
346 \sdaps_checkbox:nn {} {}
347
348 \hspace{1em}
349 \sdaps_context_set:n { * = {} }
350 \sdaps_checkbox:nn {} {}
351
352
353 \hspace{1em}
354 \sdaps_context_set:n { multichoice={ellipse} }
355 \sdaps_checkbox:nn {} {}
356
357 \hspace{1em}
358 \sdaps_context_append:nn { * } { linewidth=2bp }
359 \sdaps_checkbox:nn {} {}
360
361 \hspace{1em}
362 \sdaps_context_append:nn { multichoice } { fill=red }
363 \sdaps_checkbox:nn {} {}
364
365 \hspace{1em}
366 \sdaps_context_set:n { * = {} }
367 \sdaps_checkbox:nn {} {}
368
369 \newline
370
371 \sdaps_context_set:n { * = { centered_text = X }, multichoice = { width=5mm, height=5mm} }
372 \sdaps_checkbox:nn {} {}
373
374 \hspace{1em}
375 \sdaps_context_append:nn { * } { linewidth=2bp }
376 \sdaps_checkbox:nn {} {}
377
378 \hspace{1em}
379 \sdaps_context_append:nn { checkbox } { fill=red }
380 \sdaps_checkbox:nn {} {}
381
382 \hspace{1em}
383 \sdaps_context_set:n { * = {} }
384 \sdaps_checkbox:nn {} {}
385
386 \hspace{1em}
387 \sdaps_context_set:n { multichoice={draw_check} }
388 \sdaps_checkbox:nn {} {}
389
390 \hspace{1em}
391 \sdaps_context_append:nn { * } { linewidth=2bp }
392 \sdaps_checkbox:nn {} {}
393
394 \hspace{1em}
395 \sdaps_context_append:nn { multichoice } { fill=red }
396 \sdaps_checkbox:nn {} {}
397
398 \hspace{1em}
399 \sdaps_context_set:n { * = {} }
400 \sdaps_checkbox:nn {} {}
401
402 \ExplSyntaxOff
403
404 .. todo:: It appears the text is not centered correctly.
405