summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/sdaps/sdapslayout.dtx
blob: eed8031392a67086fdbbf1b4c80f423be7230c5b (plain)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
% \iffalse meta-comment
%
% Copyright (C) 2014 by Henry Menke <henrimenke@gmail.com>
% Copyright (C) 2019 by Benjamin Berg <benjamin@sipsolutions.net>
%
% 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
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Benjamin Berg.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{sdapslayout.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{sdapslayout}
%<*package>
    [2015/04/10 v0.1 Initial version of SDAPS layout package]
%</package>
%
%<*driver>
\documentclass{l3doc}
\usepackage{sdapslayout}[2015/04/10]
\EnableCrossrefs
%\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{sdapslayout.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.1}{2015/01/14}{Initial version}
%
% \GetFileInfo{sdapslayout.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment}
% 
%
% \title{The \textsf{sdapslayout} package\thanks{This document
%   corresponds to \textsf{sdapslayout}~\fileversion, dated \filedate.}}
% \author{Benjamin Berg \\ \texttt{benjamin@sipsolutions.net}}
%
% \maketitle
%
% \section{Documentation}
%
% Please refer to \url{https://sdaps.org/class-doc} for documentation.
%
% \section{Implementation}
%
% This package uses the \LaTeX3 language internally, so we need to enable it.
%    \begin{macrocode}
% We need at least 2011-08-23 for \keys_set_known:nnN
\RequirePackage{expl3}[2011/08/23]
%\RequirePackage{xparse}
\ExplSyntaxOn
%    \end{macrocode}
%
% And we need a number of other packages.
%    \begin{macrocode}
\ExplSyntaxOff

\RequirePackage{sdapsbase}
\RequirePackage{sdapsarray}
\RequirePackage{xparse}


\ExplSyntaxOn

%    \end{macrocode}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
% \subsection{Choice Question Layout}
%
% \subsubsection{Choice Question Matrix Layout}
%
% The following macros provide the funcitonality to layout choice questions in
% a matrix like fashion.
%
%    \begin{macrocode}

\tl_new:N \l_sdaps_choicearray_qobject_type_tl
\bool_new:N \l_sdaps_choicearray_horizontal_bool
\tl_new:N \l_sdaps_choicearray_layouter_tl
\tl_new:N \l_sdaps_choicearray_align_tl
\tl_new:N \l_sdaps_choicearray_extra_tl
\tl_new:N \l_sdaps_choicearray_type_tl
\tl_new:N \l_sdaps_choice_var_tl
\tl_new:N \l_sdaps_choice_val_tl
\tl_new:N \l_sdaps_choice_text_tl
\tl_new:N \l_sdaps_question_var_tl
\tl_new:N \l_sdaps_question_text_tl
\clist_new:N \l_sdaps_question_range_clist
\int_new:N \g_sdaps_choices_box_int
\seq_new:N \g_sdaps_choices_filter_seq
\seq_new:N \g_sdaps_choices_cell_seq
\seq_new:N \g_sdaps_choices_text_seq

\keys_define:nn { sdaps / choicearray }
{
  horizontal .bool_set:N = \l_sdaps_choicearray_horizontal_bool,
  horizontal .default:n  = true,
  horizontal .initial:n  = true,
  vertical   .bool_set_inverse:N = \l_sdaps_choicearray_horizontal_bool,
  vertical   .default:n  = true,
  layouter   .tl_set:N   = \l_sdaps_choicearray_layouter_tl,
  layouter   .initial:n  = default,
  align      .tl_set:N   = \l_sdaps_choicearray_align_tl,
  align      .initial:n  = { },

  type       .choices:nn  = { multichoice, singlechoice } { \tl_set:Nx \l_sdaps_choicearray_type_tl { \l_keys_choice_tl } },
  type       .initial:n   = { multichoice },

  singlechoice .meta:n   = { type=singlechoice },
  multichoice  .meta:n   = { type=multichoice },

  noalign    .meta:n  = { align= },
}

\keys_define:nn { sdaps / choicearray / choice }
{
  var        .tl_set:N   = \l_sdaps_choice_var_tl,
  val        .tl_set:N   = \l_sdaps_choice_val_tl,
  text       .tl_set:N   = \l_sdaps_choice_text_tl,
}

\keys_define:nn { sdaps / choicearray / question }
{
  var        .tl_set:N   = \l_sdaps_question_var_tl,
  text       .tl_set:N   = \l_sdaps_question_text_tl,

  range      .clist_set:N   = \l_sdaps_question_range_clist,
  range      .initial:n = {...}
}

\cs_new_protected_nopar:Npn \_sdaps_choicearray_preprocess:n #1
{
  \keys_set_known:nnN { sdaps / choicearray } { #1 } \l_sdaps_choicearray_extra_tl

  \sdaps_checkbox_set_type:V \l_sdaps_choicearray_type_tl
  \tl_if_eq:VnTF \l_sdaps_choicearray_type_tl { multichoice } {
    \tl_set:Nn \l_sdaps_choicearray_qobject_type_tl { Choice }
  } {
    \tl_set:Nn \l_sdaps_choicearray_qobject_type_tl { Option }
  }
}
\cs_generate_variant:Nn \_sdaps_choicearray_preprocess:n { V }

\cs_new_protected_nopar:Npn \_sdaps_choicearray_process_choice_insert_tail_after:w {
  \bgroup
    \group_insert_after:N \_sdaps_choicearray_process_choice_tail:
    \sdaps_array_nested_alignenv:
    \tex_let:D\next=
}

\cs_new_protected_nopar:Nn \_sdaps_choicearray_process_choice_tail: {
  \ignorespaces
}

\cs_new_nopar:Nn \_sdaps_choicearray_grab_choice:n {
  \seq_gput_right:Nn \g_sdaps_choices_text_seq { #1 }
  \group_begin:
    \sdaps_array_nested_alignenv:
    #1
  \group_end:
  \_sdaps_choicearray_process_choice_tail:
}

\cs_new_nopar:Npn \_sdaps_choicearray_process_choice:nw #1
{
  % This modifies grouping so it has to be at the start
  \sdaps_array_alignment:
  \leavevmode

  \tl_clear:N \l_sdaps_choice_var_tl
  \tl_clear:N \l_sdaps_choice_val_tl
  \tl_clear:N \l_sdaps_choice_text_tl

  \keys_set:nn { sdaps / choicearray / choice } { #1 }

  \int_gincr:N \g_sdaps_choices_box_int

  \tl_if_empty:NT \l_sdaps_choice_var_tl {
    % Prefix with _ to force prefixing with generated variable
    \tl_set:Nx \l_sdaps_choice_var_tl { \int_use:N \g_sdaps_choices_box_int }
  }
  \tl_if_empty:NT \l_sdaps_choice_val_tl {
    \tl_set:Nx \l_sdaps_choice_val_tl { \int_use:N \g_sdaps_choices_box_int }
  }

  \tl_if_eq:VnTF \l_sdaps_choicearray_type_tl { multichoice } {
    \seq_gput_right:NV \g_sdaps_choices_filter_seq \l_sdaps_choice_var_tl
  } {
    \seq_gput_right:NV \g_sdaps_choices_filter_seq \l_sdaps_choice_val_tl
  }

  \seq_gput_right:Nx \g_sdaps_choices_cell_seq { \exp_not:n { \sdaps_checkbox:nn } { _ \l_sdaps_choice_var_tl } { \l_sdaps_choice_val_tl } }

  \tl_if_empty:NTF \l_sdaps_choice_text_tl {
    % We need to leave a command in the stream that grabs the next parameter
    % and outputs it immediately
    \cs_set_eq:NN \l_tmpa_token \_sdaps_choicearray_grab_choice:n
  } {
    % Nothing else to do
    \seq_gput_right:NV \g_sdaps_choices_text_seq { \l_sdaps_choice_text_tl }
    \cs_set_eq:NN \l_tmpa_token \_sdaps_choicearray_process_choice_insert_tail_after:w
  }
  \l_tmpa_token
}
\cs_generate_variant:Nn \_sdaps_choicearray_process_choice:nw { Vw }

\cs_new_protected_nopar:Nn \_sdaps_choicearray_process_question_grab:n {
  \tl_set:Nn \l_sdaps_question_text_tl { #1 }

  \_sdaps_choicearray_process_question_head:

  \group_begin:
    \sdaps_array_nested_alignenv:
    #1
  \group_end:

  \_sdaps_choicearray_process_question_tail:
}

\cs_new_protected_nopar:Npn \_sdaps_choicearray_process_question_inline:w {
  \_sdaps_choicearray_process_question_head:
  \bgroup
    \group_insert_after:N \_sdaps_choicearray_process_question_tail:
    \sdaps_array_nested_alignenv:
    \tex_let:D\next=
}

\cs_new_protected_nopar:Nn \_sdaps_choicearray_process_question_head: {
  \sdaps_qobject_begin:nnV { choicearray_question } \l_sdaps_choicearray_qobject_type_tl \l_sdaps_question_text_tl

  \tl_if_empty:NF \l_sdaps_question_var_tl {
    \sdaps_qobject_append_var:V \l_sdaps_question_var_tl
  }
}

\cs_new_protected_nopar:Nn \_sdaps_choicearray_process_question_tail: {
  \seq_gclear:N \g_tmpa_seq
  % l_tmpa_bool is whether we are in a run (i.e. ...)
  \bool_set_false:N \l_tmpa_bool

  \clist_gset_eq:NN \g_tmpa_clist \l_sdaps_question_range_clist
  \clist_gpop:NN \g_tmpa_clist \l_tmpa_tl

  \seq_map_inline:Nn \g_sdaps_choices_filter_seq {
    \tl_if_eq:VnT \l_tmpa_tl { ... } {
      \bool_set_true:N \l_tmpa_bool
      \clist_gpop:NN \g_tmpa_clist \l_tmpa_tl
    }

    \tl_if_eq:VnTF \l_tmpa_tl { ##1 } {
      \seq_gput_right:Nn \g_tmpa_seq { \c_true_bool }
      \bool_set_false:N \l_tmpa_bool
      \clist_gpop:NN \g_tmpa_clist \l_tmpa_tl
    } {
      % Append if we are handling a run of items and the current item is not
      % the last one.
      \bool_if:NTF \l_tmpa_bool {
        \seq_gput_right:Nn \g_tmpa_seq { \c_true_bool }
      } {
        % Otherwise, ignore this item
        \seq_gput_right:Nn \g_tmpa_seq { \c_false_bool }
      }
    }
  }

  \seq_gset_eq:NN \g_tmpb_seq \g_tmpa_seq

  \seq_map_inline:Nn \g_sdaps_choices_text_seq {
    \seq_gpop_left:NN \g_tmpa_seq \l_tmpa_tl
    \tl_if_eq:VnT \l_tmpa_tl { \c_true_bool } {
      \sdaps_answer:f { ##1 }
    }
  }

  \seq_map_inline:Nn \g_sdaps_choices_cell_seq {
    \sdaps_array_alignment:

    \seq_gpop_left:NN \g_tmpb_seq \l_tmpa_tl
    \tl_if_eq:VnT \l_tmpa_tl { \c_true_bool } {
      ##1
    }
  }

  \sdaps_qobject_end:n { choicearray_question }
  \ignorespaces
}

\cs_new_nopar:Npn \_sdaps_choicearray_process_question:nw #1
{
  \sdaps_array_newline:
  \leavevmode

  \keys_set:nn { sdaps / choicearray / question } { #1 }

  \tl_if_empty:NTF \l_sdaps_question_text_tl {
    % We need to leave a command in the stream that grabs the next parameter,
    % outputs it again, and finishes the question.
    \cs_set_eq:NN \l_tmpa_token \_sdaps_choicearray_process_question_grab:n
  } {
    % Insert the question around the next argument
    \cs_set_eq:NN \l_tmpa_token \_sdaps_choicearray_process_question_inline:w
  }
  \l_tmpa_token
}
\cs_generate_variant:Nn \_sdaps_choicearray_process_question:nw { Vw }



%
%    \end{macrocode}
%
%
% \subsection{Range Question Layout}
%
% \subsubsection{Range Question Matrix Layout}
%
% The following macros provide the functionality to layout range/option
% questions in a matrix like fashion.
%
%    \begin{macrocode}


\tl_new:N \l_sdaps_rangearray_align_tl
\tl_new:N \l_sdaps_rangearray_extra_tl
\int_new:N \l_sdaps_rangearray_rangecount_int
\bool_new:N \l_sdaps_rangearray_other_bool
\tl_new:N \g_sdaps_question_var_tl
\tl_new:N \g_sdaps_question_text_tl
\tl_new:N \g_sdaps_question_lowertext_tl
\tl_new:N \g_sdaps_question_uppertext_tl
\tl_new:N \g_sdaps_question_othertext_tl

\msg_new:nnn { sdapslayout } { option_not_supported } { The~#1~option~is~not~supported~by~this~environment. }

\keys_define:nn { sdaps / rangearray }
{
  count      .int_set:N  = \l_sdaps_rangearray_rangecount_int,
  count      .initial:n  = 5,
  align      .tl_set:N   = \l_sdaps_rangearray_align_tl,
  align      .initial:n  = { },
  % Override and disallow flipping; it does not work currently
  flip       .code:n     = { \msg_error:nnn { sdapslayout } { option_not_supported } { flip } },
  other      .bool_set:N = \l_sdaps_rangearray_other_bool,
  other      .default:n  = true,
  other      .initial:n  = false,
}

\keys_define:nn { sdaps / rangearray / question }
{
  var        .tl_gset:N   = \g_sdaps_question_var_tl,
  text       .tl_gset:N   = \g_sdaps_question_text_tl,
  upper      .tl_gset:N   = \g_sdaps_question_uppertext_tl,
  lower      .tl_gset:N   = \g_sdaps_question_lowertext_tl,
  other      .tl_gset:N   = \g_sdaps_question_othertext_tl,
}

\cs_new_protected_nopar:Npn \_sdaps_rangearray_preprocess:n #1
{
  \keys_set_known:nnN { sdaps / rangearray } { #1 } \l_sdaps_rangearray_extra_tl

  \sdaps_checkbox_set_type:n { singlechoice }
}
\cs_generate_variant:Nn \_sdaps_rangearray_preprocess:n { V }

% Before/After the different parts

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_before_question: {
  \sdaps_array_newline:
  % Note: This needs to be after sdaps_array_newline as the command may be
  %       discarded otherwise (i.e. it does not make it into the output stream)
  %       We also need to leave vmode here
  \leavevmode
  \sdaps_qobject_begin:nnV { rangearray_question } { Range } \g_sdaps_question_text_tl

  \tl_if_empty:NF \g_sdaps_question_var_tl {
    \sdaps_qobject_append_var:V \g_sdaps_question_var_tl
  }

  \ignorespaces
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_before_lower: {
  % right align
  \sdaps_array_alignment:
  \leavevmode
  \sdaps_range:nnV { lower } { 0 } \g_sdaps_question_lowertext_tl
  \sdaps_if_rtl:F {
    \hfill
  }
  \ignorespaces
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_before_upper: {
  \sdaps_array_alignment:
  \leavevmode
  \sdaps_range:nnV { upper } { \l_sdaps_rangearray_rangecount_int - 1 } \g_sdaps_question_uppertext_tl
  \sdaps_if_rtl:T {
    \hfill
  }
  \ignorespaces
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_before_other: {
  % Insert an extra empty column for further spacing
  \sdaps_array_alignment:
  \sdaps_array_alignment:
  \leavevmode
  \sdaps_answer:V \g_sdaps_question_othertext_tl
  \sdaps_if_rtl:TF {
    \hfill
  } {
    \sdaps_checkbox:nn { } { 0 } {} ~ {}
  }
  \ignorespaces
}



\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_after_question: {
  % Insert an extra empty column for further spacing
  \sdaps_array_alignment:
  \tl_if_empty:NTF \g_sdaps_question_lowertext_tl {
    \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_grab_lower:n
  } {
    \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_inline_lower:w
  }
  \l_tmpa_token
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_after_lower: {
  % Insert the option checkbox column
  \sdaps_if_rtl:T {
    \hfill\kern 0pt
  }
  \sdaps_array_alignment:
  \leavevmode
  % Seems like right to left writing mode is not started without a paragraph
  \sdaps_if_rtl:T { \beginR }
  % Assume we have at least one checkbox
  \sdaps_checkbox:nn { } { 1 }
  \int_step_inline:nnnn { 2 } { 1 } { \l_sdaps_rangearray_rangecount_int } {
    \hspace{1em} \sdaps_checkbox:nn { } { ##1 }
  }
  \sdaps_if_rtl:T { \endR }

  \tl_if_empty:NTF \g_sdaps_question_uppertext_tl {
    \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_grab_upper:n
  } {
    \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_inline_upper:w
  }
  \l_tmpa_token
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_after_upper: {
  \sdaps_if_rtl:F {
    \hfill\kern 0pt
  }

  \bool_if:NTF \l_sdaps_rangearray_other_bool {
    \tl_if_empty:NTF \g_sdaps_question_othertext_tl {
      \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_grab_other:n
    } {
      \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_inline_other:w
    }
  } {
    \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_finish:
  }
  \l_tmpa_token
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_after_other: {
  \sdaps_if_rtl:TF {
    {} ~ \sdaps_checkbox:nn { } { 0 } {}
  } {
    \hfill\kern 0pt
  }
  \ignorespaces
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_finish: {
  \sdaps_qobject_end:n { rangearray_question }
  \ignorespaces
}

% Processors for inline processing/grabbing the argument

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_grab_question:n {
  \tl_gset:Nn \g_sdaps_question_text_tl { #1 }

  \_sdaps_rangearray_process_question_before_question:
  \group_begin:
    \sdaps_array_nested_alignenv:
    #1
  \group_end:
  \_sdaps_rangearray_process_question_after_question:
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_grab_lower:n {
  \tl_gset:Nn \g_sdaps_question_lowertext_tl { #1 }

  \_sdaps_rangearray_process_question_before_lower:
  \group_begin:
    \sdaps_array_nested_alignenv:
    #1
  \group_end:
  \_sdaps_rangearray_process_question_after_lower:
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_grab_upper:n {
  \tl_gset:Nn \g_sdaps_question_uppertext_tl { #1 }

  \_sdaps_rangearray_process_question_before_upper:
  \group_begin:
    \sdaps_array_nested_alignenv:
    #1
  \group_end:
  \_sdaps_rangearray_process_question_after_upper:
}

\cs_new_protected_nopar:Nn \_sdaps_rangearray_process_question_grab_other:n {
  \tl_gset:Nn \g_sdaps_question_othertext_tl { #1 }

  % If the text is empty, assume that this particular question does not have
  % an alternative choice. Note that this column might not exist and this
  % macro will not even be called in that case.
  % If we skip the optional "other" option then we still need to insert the
  % alignment to create the column.
  \tl_if_empty:NTF \g_sdaps_question_othertext_tl {
    \sdaps_array_alignment:
    \leavevmode
    \ignorespaces
  } {
    \_sdaps_rangearray_process_question_before_other:
    \group_begin:
      \sdaps_array_nested_alignenv:
      #1
    \group_end:
    \_sdaps_rangearray_process_question_after_other:
  }
  \_sdaps_rangearray_process_question_finish:
}



\cs_new_protected_nopar:Npn \_sdaps_rangearray_process_question_inline_question:w {
  \_sdaps_rangearray_process_question_before_question:
  \bgroup
    \group_insert_after:N \_sdaps_rangearray_process_question_after_question:
    \sdaps_array_nested_alignenv:
    \tex_let:D\next=
}

\cs_new_protected_nopar:Npn \_sdaps_rangearray_process_question_inline_lower:w {
  \_sdaps_rangearray_process_question_before_lower:
  \bgroup
    \group_insert_after:N \_sdaps_rangearray_process_question_after_lower:
    \tex_let:D\next=
}

\cs_new_protected_nopar:Npn \_sdaps_rangearray_process_question_inline_upper:w {
  \_sdaps_rangearray_process_question_before_upper:
  \bgroup
    \group_insert_after:N \_sdaps_rangearray_process_question_after_upper:
    \tex_let:D\next=
}

\cs_new_protected_nopar:Npn \_sdaps_rangearray_process_question_inline_other:w {
  \_sdaps_rangearray_process_question_before_other:
  % If we reach this macro then a text has been set for the other item. This
  % means we never need to ignore the "other" parameter at this point.
  \bgroup
    \group_insert_after:N \_sdaps_rangearray_process_question_after_other:
    \group_insert_after:N \_sdaps_rangearray_process_question_finish:
    \tex_let:D\next=
}




\cs_new_nopar:Npn \_sdaps_rangearray_process_question:nw #1
{
  % Is there a better way other than clearing these before parsing?
  \tl_gclear:N \g_sdaps_question_var_tl
  \tl_gclear:N \g_sdaps_question_text_tl
  \tl_gclear:N \g_sdaps_question_uppertext_tl
  \tl_gclear:N \g_sdaps_question_lowertext_tl
  \tl_gclear:N \g_sdaps_question_othertext_tl

  \keys_set:nn { sdaps / rangearray / question } { #1 }

  \tl_if_empty:NTF \g_sdaps_question_text_tl {
    % We need to leave a command in the stream that grabs the next parameter,
    % outputs it again, and finishes the question.
    \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_grab_question:n
  } {
    % We need to generate the question after the next group stops
    \cs_set_eq:NN \l_tmpa_token \_sdaps_rangearray_process_question_inline_question:w
  }
  \l_tmpa_token
}
\cs_generate_variant:Nn \_sdaps_rangearray_process_question:nw { Vw }



%
%    \end{macrocode}
%
%
%
% \subsection{Export user facing environments}
%
%    \begin{macrocode}
%


\newenvironment { choicearray } [ 1 ] []
{
  \group_begin:

    \sdaps_context_get:nN { choicearray } \l_tmpa_tl
    \tl_if_eq:NNT \l_tmpa_tl \q_no_value {
      \tl_set:Nn \l_tmpa_tl {}
    }

    \tl_if_empty:nF { #1 } {
      \tl_if_empty:NTF \l_tmpa_tl {
        \tl_set:Nn \l_tmpa_tl { #1 }
      } {
        \tl_set:Nf \l_tmpa_tl { \l_tmpa_tl, #1 }
      }
    }

    \_sdaps_choicearray_preprocess:V \l_tmpa_tl
    % Clear the variables
    \seq_gclear:N \g_sdaps_choices_filter_seq
    \seq_gclear:N \g_sdaps_choices_cell_seq
    \seq_gclear:N \g_sdaps_choices_text_seq

    \int_gzero:N \g_sdaps_choices_box_int

    % Define new commands
    \newcommand \choice [ 1 ] [] {
      \_sdaps_choicearray_process_choice:nw { ##1 }
    }
    \newcommand \question [ 1 ] [] {
      \_sdaps_choicearray_process_question:nw { ##1 }
    }

    \group_begin:

      \tl_set:Nx \l_tmpb_tl {keepenv,layouter=\tl_use:N\l_sdaps_choicearray_layouter_tl,align=\l_sdaps_choicearray_align_tl\bool_if:NF\l_sdaps_choicearray_horizontal_bool{,flip},\l_sdaps_choicearray_extra_tl}
      \expandafter\sdapsarray\expandafter[\l_tmpb_tl]
}
{
      \endsdapsarray
    \group_end:

  \group_end:
}

\newenvironment { optionarray } [ 1 ] []
{
  \choicearray[singlechoice,#1]
}
{
  \endchoicearray
}

\newenvironment { rangearray } [ 1 ] []
{
  \group_begin:

    \sdaps_context_get:nN { rangearray } \l_tmpa_tl
    \tl_if_eq:NNT \l_tmpa_tl \q_no_value {
      \tl_set:Nn \l_tmpa_tl {}
    }

    \tl_if_empty:nF { #1 } {
      \tl_if_empty:NTF \l_tmpa_tl {
        \tl_set:Nn \l_tmpa_tl { #1 }
      } {
        \tl_set:Nf \l_tmpa_tl { \l_tmpa_tl, #1 }
      }
    }

    \_sdaps_rangearray_preprocess:V \l_tmpa_tl

    \newcommand \range [ 1 ] [] {
      \_sdaps_rangearray_process_question:nw { ##1 }
    }

    \group_begin:

      \tl_set:Nx \l_tmpb_tl {keepenv,align=\l_sdaps_rangearray_align_tl,no_header,\l_sdaps_rangearray_extra_tl}
      \expandafter\sdapsarray\expandafter[\l_tmpb_tl]
}
{
      \endsdapsarray
    \group_end:

  \group_end:
}


\ExplSyntaxOff

%
%    \end{macrocode}
%
% \iffalse
% \PrintChanges
% \PrintIndex
% \fi
%
% \Finale
\endinput