| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Copyright (C) 2019-2021 by Benjamin Berg <[email protected]> |
| 4 |
% |
| 5 |
% This work may be distributed and/or modified under the |
| 6 |
% conditions of the LaTeX Project Public License, either version 1.3c |
| 7 |
% of this license or (at your option) any later version. |
| 8 |
% The latest version of this license is in |
| 9 |
% http://www.latex-project.org/lppl.txt |
| 10 |
% |
| 11 |
% This work has the LPPL maintenance status `maintained'. |
| 12 |
% |
| 13 |
% The Current Maintainer of this work is Benjamin Berg. |
| 14 |
% |
| 15 |
% \fi |
| 16 |
% |
| 17 |
% \iffalse |
| 18 |
%<*driver> |
| 19 |
\ProvidesFile{sdapsarray.dtx} |
| 20 |
%</driver> |
| 21 |
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01] |
| 22 |
%<package>\ProvidesPackage{sdapsarray} |
| 23 |
%<*package> |
| 24 |
[2015/04/10 v0.1 Initial version of SDAPS array package] |
| 25 |
%</package> |
| 26 |
% |
| 27 |
%<*driver> |
| 28 |
\documentclass{ltxdoc} |
| 29 |
\usepackage{sdapsarray}[2015/04/10] |
| 30 |
%\EnableCrossrefs |
| 31 |
\CodelineIndex |
| 32 |
\RecordChanges |
| 33 |
\begin{document} |
| 34 |
\DocInput{sdapsarray.dtx} |
| 35 |
\end{document} |
| 36 |
%</driver> |
| 37 |
% \fi |
| 38 |
% |
| 39 |
% \CheckSum{0} |
| 40 |
% |
| 41 |
% \CharacterTable |
| 42 |
% {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 |
| 43 |
% 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 |
| 44 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 45 |
% Exclamation \! Double quote \" Hash (number) \# |
| 46 |
% Dollar \$ Percent \% Ampersand \& |
| 47 |
% Acute accent \' Left paren \( Right paren \) |
| 48 |
% Asterisk \* Plus \+ Comma \, |
| 49 |
% Minus \- Point \. Solidus \/ |
| 50 |
% Colon \: Semicolon \; Less than \< |
| 51 |
% Equals \= Greater than \> Question mark \? |
| 52 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 53 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 54 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 55 |
% Right brace \} Tilde \~} |
| 56 |
% |
| 57 |
% |
| 58 |
% \changes{v0.1}{2015/01/14}{Initial version} |
| 59 |
% |
| 60 |
% \GetFileInfo{sdapsarray.dtx} |
| 61 |
% |
| 62 |
% \DoNotIndex{\newcommand,\newenvironment} |
| 63 |
% |
| 64 |
% |
| 65 |
% \title{The \textsf{sdapsarray} package\thanks{This document |
| 66 |
% corresponds to \textsf{sdapsarray}~\fileversion, dated \filedate.}} |
| 67 |
% \author{Benjamin Berg \\ \texttt{[email protected]}} |
| 68 |
% |
| 69 |
% \maketitle |
| 70 |
% |
| 71 |
% \section{Documentation} |
| 72 |
% |
| 73 |
% Please refer to \url{https://sdaps.org/class-doc} for documentation. |
| 74 |
% |
| 75 |
% \StopEventually{\PrintChanges\PrintIndex} |
| 76 |
% |
| 77 |
% \section{Implementation} |
| 78 |
% |
| 79 |
% This package uses the \LaTeX3 language internally, so we need to enable it. |
| 80 |
% \begin{macrocode} |
| 81 |
% We need at least 2011-08-23 for \keys_set_known:nnN |
| 82 |
\RequirePackage{expl3}[2011/08/23] |
| 83 |
%\RequirePackage{xparse} |
| 84 |
\ExplSyntaxOn |
| 85 |
% \end{macrocode} |
| 86 |
% |
| 87 |
% And we need a number of other packages. |
| 88 |
% \begin{macrocode} |
| 89 |
\ExplSyntaxOff |
| 90 |
|
| 91 |
\RequirePackage{xparse} |
| 92 |
\RequirePackage{sdapsbase} |
| 93 |
|
| 94 |
|
| 95 |
\ExplSyntaxOn |
| 96 |
|
| 97 |
% \end{macrocode} |
| 98 |
% |
| 99 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 100 |
% |
| 101 |
% \subsection{Tempfile handling} |
| 102 |
% |
| 103 |
% This is a bit weird, but under some conditions we need extended information |
| 104 |
% about each row in the document (for page break detection). As it makes little |
| 105 |
% to no sense to load all this information into memory at start we use two |
| 106 |
% temporary files instead. As these files should not change their content |
| 107 |
% for reruns (that would break e.g. latexmk) we copy the "tic" file into "toc" |
| 108 |
% so that "toc" can be read while "tic" is being re-written. |
| 109 |
% We then go on to define a macro which will read a single |
| 110 |
% line and return true if it is different from the previous line. |
| 111 |
% This is the indicator that a page/column break has happened and the |
| 112 |
% row header needs to be inserted. |
| 113 |
% |
| 114 |
% \begin{macrocode} |
| 115 |
|
| 116 |
% This will change in early 2018, but the new code apparently does not |
| 117 |
% provide the old name of the definition. |
| 118 |
% i.e. this is a bad hack and should be removed latest in 2019 or so |
| 119 |
\cs_if_exist:NF \ior_str_get:NN { \cs_set_eq:Nc \ior_str_get:NN { ior_get_str:NN } } |
| 120 |
|
| 121 |
\bool_new:N \g__sdaps_array_info_open |
| 122 |
\bool_gset_false:N \g__sdaps_array_info_open |
| 123 |
\iow_new:N \g__sdaps_array_info_iow |
| 124 |
\ior_new:N \g__sdaps_array_info_ior |
| 125 |
|
| 126 |
\cs_generate_variant:Nn \int_set:Nn { Nf } |
| 127 |
\cs_generate_variant:Nn \coffin_join:NnnNnnnn { NnVNnVnn } |
| 128 |
|
| 129 |
\cs_new_protected_nopar:Nn \_sdaps_array_open_tmpfiles: |
| 130 |
{ |
| 131 |
% Guard against being executed multiple times |
| 132 |
\bool_if:NF \g__sdaps_array_info_open { |
| 133 |
\bool_gset_true:N \g__sdaps_array_info_open |
| 134 |
|
| 135 |
% Also ensures toc file exists (i.e. is readable) |
| 136 |
\iow_open:Nn \g__sdaps_array_info_iow { \c_sys_jobname_str .sdapsarraytoc } |
| 137 |
\file_if_exist:nTF { \c_sys_jobname_str .sdapsarraytic } { |
| 138 |
% Copy into toc file, then open that. |
| 139 |
\ior_open:Nn \g__sdaps_array_info_ior { \c_sys_jobname_str .sdapsarraytic } |
| 140 |
|
| 141 |
\ior_str_map_inline:Nn \g__sdaps_array_info_ior { \iow_now:Nn \g__sdaps_array_info_iow { ##1 } } |
| 142 |
\ior_close:N \g__sdaps_array_info_ior |
| 143 |
|
| 144 |
\ior_open:Nn \g__sdaps_array_info_ior { \c_sys_jobname_str .sdapsarraytoc } |
| 145 |
} { |
| 146 |
} |
| 147 |
\iow_close:N \g__sdaps_array_info_iow |
| 148 |
|
| 149 |
\iow_open:Nn \g__sdaps_array_info_iow { \c_sys_jobname_str .sdapsarraytic } |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
\tl_new:N \g__sdaps_array_last_row_tl |
| 154 |
\tl_gset_eq:NN \g__sdaps_array_last_row_tl \c_empty_tl |
| 155 |
|
| 156 |
\cs_new_protected_nopar:Nn \_sdaps_array_check_insert_header:N |
| 157 |
{ |
| 158 |
\bool_gset_eq:NN #1 \c_false_bool |
| 159 |
\ior_if_eof:NF \g__sdaps_array_info_ior { |
| 160 |
\ior_str_get:NN \g__sdaps_array_info_ior \l_tmpa_tl |
| 161 |
|
| 162 |
\tl_if_eq:NNF \g__sdaps_array_last_row_tl \c_empty_tl { |
| 163 |
\tl_if_eq:NNF \g__sdaps_array_last_row_tl \l_tmpa_tl { |
| 164 |
\bool_gset_eq:NN #1 \c_true_bool |
| 165 |
} |
| 166 |
} |
| 167 |
\tl_gset_eq:NN \g__sdaps_array_last_row_tl \l_tmpa_tl |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
% \end{macrocode} |
| 172 |
% |
| 173 |
% \subsection{Initialization} |
| 174 |
% |
| 175 |
% Global definitions and penalty constant. |
| 176 |
% |
| 177 |
% \begin{macrocode} |
| 178 |
|
| 179 |
\prop_new:N \g__sdaps_array_layouter_prop |
| 180 |
|
| 181 |
% XXX: Penalty in between rows. After the header a nobreak is inserted, but |
| 182 |
% do we want special penalties elsewhere (preventing orphans/widows?) |
| 183 |
\int_new:N \g_sdaps_array_row_penalty_tl |
| 184 |
\int_gset:Nn \g_sdaps_array_row_penalty_tl { 10 } |
| 185 |
|
| 186 |
% \end{macrocode} |
| 187 |
% |
| 188 |
% \subsection{Initialization} |
| 189 |
% |
| 190 |
% Define some routines to store width information for columns (across builds). |
| 191 |
% |
| 192 |
% \begin{macrocode} |
| 193 |
|
| 194 |
\tl_new:N \g_sdaps_array_shared_data_tl |
| 195 |
\tl_new:N \g_sdaps_array_stored_data_tl |
| 196 |
\tl_new:N \g_sdaps_array_local_data_tl |
| 197 |
\tl_new:N \g_sdaps_array_local_data_new_tl |
| 198 |
\prop_new:N \g__sdaps_array_stored_data_prop |
| 199 |
\prop_new:N \g__sdaps_array_shared_data_prop |
| 200 |
\prop_new:N \g__sdaps_array_local_data_prop |
| 201 |
|
| 202 |
\cs_generate_variant:Nn \prop_item:Nn { NV } |
| 203 |
|
| 204 |
\cs_new_protected_nopar:Nn \_sdaps_array_load_data: |
| 205 |
{ |
| 206 |
\tl_gset:Nx \g_sdaps_array_stored_data_tl { \prop_item:NV \g__sdaps_array_stored_data_prop \l__sdaps_array_global_name_tl } |
| 207 |
\tl_gset:Nx \g_sdaps_array_shared_data_tl { \prop_item:NV \g__sdaps_array_shared_data_prop \l__sdaps_array_global_name_tl } |
| 208 |
\tl_gset:Nx \g_sdaps_array_local_data_tl { \prop_item:NV \g__sdaps_array_local_data_prop \l__sdaps_array_local_name_tl } |
| 209 |
} |
| 210 |
|
| 211 |
\cs_new_protected_nopar:Nn \_sdaps_array_store_data: |
| 212 |
{ |
| 213 |
% Do not overwrite the "stored" data that we have right now. |
| 214 |
\prop_gput:NVV \g__sdaps_array_shared_data_prop \l__sdaps_array_global_name_tl \g_sdaps_array_shared_data_tl |
| 215 |
|
| 216 |
\immediate\write\@auxout{\exp_not:n{\sdapsarrayloadstoreddata}{\l__sdaps_array_global_name_tl}{\g_sdaps_array_shared_data_tl}} |
| 217 |
\tl_if_empty:NF \g_sdaps_array_local_data_new_tl { |
| 218 |
\immediate\write\@auxout{\exp_not:n{\sdapsarrayloadlocaldata}{\l__sdaps_array_local_name_tl}{\g_sdaps_array_local_data_new_tl}} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
% Define for loading sdaps code in aux file |
| 223 |
\cs_new_protected_nopar:Nn \sdaps_array_load_stored_data:nn { |
| 224 |
\prop_gput:Nnn \g__sdaps_array_stored_data_prop { #1 } { #2 } |
| 225 |
} |
| 226 |
\cs_new_eq:NN \sdapsarrayloadstoreddata \sdaps_array_load_stored_data:nn |
| 227 |
|
| 228 |
% Define for loading sdaps code in aux file |
| 229 |
\cs_new_protected_nopar:Nn \sdaps_array_load_local_data:nn { |
| 230 |
\prop_gput:Nnn \g__sdaps_array_local_data_prop { #1 } { #2 } |
| 231 |
} |
| 232 |
\cs_new_eq:NN \sdapsarrayloadlocaldata \sdaps_array_load_local_data:nn |
| 233 |
|
| 234 |
% \end{macrocode} |
| 235 |
% |
| 236 |
% \subsection{Array Layouter} |
| 237 |
% |
| 238 |
% \subsubsection{User facing macros} |
| 239 |
% |
| 240 |
% \begin{macrocode} |
| 241 |
|
| 242 |
\int_new:N \g__sdaps_array_current_id_int |
| 243 |
\tl_new:N \l__sdaps_array_global_name_tl |
| 244 |
\tl_new:N \l__sdaps_array_local_name_tl |
| 245 |
|
| 246 |
\cs_generate_variant:Nn \keys_set:nn { nf } |
| 247 |
\cs_new_protected_nopar:Nn \sdaps_array_begin:nn |
| 248 |
{ |
| 249 |
\tl_set:Nx \l__sdaps_array_local_name_tl { sdapsarray \int_use:N\g__sdaps_array_current_id_int } |
| 250 |
\int_gincr:N \g__sdaps_array_current_id_int |
| 251 |
\tl_if_empty:nTF { #2 } { |
| 252 |
\tl_set:NV \l__sdaps_array_global_name_tl \l__sdaps_array_local_name_tl |
| 253 |
} { |
| 254 |
\tl_set:Nx \l__sdaps_array_global_name_tl { #2 } |
| 255 |
} |
| 256 |
|
| 257 |
\_sdaps_array_load_data: |
| 258 |
|
| 259 |
\keys_set:nf { sdaps / array } { \prop_item:Nn \g__sdaps_array_layouter_prop { #1 } } |
| 260 |
|
| 261 |
% Force cell layouter instead of column header layouter |
| 262 |
\bool_if:NT \l_sdaps_sdapsarray_no_header_bool { |
| 263 |
\tl_set:NV \l__sdaps_array_colhead_tl \l__sdaps_array_cell_tl |
| 264 |
} |
| 265 |
|
| 266 |
\_sdaps_array_open_tmpfiles: |
| 267 |
|
| 268 |
\tl_gset_eq:NN \g__sdaps_array_last_row_tl \c_empty_tl |
| 269 |
\bool_gset_true:N \g_sdaps_array_first_row_bool |
| 270 |
|
| 271 |
\l__sdaps_array_begin_tl |
| 272 |
} |
| 273 |
\cs_generate_variant:Nn \sdaps_array_begin:nn { Vn } |
| 274 |
\cs_generate_variant:Nn \sdaps_array_begin:nn { VV } |
| 275 |
\cs_generate_variant:Nn \sdaps_array_begin:nn { nV } |
| 276 |
|
| 277 |
\cs_new_protected_nopar:Nn \sdaps_array_begin:n |
| 278 |
{ |
| 279 |
\sdaps_array_begin:nn { #1 } { } |
| 280 |
} |
| 281 |
|
| 282 |
\cs_new_protected_nopar:Nn \sdaps_array_row_start: |
| 283 |
{ |
| 284 |
\l__sdaps_array_row_start_tl |
| 285 |
} |
| 286 |
|
| 287 |
\cs_new_protected_nopar:Nn \_sdaps_array_assign_use: { |
| 288 |
\box_use:N \l_tmpa_box |
| 289 |
\egroup |
| 290 |
} |
| 291 |
|
| 292 |
\cs_new_protected_nopar:Npn \sdaps_array_assign_colhead:Nw #1 |
| 293 |
{ |
| 294 |
\l__sdaps_array_colhead_tl #1 |
| 295 |
} |
| 296 |
|
| 297 |
\cs_new_protected_nopar:Npn \sdaps_array_colhead:w |
| 298 |
{ |
| 299 |
\bgroup |
| 300 |
\l__sdaps_array_colhead_tl \l_tmpa_box \bgroup |
| 301 |
\group_insert_after:N\_sdaps_array_assign_use: |
| 302 |
% swallow group opening token |
| 303 |
\tex_let:D\next= |
| 304 |
} |
| 305 |
|
| 306 |
\cs_new_protected:Npn \sdaps_array_assign_rowhead:Nw #1 |
| 307 |
{ |
| 308 |
\l__sdaps_array_rowhead_tl #1 |
| 309 |
} |
| 310 |
|
| 311 |
\cs_new_protected:Npn \sdaps_array_rowhead:w |
| 312 |
{ |
| 313 |
\bgroup |
| 314 |
\l__sdaps_array_rowhead_tl \l_tmpa_box \bgroup |
| 315 |
\group_insert_after:N\_sdaps_array_assign_use: |
| 316 |
% swallow group opening token |
| 317 |
\tex_let:D\next= |
| 318 |
} |
| 319 |
|
| 320 |
\cs_new_protected_nopar:Npn \sdaps_array_assign_cell:Nw #1 |
| 321 |
{ |
| 322 |
\l__sdaps_array_cell_tl #1 |
| 323 |
} |
| 324 |
|
| 325 |
\cs_new_protected_nopar:Npn \sdaps_array_cell:w |
| 326 |
{ |
| 327 |
\bgroup |
| 328 |
\l__sdaps_array_cell_tl \l_tmpa_box \bgroup |
| 329 |
\group_insert_after:N\_sdaps_array_assign_use: |
| 330 |
% swallow group opening token |
| 331 |
\tex_let:D\next= |
| 332 |
} |
| 333 |
|
| 334 |
% XXX: Could this live in local scope instead? |
| 335 |
\box_new:N \g__sdaps_array_header_box |
| 336 |
\dim_new:N \g__sdaps_array_header_dim |
| 337 |
|
| 338 |
\cs_new_protected_nopar:Nn \_sdaps_array_calc_interlineskip:nnN |
| 339 |
{ |
| 340 |
\dim_compare:nNnTF { #1 } > { -1000pt } { |
| 341 |
\skip_set:Nn #3 { \baselineskip - #1 - #2 } |
| 342 |
\dim_compare:nNnF { #3 } > { \lineskiplimit } { |
| 343 |
\skip_set:Nn #3 { \lineskip } |
| 344 |
} |
| 345 |
} { |
| 346 |
\skip_set:Nn #3 { 0pt } |
| 347 |
} |
| 348 |
\skip_set:Nn #3 { #3 + \l_sdaps_sdapsarray_rowsep_dim } |
| 349 |
} |
| 350 |
|
| 351 |
\cs_new_protected_nopar:Nn \sdaps_array_row:NN |
| 352 |
{ |
| 353 |
\if_mode_vertical: |
| 354 |
\else: |
| 355 |
\msg_error:nn { sdapsarray } { wrong_mode } |
| 356 |
\fi |
| 357 |
|
| 358 |
% XXX: \l_tmpa_dim is the height to the first baseline in the box. Note that |
| 359 |
% we use the real baseline in the case of the header row! |
| 360 |
\l__sdaps_array_row_tl #1 #2 \l_tmpb_box \l_tmpa_dim |
| 361 |
|
| 362 |
\bool_if:nTF { \g_sdaps_array_first_row_bool && !\l_sdaps_sdapsarray_no_header_bool } { |
| 363 |
% Stow away the box for later use |
| 364 |
\box_gset_eq:NN \g__sdaps_array_header_box \l_tmpb_box |
| 365 |
\dim_gset:Nn \g__sdaps_array_header_dim { \box_ht:N \g__sdaps_array_header_box + \box_dp:N \g__sdaps_array_header_box } |
| 366 |
} { |
| 367 |
% Pagebreak detection (not needed for header row) |
| 368 |
\_sdaps_array_check_insert_header:N \g_tmpa_bool |
| 369 |
|
| 370 |
\hbox_set:Nn \l_tmpb_box { |
| 371 |
\pdfsavepos |
| 372 |
\iow_shipout_x:Nn \g__sdaps_array_info_iow { |
| 373 |
\thepage, |
| 374 |
\the\pdflastxpos |
| 375 |
} |
| 376 |
\box_use:N \l_tmpb_box |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
\bool_if:nTF { \g_sdaps_array_first_row_bool || \l_sdaps_sdapsarray_no_header_bool } { |
| 381 |
\_sdaps_array_calc_interlineskip:nnN { \prevdepth } { \l_tmpa_dim } \l_tmpa_skip |
| 382 |
\nointerlineskip |
| 383 |
\skip_vertical:n { \l_tmpa_skip } |
| 384 |
|
| 385 |
% However do not insert the rowskip in the case of the first line. |
| 386 |
% We rely on surrounding code to insert proper spacing before/after |
| 387 |
% the environment. |
| 388 |
\bool_if:NT \g_sdaps_array_first_row_bool { |
| 389 |
\skip_vertical:n { - \l_sdaps_sdapsarray_rowsep_dim } |
| 390 |
} |
| 391 |
|
| 392 |
\box_use:N \l_tmpb_box |
| 393 |
% For the header, insert a \nobreak, otherwise the normal inter-row penalty |
| 394 |
\bool_if:NTF \l_sdaps_sdapsarray_no_header_bool { |
| 395 |
\penalty\int_use:N\g_sdaps_array_row_penalty_tl |
| 396 |
} { |
| 397 |
\nobreak |
| 398 |
} |
| 399 |
|
| 400 |
\bool_gset_false:N \g_sdaps_array_first_row_bool |
| 401 |
} { |
| 402 |
% The idea is simple. Before every line the header is re-inserted (either |
| 403 |
% the real one or an empty box with the same dimensions). In the case that |
| 404 |
% there is *no* page break we insert a corresponding negative skip so that |
| 405 |
% the resulting skip (including interline skip) is exactly the normal |
| 406 |
% interline skip between the rows. |
| 407 |
% In the case that the skip *is* discarded we end up with the header box |
| 408 |
% and the normal interline skip between the new row and the header. i.e.: |
| 409 |
% skip = 1 * ( interlineskip_for_row - interlineskip_for_header - header_height - header_depth ) + header_height + header_depth + interlineksip_for_header |
| 410 |
% or |
| 411 |
% skip = 0 * ( interlineskip_for_row - interlineskip_for_header - header_height - header_depth ) + header_height + header_depth + interlineksip_for_header |
| 412 |
|
| 413 |
% Calculate interlineksip_for_row and interlineskip_for_header |
| 414 |
\_sdaps_array_calc_interlineskip:nnN { \prevdepth } { \l_tmpa_dim } \l_tmpa_skip |
| 415 |
\_sdaps_array_calc_interlineskip:nnN { \box_dp:N \g__sdaps_array_header_box } { \l_tmpa_dim } \l_tmpb_skip |
| 416 |
\nointerlineskip |
| 417 |
\skip_vertical:n { \l_tmpa_skip - \l_tmpb_skip } |
| 418 |
\kern -\g__sdaps_array_header_dim |
| 419 |
|
| 420 |
% Inser the real or fake box |
| 421 |
\bool_if:NTF \g_tmpa_bool { |
| 422 |
\box_use:N \g__sdaps_array_header_box |
| 423 |
} { |
| 424 |
\hrule height \box_ht:N \g__sdaps_array_header_box depth \box_dp:N \g__sdaps_array_header_box width 0pt |
| 425 |
} |
| 426 |
\nobreak |
| 427 |
% Insert the calculated interline skip (in the same way the TeX would do it. |
| 428 |
\nointerlineskip |
| 429 |
\skip_vertical:N \l_tmpb_skip |
| 430 |
|
| 431 |
\box_use:N \l_tmpb_box |
| 432 |
|
| 433 |
% And insert the sdapsarray sepecific inter row penalty. |
| 434 |
\penalty\int_use:N\g_sdaps_array_row_penalty_tl |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
\cs_new_protected_nopar:Nn \sdaps_array_end: |
| 439 |
{ |
| 440 |
\l__sdaps_array_end_tl |
| 441 |
\box_gclear:N \g__sdaps_array_header_box |
| 442 |
} |
| 443 |
|
| 444 |
% \end{macrocode} |
| 445 |
% |
| 446 |
% \subsubsection{Common Layouter Macros} |
| 447 |
% |
| 448 |
% \begin{macrocode} |
| 449 |
|
| 450 |
\tl_new:N \l__sdaps_array_begin_tl |
| 451 |
\tl_new:N \l__sdaps_array_row_start_tl |
| 452 |
\tl_new:N \l__sdaps_array_colhead_tl |
| 453 |
\tl_new:N \l__sdaps_array_rowhead_tl |
| 454 |
\tl_new:N \l__sdaps_array_cell_tl |
| 455 |
\tl_new:N \l__sdaps_array_row_tl |
| 456 |
\tl_new:N \l__sdaps_array_end_tl |
| 457 |
|
| 458 |
\keys_define:nn { sdaps / array } |
| 459 |
{ |
| 460 |
begin .tl_set:N = \l__sdaps_array_begin_tl, |
| 461 |
row_start .tl_set:N = \l__sdaps_array_row_start_tl, |
| 462 |
colhead .tl_set:N = \l__sdaps_array_colhead_tl, |
| 463 |
rowhead .tl_set:N = \l__sdaps_array_rowhead_tl, |
| 464 |
cell .tl_set:N = \l__sdaps_array_cell_tl, |
| 465 |
row .tl_set:N = \l__sdaps_array_row_tl, |
| 466 |
end .tl_set:N = \l__sdaps_array_end_tl, |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
\seq_new:N \g_sdaps_array_overhangs_left_seq |
| 471 |
\seq_new:N \g_sdaps_array_overhangs_right_seq |
| 472 |
\seq_new:N \g_sdaps_array_shared_colwidths_seq |
| 473 |
\seq_new:N \g_sdaps_array_stored_colwidths_seq |
| 474 |
|
| 475 |
\cs_new_protected_nopar:Npn \_sdaps_array_rowhead_default:Nw #1 |
| 476 |
{ |
| 477 |
\tl_if_empty:NTF \g_sdaps_array_local_data_tl { |
| 478 |
\tl_if_empty:NTF \g_sdaps_array_local_data_new_tl { |
| 479 |
\dim_set:Nn \l_tmpa_dim { 0.5 \hsize } |
| 480 |
} { |
| 481 |
\dim_set:Nn \l_tmpa_dim { \g_sdaps_array_local_data_new_tl } |
| 482 |
} |
| 483 |
} { |
| 484 |
\dim_set:Nn \l_tmpa_dim { \g_sdaps_array_local_data_tl } |
| 485 |
} |
| 486 |
% \vbox_set_top:Nw is still missing as of 2017-08-11 |
| 487 |
\tex_setbox:D #1 \tex_vtop:D \bgroup |
| 488 |
\sdaps_if_rtl:TF { |
| 489 |
\raggedright |
| 490 |
} { |
| 491 |
\raggedleft |
| 492 |
} |
| 493 |
\hsize=\dim_use:N\l_tmpa_dim |
| 494 |
\group_begin:\bgroup |
| 495 |
\group_insert_after:N \group_end: |
| 496 |
\group_insert_after:N \egroup |
| 497 |
% swallow group opening token |
| 498 |
\tex_let:D\next= |
| 499 |
} |
| 500 |
|
| 501 |
\cs_new_protected_nopar:Npn \_sdaps_array_cell_default:Nw #1 |
| 502 |
{ |
| 503 |
\hbox_set:Nw #1 \bgroup |
| 504 |
% swallow group opening token |
| 505 |
\group_insert_after:N \hbox_set_end: |
| 506 |
\tex_let:D\next= |
| 507 |
} |
| 508 |
|
| 509 |
\cs_new:Nn \_sdaps_array_cell_rotated_end: { |
| 510 |
\hbox_set_end: |
| 511 |
\dim_set:Nn \l_tmpa_dim { \box_ht:N \l_tmpa_box } |
| 512 |
\dim_set:Nn \l_tmpb_dim { \box_dp:N \l_tmpa_box } |
| 513 |
|
| 514 |
\dim_set:Nn \l_tmpa_dim { \l_sdaps_sdapsarray_angle_sine_tl \l_tmpa_dim } |
| 515 |
\dim_set:Nn \l_tmpb_dim { \l_sdaps_sdapsarray_angle_sine_tl \l_tmpb_dim } |
| 516 |
|
| 517 |
\box_rotate:Nn \l_tmpa_box { \l_sdaps_sdapsarray_angle_int } |
| 518 |
|
| 519 |
% We want the baseline of the box to be centered, that only works if we |
| 520 |
% leave the same space both ways. |
| 521 |
% That is not ideal, but we cannot move the cell content accordingly. |
| 522 |
\dim_set:Nn \l_tmpb_dim { \dim_max:nn { \l_tmpa_dim } { \l_tmpb_dim } } |
| 523 |
\skip_horizontal:n { \l_tmpb_dim } |
| 524 |
\rlap{ |
| 525 |
\skip_horizontal:n { -\l_tmpa_dim } |
| 526 |
\box_use:N \l_tmpa_box |
| 527 |
} |
| 528 |
\skip_horizontal:n { \l_tmpb_dim } |
| 529 |
% dummy skip that will be removed again by other code |
| 530 |
\skip_horizontal:n { 0pt } |
| 531 |
|
| 532 |
\dim_set:Nn \l_tmpa_dim { \l_tmpa_dim + \l_tmpb_dim } |
| 533 |
|
| 534 |
\dim_set:Nn \l_tmpb_dim { \box_wd:N \l_tmpa_box } |
| 535 |
\dim_set:Nn \l_tmpa_dim { \dim_max:nn { 0pt } { \l_tmpb_dim - \l_tmpa_dim } } |
| 536 |
|
| 537 |
\seq_gpush:Nn \g_sdaps_array_overhangs_left_seq { 0pt } |
| 538 |
\seq_gpush:Nx \g_sdaps_array_overhangs_right_seq { \dim_use:N \l_tmpa_dim } |
| 539 |
\egroup |
| 540 |
\hbox_set_end: |
| 541 |
} |
| 542 |
|
| 543 |
% Only sane for header row |
| 544 |
\cs_new_protected_nopar:Npn \_sdaps_array_cell_rotated:Nw #1 |
| 545 |
{ |
| 546 |
\hbox_set:Nw #1 \bgroup |
| 547 |
\hbox_set:Nw \l_tmpa_box |
| 548 |
\bgroup |
| 549 |
\group_insert_after:N \_sdaps_array_cell_rotated_end: |
| 550 |
% swallow group opening token |
| 551 |
\tex_let:D\next= |
| 552 |
} |
| 553 |
|
| 554 |
% XXX: A parbox layouter with fixed width would be nice |
| 555 |
%\cs_new_protected_nopar:Nn \sdaps_array_cell_fixed:n {} |
| 556 |
|
| 557 |
|
| 558 |
\cs_new_protected_nopar:Nn \_sdaps_array_begin_default: |
| 559 |
{ |
| 560 |
\tl_if_empty:NTF \g_sdaps_array_shared_data_tl { |
| 561 |
\seq_clear:N \g_sdaps_array_shared_colwidths_seq |
| 562 |
} { |
| 563 |
\seq_gset_split:NnV \g_sdaps_array_shared_colwidths_seq { ~ } \g_sdaps_array_shared_data_tl |
| 564 |
} |
| 565 |
\tl_if_empty:NTF \g_sdaps_array_stored_data_tl { |
| 566 |
\seq_clear:N \g_sdaps_array_stored_colwidths_seq |
| 567 |
} { |
| 568 |
\seq_gset_split:NnV \g_sdaps_array_stored_colwidths_seq { ~ } \g_sdaps_array_stored_data_tl |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
\cs_new_protected_nopar:Nn \_sdaps_array_end_default: |
| 573 |
{ |
| 574 |
\tl_gset:Nx \g_sdaps_array_shared_data_tl { \seq_use:Nn \g_sdaps_array_shared_colwidths_seq { ~ } } |
| 575 |
\tl_gset:Nx \g_sdaps_array_stored_data_tl { \seq_use:Nn \g_sdaps_array_stored_colwidths_seq { ~ } } |
| 576 |
|
| 577 |
% Clear the global sequences, to save memory |
| 578 |
\seq_gclear:N \g_sdaps_array_overhangs_left_seq |
| 579 |
\seq_gclear:N \g_sdaps_array_overhangs_right_seq |
| 580 |
\seq_gclear:N \g_sdaps_array_shared_colwidths_seq |
| 581 |
\seq_gclear:N \g_sdaps_array_stored_colwidths_seq |
| 582 |
|
| 583 |
\_sdaps_array_store_data: |
| 584 |
} |
| 585 |
|
| 586 |
\cs_new_protected_nopar:Nn \_sdaps_array_row_start_default: |
| 587 |
{ |
| 588 |
\seq_gclear:N \g_sdaps_array_overhangs_left_seq |
| 589 |
\seq_gclear:N \g_sdaps_array_overhangs_right_seq |
| 590 |
} |
| 591 |
|
| 592 |
\cs_new_protected_nopar:Nn \_sdaps_array_row:NNNN |
| 593 |
{ |
| 594 |
% #1: A vbox with baseline on the *first* item containing the row header |
| 595 |
% (\vtop in plain TeX). |
| 596 |
% #2: Data cells packed into an hbox. Each of these needs to be set to the |
| 597 |
% correct width and inserted. |
| 598 |
% #3: The box register to store the resulting hbox in. The depth of this box |
| 599 |
% needs to be correct to calculate the interline glue to the following |
| 600 |
% row. |
| 601 |
% #4: A dim register to store the height of the box in for the purpose of |
| 602 |
% calculating the interline glue in front of the produced row. |
| 603 |
% |
| 604 |
% { \dim_use:N\@totalleftmargin } { \dim_use:N\linewidth } |
| 605 |
% The macro should create an hbox which is exactly \linewidth wide and also |
| 606 |
% contains internal indentation by \@totalleftmargin into the box register #3. |
| 607 |
% The box will be used while in vertical mode and may% be inserted multiple |
| 608 |
% times in the case of the header row. |
| 609 |
% To simplify the iteration it is guaranteed that the data cell boxes are not |
| 610 |
% completely empty. This means the code can simply unbox until it sees a box |
| 611 |
% that is void. |
| 612 |
|
| 613 |
\seq_gclear:N \g_tmpa_seq |
| 614 |
|
| 615 |
% Insert the boxes into a local hbox to work with them |
| 616 |
\hbox_set:Nn #2 { |
| 617 |
\hbox_unpack:N #2 |
| 618 |
|
| 619 |
% Handle the overhang, note that we modify the \g_sdaps_array_overhangs_right_seq locally only! |
| 620 |
\seq_pop:NNTF \g_sdaps_array_overhangs_right_seq \l_tmpa_tl { |
| 621 |
\dim_set:Nn \l_tmpb_dim { \l_tmpa_tl } |
| 622 |
} { |
| 623 |
\dim_set:Nn \l_tmpb_dim { 0pt } |
| 624 |
} |
| 625 |
% Implicit "last" column that contains the overhang |
| 626 |
\seq_gpop:NNTF \g_sdaps_array_shared_colwidths_seq \l_tmpa_tl { |
| 627 |
\dim_set:Nn \l_tmpa_dim { \l_tmpa_tl } |
| 628 |
} { |
| 629 |
\dim_set:Nn \l_tmpa_dim { 0pt } |
| 630 |
} |
| 631 |
\dim_set:Nn \l_tmpa_dim { \dim_max:nn { \l_tmpa_dim } { \l_tmpb_dim } } |
| 632 |
|
| 633 |
|
| 634 |
% MAX with stored values (NOTE: sequence only modified in local scope) |
| 635 |
\seq_pop:NNTF \g_sdaps_array_stored_colwidths_seq \l_tmpa_tl { |
| 636 |
\dim_set:Nn \l_tmpb_dim { \l_tmpa_tl } |
| 637 |
} { |
| 638 |
\dim_set:Nn \l_tmpb_dim { 0pt } |
| 639 |
} |
| 640 |
% Store value from this run, and then calculate max with previous run |
| 641 |
\seq_gput_right:Nx \g_tmpa_seq { \dim_use:N \l_tmpa_dim } |
| 642 |
\dim_set:Nn \l_tmpa_dim { \dim_max:nn { \l_tmpa_dim } { \l_tmpb_dim } } |
| 643 |
|
| 644 |
|
| 645 |
% Insert the overhang space |
| 646 |
\hbox_set:Nn \l_tmpa_box { \skip_horizontal:n { \l_tmpa_dim } } |
| 647 |
|
| 648 |
% Now grab the first of the cells, and then loop over the rest |
| 649 |
\box_set_to_last:N #2 |
| 650 |
\bool_do_while:nn { ! \box_if_empty_p:N #2 } { |
| 651 |
% Strip any trailing glue (i.e. space) coming from the user (for the |
| 652 |
% leading side we ensure that \tex_ignorespaces:D is called). |
| 653 |
% Note that this may remove e.g. a trailing \hfill from the user, the |
| 654 |
% user needs to work around that (in the same way as is required in e.g. |
| 655 |
% tabular). |
| 656 |
\hbox_set:Nn #2 { \hbox_unpack:N #2 \unskip } |
| 657 |
|
| 658 |
% Pop the target width for the current box (i.e. we don't globally |
| 659 |
% modify the clist here). |
| 660 |
\seq_gpop:NNTF \g_sdaps_array_shared_colwidths_seq \l_tmpa_tl { |
| 661 |
\dim_set:Nn \l_tmpa_dim { \l_tmpa_tl } |
| 662 |
} { |
| 663 |
\dim_set:Nn \l_tmpa_dim { 0pt } |
| 664 |
} |
| 665 |
% Calculate the maximum width of current and previous items |
| 666 |
\dim_set:Nn \l_tmpa_dim { \dim_max:nn { \box_wd:N #2 } { \l_tmpa_dim } } |
| 667 |
|
| 668 |
|
| 669 |
% MAX with stored values (NOTE: sequence only modified in local scope) |
| 670 |
\seq_pop:NNTF \g_sdaps_array_stored_colwidths_seq \l_tmpa_tl { |
| 671 |
\dim_set:Nn \l_tmpb_dim { \l_tmpa_tl } |
| 672 |
} { |
| 673 |
\dim_set:Nn \l_tmpb_dim { 0pt } |
| 674 |
} |
| 675 |
% Store value from this run, and then calculate max with previous run |
| 676 |
\seq_gput_right:Nx \g_tmpa_seq { \dim_use:N \l_tmpa_dim } |
| 677 |
\dim_set:Nn \l_tmpa_dim { \dim_max:nn { \l_tmpa_dim } { \l_tmpb_dim } } |
| 678 |
|
| 679 |
% Set the box into a new box with the correct width which contains fil |
| 680 |
% to center it. |
| 681 |
\hbox_set_to_wd:Nnn \l_tmpb_box \l_tmpa_dim { \hfil \hbox_unpack:N #2 \hfil } |
| 682 |
|
| 683 |
% This loops works backward, so attach the cell on the right side. |
| 684 |
% We used to make sure that it is layed out in order, but that is now |
| 685 |
% obsolete and doing it out of order is simpler in RTL mode. |
| 686 |
\sdaps_if_rtl:TF { |
| 687 |
% The boxes are shown on the page from LTR |
| 688 |
\hbox_set:Nn \l_tmpa_box { \box_use:N \l_tmpa_box \skip_horizontal:n { \l_sdaps_sdapsarray_colsep_dim } \box_use:N \l_tmpb_box \skip_horizontal:n { \l_sdaps_sdapsarray_colsep_dim } } |
| 689 |
} { |
| 690 |
\hbox_set:Nn \l_tmpa_box { \skip_horizontal:n { \l_sdaps_sdapsarray_colsep_dim } \box_use:N \l_tmpb_box \skip_horizontal:n { \l_sdaps_sdapsarray_colsep_dim } \box_use:N \l_tmpa_box } |
| 691 |
} |
| 692 |
|
| 693 |
% Grab next cell |
| 694 |
\box_set_to_last:N #2 |
| 695 |
} |
| 696 |
|
| 697 |
% Get the coffin out of the nested scope by placing it into the box and |
| 698 |
% placing that into it again ... |
| 699 |
\box_use:N \l_tmpa_box |
| 700 |
} |
| 701 |
\hcoffin_set:Nn \l_tmpa_coffin { \box_use_drop:N #2 } |
| 702 |
|
| 703 |
\seq_gconcat:NNN \g_sdaps_array_shared_colwidths_seq \g_tmpa_seq \g_sdaps_array_shared_colwidths_seq |
| 704 |
\seq_gclear:N \g_tmpa_seq |
| 705 |
|
| 706 |
% Calculate the space that is left for the header column |
| 707 |
\dim_set:Nn \l_tmpa_dim { \linewidth - \coffin_wd:N \l_tmpa_coffin - 2\l_sdaps_sdapsarray_colsep_dim } |
| 708 |
\tl_gset:Nx \g_sdaps_array_local_data_new_tl { \dim_use:N \l_tmpa_dim } |
| 709 |
|
| 710 |
% TODO: The \hfil here is a hack to prevent a warning if the vbox is empty. |
| 711 |
% Unfortunately checking for an empty box does not work for some reason. |
| 712 |
\dim_set:Nn \l_tmpb_dim { \box_ht:N #1 } |
| 713 |
\sdaps_if_rtl:TF { |
| 714 |
\hcoffin_set:Nn \l_tmpb_coffin { \hbox_to_wd:nn \l_tmpa_dim { \hfil \vbox:n { \vbox_unpack_drop:N #1 } } \skip_horizontal:n { \l_sdaps_sdapsarray_colsep_dim } } |
| 715 |
\tl_set:Nn \l_tmpa_tl { l } |
| 716 |
\tl_set:Nn \l_tmpb_tl { r } |
| 717 |
} { |
| 718 |
\hcoffin_set:Nn \l_tmpb_coffin { \skip_horizontal:n { \l_sdaps_sdapsarray_colsep_dim } \hbox_to_wd:nn \l_tmpa_dim { \hfil \vbox:n { \vbox_unpack_drop:N #1 } } } |
| 719 |
\tl_set:Nn \l_tmpa_tl { r } |
| 720 |
\tl_set:Nn \l_tmpb_tl { l } |
| 721 |
} |
| 722 |
\dim_set:Nn \l_tmpa_dim { \coffin_ht:N \l_tmpb_coffin } |
| 723 |
|
| 724 |
% If the first/last baseline differ then center the vbox, otherwise align the |
| 725 |
% baseline with the cells |
| 726 |
\dim_compare:nNnTF { \l_tmpa_dim } = { \l_tmpb_dim } { |
| 727 |
\coffin_join:NnVNnVnn \l_tmpb_coffin { H } \l_tmpa_tl \l_tmpa_coffin { H } \l_tmpb_tl { \l_sdaps_sdapsarray_colsep_dim } { 0pt } |
| 728 |
\dim_set:Nn #4 { \coffin_ht:N \l_tmpb_coffin } |
| 729 |
} { |
| 730 |
\coffin_join:NnVNnVnn \l_tmpb_coffin { vc } \l_tmpa_tl \l_tmpa_coffin { vc } \l_tmpb_tl { \l_sdaps_sdapsarray_colsep_dim } { 0pt } |
| 731 |
% XXX: Assume that the header is higher than the content cells |
| 732 |
\dim_set:Nn #4 { \l_tmpb_dim } |
| 733 |
} |
| 734 |
|
| 735 |
\hbox_set:Nn #3 { \skip_horizontal:N \@totalleftmargin \coffin_typeset:Nnnnn \l_tmpb_coffin { H } { l } { 0pt } { 0pt } } |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
\prop_gput:Nnn \g__sdaps_array_layouter_prop { default } { |
| 740 |
begin = { \_sdaps_array_begin_default: }, |
| 741 |
row_start = { \_sdaps_array_row_start_default: }, |
| 742 |
rowhead = { \_sdaps_array_rowhead_default:Nw }, |
| 743 |
colhead = { \_sdaps_array_cell_default:Nw }, |
| 744 |
cell = { \_sdaps_array_cell_default:Nw }, |
| 745 |
row = { \_sdaps_array_row:NNNN }, |
| 746 |
end = { \_sdaps_array_end_default: }, |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
\prop_gput:Nnn \g__sdaps_array_layouter_prop { rotated } { |
| 751 |
begin = { \_sdaps_array_begin_default: }, |
| 752 |
row_start = { \_sdaps_array_row_start_default: }, |
| 753 |
rowhead = { \_sdaps_array_rowhead_default:Nw }, |
| 754 |
colhead = { \_sdaps_array_cell_rotated:Nw }, |
| 755 |
cell = { \_sdaps_array_cell_default:Nw }, |
| 756 |
row = { \_sdaps_array_row:NNNN }, |
| 757 |
end = { \_sdaps_array_end_default: }, |
| 758 |
} |
| 759 |
|
| 760 |
% \end{macrocode} |
| 761 |
% |
| 762 |
% \subsection{Exporting a tabular/array like environment} |
| 763 |
% |
| 764 |
% \subsubsection{Helper required for the environment} |
| 765 |
% |
| 766 |
% \begin{macrocode} |
| 767 |
|
| 768 |
\bool_new:N \g_sdaps_array_first_row_bool |
| 769 |
\bool_new:N \l__sdaps_sdapsarray_in_top_group_bool |
| 770 |
\bool_new:N \l__sdaps_sdapsarray_have_content_bool |
| 771 |
\bool_new:N \l_sdaps_sdapsarray_flip_bool |
| 772 |
\tl_new:N \l_sdaps_sdapsarray_layouter_tl |
| 773 |
\tl_new:N \l_sdaps_sdapsarray_align_tl |
| 774 |
\bool_new:N \l_sdaps_sdapsarray_keepenv_bool |
| 775 |
\int_new:N \l_sdaps_sdapsarray_angle_int |
| 776 |
\tl_new:N \l_sdaps_sdapsarray_angle_sine_tl |
| 777 |
\dim_new:N \l_sdaps_sdapsarray_colsep_dim |
| 778 |
\dim_new:N \l_sdaps_sdapsarray_rowsep_dim |
| 779 |
\bool_new:N \l_sdaps_sdapsarray_no_header_bool |
| 780 |
|
| 781 |
\keys_define:nn { sdaps / sdapsarray } |
| 782 |
{ |
| 783 |
flip .bool_set:N = \l_sdaps_sdapsarray_flip_bool, |
| 784 |
flip .initial:n = false, |
| 785 |
flip .default:n = true, |
| 786 |
layouter .tl_set:N = \l_sdaps_sdapsarray_layouter_tl, |
| 787 |
layouter .initial:n = default, |
| 788 |
align .tl_set:N = \l_sdaps_sdapsarray_align_tl, |
| 789 |
align .initial:n = { }, |
| 790 |
keepenv .bool_set:N = \l_sdaps_sdapsarray_keepenv_bool, |
| 791 |
keepenv .initial:n = false, |
| 792 |
keepenv .default:n = true, |
| 793 |
no_header .bool_set:N = \l_sdaps_sdapsarray_no_header_bool, |
| 794 |
no_header .initial:n = false, |
| 795 |
no_header .default:n = true, |
| 796 |
|
| 797 |
angle .code:n = { |
| 798 |
\int_set:Nn \l_sdaps_sdapsarray_angle_int {#1} |
| 799 |
\tl_set:Nx \l_sdaps_sdapsarray_angle_sine_tl { \fp_to_decimal:n {sind(#1)}} |
| 800 |
}, |
| 801 |
angle .initial:n = 70, |
| 802 |
colsep .dim_set:N = \l_sdaps_sdapsarray_colsep_dim, |
| 803 |
colsep .initial:n = 6pt, |
| 804 |
rowsep .dim_set:N = \l_sdaps_sdapsarray_rowsep_dim, |
| 805 |
rowsep .initial:n = 0pt, |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
% \end{macrocode} |
| 810 |
% |
| 811 |
% \subsubsection{Environment definition} |
| 812 |
% |
| 813 |
% \begin{macrocode} |
| 814 |
|
| 815 |
\cs_new_nopar:Nn \l_sdaps_sdapsarray_alignment_set_have_content: |
| 816 |
{ |
| 817 |
\bool_set_true:N\l__sdaps_sdapsarray_have_content_bool |
| 818 |
} |
| 819 |
|
| 820 |
\cs_new_nopar:Nn \_sdaps_sdapsarray_alignment: { |
| 821 |
% End the last group, which will be the group that was begun earlier. |
| 822 |
% If the earlier cell was the first one, then this egroup also starts the |
| 823 |
% hbox to temporarily store the cells. |
| 824 |
\egroup |
| 825 |
\bool_if:NF \l__sdaps_sdapsarray_in_top_group_bool { |
| 826 |
\msg_error:nnn { sdapsarray } { unmatched_grouping_level } { an~alignment~tab } |
| 827 |
} |
| 828 |
|
| 829 |
% We need to notify the outside scope that there are items, will be inserted |
| 830 |
% multiple times, but that does not matter. |
| 831 |
\group_insert_after:N\l_sdaps_sdapsarray_alignment_set_have_content: |
| 832 |
|
| 833 |
% Just in case someone leaked a change into our scope |
| 834 |
\bool_if:NF \l_sdaps_sdapsarray_keepenv_bool { |
| 835 |
\cs_set_eq:NN \cr \_sdaps_sdapsarray_newline: |
| 836 |
\cs_set_eq:NN \\ \cr |
| 837 |
} |
| 838 |
|
| 839 |
% Define a cell now, we can just put everything into a new cell, and that |
| 840 |
% should work fine. |
| 841 |
% Note that cells are not safe for fragile commands at the moment. |
| 842 |
\_sdaps_sdapsarray_cell:w \bgroup |
| 843 |
\bool_set_false:N \l__sdaps_sdapsarray_in_top_group_bool |
| 844 |
\cs_set_eq:NN \\ \cr |
| 845 |
\tex_ignorespaces:D |
| 846 |
} |
| 847 |
|
| 848 |
\msg_new:nnn { sdapsarray } { unmatched_grouping_level } { The~grouping~level~of~a~cell~was~not~even.~Please~ensure~all~braces~are~balanced~out!~This~error~occured~at~#1. } |
| 849 |
\msg_new:nnn { sdapsarray } { unequal_cols } { The~number~of~columns~is~not~equal~for~all~rows. } |
| 850 |
\msg_new:nnn { sdapsarray } { no_new_line_at_end } { You~have~terminated~the~last~line~with~\textbackslash\textbackslash~or~similar.~This~can~have~side~effects,~please~remove~it. } |
| 851 |
\msg_new:nnn { sdapsarray } { wrong_mode } { The~sdapsarray~environment~can~only~function~in~vertical~mode~(both~inner~and~outer). } |
| 852 |
|
| 853 |
\cs_new:Nn \_sdaps_sdapsarray_start_cells: { |
| 854 |
\bool_if:NF \l__sdaps_sdapsarray_in_top_group_bool { |
| 855 |
\msg_error:nnn { sdapsarray } { unmatched_grouping_level } { the~end~of~a~row~header } |
| 856 |
} |
| 857 |
\egroup |
| 858 |
|
| 859 |
% We are in the environment scope again here |
| 860 |
|
| 861 |
% Notify code that we are going to generate cells for a new row. |
| 862 |
\sdaps_array_row_start: |
| 863 |
|
| 864 |
% Start an hbox to stow away the cells. |
| 865 |
% The rest of the setup happens in the alignment handler |
| 866 |
\hbox_set:Nw \l_tmpb_box \bgroup |
| 867 |
\group_insert_after:N \hbox_set_end: |
| 868 |
\bool_set_true:N \l__sdaps_sdapsarray_in_top_group_bool |
| 869 |
} |
| 870 |
|
| 871 |
\cs_new_nopar:Nn \_sdaps_sdapsarray_linestart: { |
| 872 |
\sdaps_array_assign_rowhead:Nw \l_tmpa_box |
| 873 |
\bgroup |
| 874 |
\cs_set_eq:NN \\ \cr |
| 875 |
|
| 876 |
\bool_set_true:N \l__sdaps_sdapsarray_in_top_group_bool |
| 877 |
\bgroup |
| 878 |
\group_insert_after:N \_sdaps_sdapsarray_start_cells: |
| 879 |
\bool_set_false:N \l__sdaps_sdapsarray_in_top_group_bool |
| 880 |
% Ignore following spaces by the user |
| 881 |
\tex_ignorespaces:D |
| 882 |
} |
| 883 |
|
| 884 |
\cs_new_nopar:Nn \_sdaps_sdapsarray_newline: { |
| 885 |
\egroup |
| 886 |
\bool_if:NF \l__sdaps_sdapsarray_in_top_group_bool { |
| 887 |
\msg_error:nnn { sdapsarray } { unmatched_grouping_level } { the~end~of~a~row } |
| 888 |
} |
| 889 |
\egroup |
| 890 |
|
| 891 |
% We are in the environment scope again here |
| 892 |
% Output the last line if the cells were non-empty. |
| 893 |
\bool_if:NT \l__sdaps_sdapsarray_have_content_bool { |
| 894 |
\sdaps_array_row:NN \l_tmpa_box \l_tmpb_box |
| 895 |
} |
| 896 |
\bool_set_false:N \l__sdaps_sdapsarray_have_content_bool |
| 897 |
|
| 898 |
\cs_set_eq:NN \_sdaps_sdapsarray_cell:w \sdaps_array_cell:w |
| 899 |
\_sdaps_sdapsarray_linestart: |
| 900 |
} |
| 901 |
|
| 902 |
\cs_new:Npn\sdaps_array_nested_alignenv: { |
| 903 |
\char_set_catcode_alignment:N & |
| 904 |
\cs_set_eq:NN \cr \sdaps_orig_cr |
| 905 |
\cs_set_eq:NN \\ \sdaps_orig_backslash |
| 906 |
} |
| 907 |
|
| 908 |
\cs_new:Npn\sdaps_array_nested_alignenv:w { |
| 909 |
\bgroup |
| 910 |
\sdaps_array_nested_alignenv: |
| 911 |
% swallow group opening token |
| 912 |
\tex_let:D\next= |
| 913 |
} |
| 914 |
\cs_new_eq:NN \sdapsnested \sdaps_array_nested_alignenv:w |
| 915 |
|
| 916 |
\group_begin: |
| 917 |
\char_set_catcode_active:N & |
| 918 |
\cs_new:Nn \_sdaps_sdapsarray_defines: { |
| 919 |
\cs_set_eq:NN \sdaps_orig_cr \cr |
| 920 |
\cs_set_eq:NN \sdaps_orig_backslash \\ |
| 921 |
\bool_if:NF \l_sdaps_sdapsarray_keepenv_bool { |
| 922 |
\char_set_catcode_active:N & |
| 923 |
\cs_set_eq:NN \cr \sdaps_array_newline: |
| 924 |
\cs_set_eq:NN \\ \cr |
| 925 |
\cs_set_eq:NN & \sdaps_array_alignment: |
| 926 |
} |
| 927 |
} |
| 928 |
\group_end: |
| 929 |
|
| 930 |
%%%%%% |
| 931 |
% Flipping environment |
| 932 |
%%%%%% |
| 933 |
|
| 934 |
% First some helpers |
| 935 |
|
| 936 |
\box_new:N \l_sdaps_sdapsarray_headers_box |
| 937 |
\box_new:N \l_sdaps_sdapsarray_boxlist_head_box |
| 938 |
\box_new:N \l_sdaps_sdapsarray_boxlist_tail_box |
| 939 |
|
| 940 |
\cs_new_protected:Nn \_sdaps_sdapsarray_prepend_box:NN { |
| 941 |
\hbox_set:Nn #2 { |
| 942 |
\box_use:N #1 |
| 943 |
\hbox_unpack:N #2 |
| 944 |
} |
| 945 |
\box_clear:N #1 |
| 946 |
} |
| 947 |
|
| 948 |
\cs_new_protected:Nn \_sdaps_sdapsarray_append_box:NN { |
| 949 |
\hbox_set:Nn #2 { |
| 950 |
\hbox_unpack:N #2 |
| 951 |
\box_use:N #1 |
| 952 |
} |
| 953 |
\box_clear:N #1 |
| 954 |
} |
| 955 |
|
| 956 |
\cs_new_protected:Nn \_sdaps_sdapsarray_pop_last_box:NN { |
| 957 |
\hbox_set:Nn #2 { |
| 958 |
\hbox_unpack:N #2 |
| 959 |
\box_gset_to_last:N \g_tmpa_box |
| 960 |
} |
| 961 |
\box_set_eq:NN #1 \g_tmpa_box |
| 962 |
\box_gclear:N \g_tmpa_box |
| 963 |
} |
| 964 |
|
| 965 |
\cs_new_protected:Nn \_sdaps_sdapsarray_pop_last_hbox_unpack:NN { |
| 966 |
\_sdaps_sdapsarray_pop_last_box:NN #1 #2 |
| 967 |
\hbox_set:Nn #1 { |
| 968 |
\hbox_unpack:N #1 |
| 969 |
\box_gset_to_last:N \g_tmpa_box |
| 970 |
} |
| 971 |
\box_set_eq:NN #1 \g_tmpa_box |
| 972 |
\box_gclear:N \g_tmpa_box |
| 973 |
} |
| 974 |
|
| 975 |
\cs_new_protected:Nn \_sdaps_sdapsarray_boxlist_void_if_empty:N { |
| 976 |
\hbox_set:Nn #1 { |
| 977 |
\hbox_unpack:N #1 |
| 978 |
\box_set_to_last:N #1 |
| 979 |
\box_if_empty:NTF #1 { |
| 980 |
\bool_gset_true:N \g_tmpa_bool |
| 981 |
} { |
| 982 |
\box_use:N #1 |
| 983 |
\bool_gset_false:N \g_tmpa_bool |
| 984 |
} |
| 985 |
} |
| 986 |
\bool_if:NT \g_tmpa_bool { |
| 987 |
\box_clear:N #1 |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
% Lets say we are in row 4, cell 2 as defined in the environment, so the actual |
| 992 |
% position is 2, 4. Minus the row headers, this gives us 2, 3. |
| 993 |
% This means we need to append the cell to the box 3rd last box. |
| 994 |
% In that case we have to append the cell |
| 995 |
|
| 996 |
\cs_new_protected_nopar:Nn \_sdaps_sdapsarray_alignment_flip: |
| 997 |
{ |
| 998 |
\_sdaps_sdapsarray_end_cell_flip: |
| 999 |
|
| 1000 |
% Just in case someone leaked a change into our scope |
| 1001 |
\bool_if:NF \l_sdaps_sdapsarray_keepenv_bool { |
| 1002 |
\cs_set_eq:NN \cr \_sdaps_sdapsarray_newline_flip: |
| 1003 |
\cs_set_eq:NN \\ \cr |
| 1004 |
} |
| 1005 |
|
| 1006 |
% Next up is either a cell or a row header. We can figure that out by checking |
| 1007 |
% that the row headings box is void |
| 1008 |
\box_if_empty:NTF \l_sdaps_sdapsarray_headers_box { |
| 1009 |
\sdaps_array_assign_rowhead:Nw \l_tmpa_box \bgroup |
| 1010 |
\bool_set_false:N \l__sdaps_sdapsarray_in_top_group_bool |
| 1011 |
\cs_set_eq:NN \\ \cr |
| 1012 |
% Ignore following spaces by the user |
| 1013 |
\tex_ignorespaces:D |
| 1014 |
} { |
| 1015 |
\sdaps_array_assign_cell:Nw \l_tmpa_box \bgroup |
| 1016 |
\bool_set_false:N \l__sdaps_sdapsarray_in_top_group_bool |
| 1017 |
\cs_set_eq:NN \\ \cr |
| 1018 |
\tex_ignorespaces:D |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
\cs_new_nopar:Nn \_sdaps_sdapsarray_end_cell_flip: { |
| 1023 |
\egroup % Finish of the cell |
| 1024 |
\bool_if:NF \l__sdaps_sdapsarray_in_top_group_bool { |
| 1025 |
\msg_error:nnn { sdapsarray } { unmatched_grouping_level } { end~of~cell~or~row } |
| 1026 |
} |
| 1027 |
|
| 1028 |
% Get last box from head |
| 1029 |
\_sdaps_sdapsarray_pop_last_box:NN \l_tmpb_box \l_sdaps_sdapsarray_boxlist_head_box |
| 1030 |
% Append the new box to the list of boxes for this row |
| 1031 |
\_sdaps_sdapsarray_append_box:NN \l_tmpa_box \l_tmpb_box |
| 1032 |
% Prepend the new box to the tail |
| 1033 |
\_sdaps_sdapsarray_prepend_box:NN \l_tmpb_box \l_sdaps_sdapsarray_boxlist_tail_box |
| 1034 |
} |
| 1035 |
|
| 1036 |
\cs_new_nopar:Nn \_sdaps_sdapsarray_end_line_flip: { |
| 1037 |
% At the end of the line, move tail into head. |
| 1038 |
% First check that head is empty. |
| 1039 |
\_sdaps_sdapsarray_boxlist_void_if_empty:N \l_sdaps_sdapsarray_boxlist_head_box |
| 1040 |
\box_if_empty:NF \l_sdaps_sdapsarray_boxlist_head_box { |
| 1041 |
\msg_error:nn { sdapsarray } { unequal_cols } |
| 1042 |
} |
| 1043 |
\box_set_eq:NN \l_sdaps_sdapsarray_boxlist_head_box \l_sdaps_sdapsarray_boxlist_tail_box |
| 1044 |
\box_clear:N \l_sdaps_sdapsarray_boxlist_tail_box |
| 1045 |
|
| 1046 |
% If this was the first row, store it away, these are the headings. |
| 1047 |
\box_if_empty:NT \l_sdaps_sdapsarray_headers_box { |
| 1048 |
\box_set_eq:NN \l_sdaps_sdapsarray_headers_box \l_sdaps_sdapsarray_boxlist_head_box |
| 1049 |
\box_clear:N \l_sdaps_sdapsarray_boxlist_head_box |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
\cs_new_nopar:Nn \_sdaps_sdapsarray_newline_flip: { |
| 1054 |
\_sdaps_sdapsarray_end_cell_flip: |
| 1055 |
\_sdaps_sdapsarray_end_line_flip: |
| 1056 |
|
| 1057 |
% Create next box to store away, this has to be a colhead at this point |
| 1058 |
\sdaps_array_assign_colhead:Nw \l_tmpa_box \bgroup |
| 1059 |
\bool_set_false:N \l__sdaps_sdapsarray_in_top_group_bool |
| 1060 |
\cs_set_eq:NN \\ \cr |
| 1061 |
\tex_ignorespaces:D |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
\NewDocumentEnvironment { sdapsarray } { o } |
| 1066 |
{ |
| 1067 |
\bool_set_false:N \l__sdaps_sdapsarray_in_top_group_bool |
| 1068 |
\group_begin: |
| 1069 |
|
| 1070 |
\tl_set:Nn \l_tmpa_tl { } |
| 1071 |
\IfNoValueF { #1 } { |
| 1072 |
\tl_set:Nn \l_tmpa_tl { #1 } |
| 1073 |
} |
| 1074 |
|
| 1075 |
\box_clear:N \l_tmpa_box |
| 1076 |
\box_clear:N \l_tmpb_box |
| 1077 |
|
| 1078 |
% Ensure vertical mode. |
| 1079 |
\tex_par:D |
| 1080 |
\if_mode_vertical: |
| 1081 |
\else: |
| 1082 |
\msg_error:nn { sdapsarray } { wrong_mode } |
| 1083 |
\fi |
| 1084 |
|
| 1085 |
% This needs to be initialized here as otherwise the values would be |
| 1086 |
% expanded at import time. |
| 1087 |
\keys_set:nV { sdaps / sdapsarray } \l_tmpa_tl |
| 1088 |
|
| 1089 |
\sdaps_array_begin:VV \l_sdaps_sdapsarray_layouter_tl \l_sdaps_sdapsarray_align_tl |
| 1090 |
|
| 1091 |
% Note, this environment is fragile; we redefine & to be active. |
| 1092 |
% One can go back into normal mode by using \sdapsnested{} though. |
| 1093 |
|
| 1094 |
\bool_if:NTF \l_sdaps_sdapsarray_flip_bool { |
| 1095 |
\cs_set_eq:NN \sdaps_array_newline: \_sdaps_sdapsarray_newline_flip: |
| 1096 |
\cs_set_eq:NN \sdaps_array_alignment: \_sdaps_sdapsarray_alignment_flip: |
| 1097 |
|
| 1098 |
\_sdaps_sdapsarray_defines: |
| 1099 |
|
| 1100 |
% Two hboxes to hold the content, note that |
| 1101 |
% a: row headers, b: cells/col headers |
| 1102 |
\box_clear:N \l_sdaps_sdapsarray_headers_box |
| 1103 |
\box_clear:N \l_sdaps_sdapsarray_boxlist_head_box |
| 1104 |
\box_clear:N \l_sdaps_sdapsarray_boxlist_tail_box |
| 1105 |
|
| 1106 |
% not sure why we need this group, but nothing works without it |
| 1107 |
\bgroup |
| 1108 |
% This is a bit creative to say the least |
| 1109 |
\sdaps_array_row_start: |
| 1110 |
|
| 1111 |
\bool_set_true:N \l__sdaps_sdapsarray_in_top_group_bool |
| 1112 |
\sdaps_array_assign_rowhead:Nw \l_tmpa_box \bgroup |
| 1113 |
\bool_set_false:N \l__sdaps_sdapsarray_in_top_group_bool |
| 1114 |
\cs_set_eq:NN \\ \cr |
| 1115 |
% Ignore following spaces by the user |
| 1116 |
\tex_ignorespaces:D |
| 1117 |
} { |
| 1118 |
\cs_set_eq:NN \sdaps_array_newline: \_sdaps_sdapsarray_newline: |
| 1119 |
\cs_set_eq:NN \sdaps_array_alignment: \_sdaps_sdapsarray_alignment: |
| 1120 |
\_sdaps_sdapsarray_defines: |
| 1121 |
|
| 1122 |
\cs_set_eq:NN \_sdaps_sdapsarray_cell:w \sdaps_array_colhead:w |
| 1123 |
\_sdaps_sdapsarray_linestart: |
| 1124 |
} |
| 1125 |
|
| 1126 |
% If we redefine &, then the next character might have the wrong catcode |
| 1127 |
% (i.e. it could still be an alignment character). Execute the alignment |
| 1128 |
% code directly if the next character is &. |
| 1129 |
\bool_if:NF \l_sdaps_sdapsarray_keepenv_bool { |
| 1130 |
\peek_charcode_remove_ignore_spaces:NT & { \sdaps_array_alignment: } |
| 1131 |
} |
| 1132 |
} |
| 1133 |
{ |
| 1134 |
\bool_if:NTF \l_sdaps_sdapsarray_flip_bool { |
| 1135 |
\_sdaps_sdapsarray_end_cell_flip: |
| 1136 |
|
| 1137 |
% At this point we should have swallowed all items from the head list. |
| 1138 |
% If not, then someone likely add a stray \\ command or similar |
| 1139 |
\_sdaps_sdapsarray_boxlist_void_if_empty:N \l_sdaps_sdapsarray_boxlist_head_box |
| 1140 |
\box_if_empty:NTF \l_sdaps_sdapsarray_boxlist_head_box { |
| 1141 |
\_sdaps_sdapsarray_end_line_flip: |
| 1142 |
} { |
| 1143 |
\msg_error:nn { sdapsarray } { no_new_line_at_end } |
| 1144 |
} |
| 1145 |
|
| 1146 |
% Now we can have fun! |
| 1147 |
% Pop cells and heading, until we cannot find any new ones. |
| 1148 |
\_sdaps_sdapsarray_pop_last_hbox_unpack:NN \l_tmpa_box \l_sdaps_sdapsarray_headers_box |
| 1149 |
\_sdaps_sdapsarray_pop_last_box:NN \l_tmpb_box \l_sdaps_sdapsarray_boxlist_head_box |
| 1150 |
\bool_do_while:nn { ! \box_if_empty_p:N \l_tmpa_box || ! \box_if_empty_p:N \l_tmpb_box } { |
| 1151 |
\sdaps_array_row:NN \l_tmpa_box \l_tmpb_box |
| 1152 |
|
| 1153 |
\sdaps_array_row_start: |
| 1154 |
|
| 1155 |
\_sdaps_sdapsarray_pop_last_hbox_unpack:NN \l_tmpa_box \l_sdaps_sdapsarray_headers_box |
| 1156 |
\_sdaps_sdapsarray_pop_last_box:NN \l_tmpb_box \l_sdaps_sdapsarray_boxlist_head_box |
| 1157 |
} |
| 1158 |
|
| 1159 |
\egroup |
| 1160 |
} { |
| 1161 |
\egroup |
| 1162 |
|
| 1163 |
\bool_if:NF \l__sdaps_sdapsarray_in_top_group_bool { |
| 1164 |
\msg_error:nnn { sdapsarray } { unmatched_grouping_level } { the~end~of~the~environment } |
| 1165 |
} |
| 1166 |
\egroup |
| 1167 |
|
| 1168 |
% We are in the environment scope again here |
| 1169 |
% Output the last line if the cells were non-empty. |
| 1170 |
\bool_if:NT \l__sdaps_sdapsarray_have_content_bool { |
| 1171 |
\sdaps_array_row:NN \l_tmpa_box \l_tmpb_box |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
\sdaps_array_end: |
| 1176 |
|
| 1177 |
\group_end: |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
\ExplSyntaxOff |
| 1184 |
|
| 1185 |
% |
| 1186 |
% \end{macrocode} |
| 1187 |
% |
| 1188 |
|
| 1189 |
% \Finale |
| 1190 |
\endinput |