summaryrefslogtreecommitdiff
path: root/macros/latex-dev/base/lttagging.dtx
blob: 6887830abdb995a75d1ae0bb2fb5a24b3d5dc465 (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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
% \iffalse meta-comment
%
% Copyright (C) 2023-2025
% The LaTeX Project and any individual authors listed elsewhere
% in this file.
%
% This file is part of the LaTeX base system.
% -------------------------------------------
%
% It 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
%    https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008 or later.
%
% This file has the LPPL maintenance status "maintained".
%
% The list of all files belonging to the LaTeX base distribution is
% given in the file `manifest.txt'. See also `legal.txt' for additional
% information.
%
% The list of derived (unpacked) files belonging to the distribution
% and covered by LPPL is defined by the unpacking scripts (with
% extension .ins) which are part of the distribution.
%
% \fi
%
% \iffalse
%%% From File: lttagging.dtx
%
%<*driver>
% \fi
\ProvidesFile{lttagging.dtx}
             [2025/02/16 v1.0n LaTeX Kernel (tagging support)]
% \iffalse
\documentclass{l3doc}
\GetFileInfo{lttagging.dtx}
\title{\filename}
\date{\filedate}
\author{\LaTeX{} project}

\begin{document}
% \MaintainedByLaTeXTeam{latex}     % should be added again the moment
                                    % it is supported by l3doc
 \maketitle
 \DocInput{\filename}
\end{document}
%</driver>
% \fi
%
% \providecommand\env[1]{\texttt{#1}}
%
% \providecommand\hook[1]{\texttt{#1\DescribeHook[noprint]{#1}}}
% \providecommand\socket[1]{\texttt{#1\DescribeSocket[noprint]{#1}}}
% \providecommand\plug[1]{\texttt{#1\DescribePlug[noprint]{#1}}}
%
% \ProvideDocElement[printtype=\textit{socket},idxtype=socket,idxgroup=Sockets]{Socket}{socketdecl}
% \ProvideDocElement[printtype=\textit{hook},idxtype=hook,idxgroup=Hooks]{Hook}{hookdecl}
% \ProvideDocElement[printtype=\textit{plug},idxtype=plug,idxgroup=Plugs]{Plug}{plugdecl}
%
%
%
%    \begin{macrocode}
%<*2ekernel|latexrelease>
%    \end{macrocode}
%
%    \begin{macrocode}
\ExplSyntaxOn
%    \end{macrocode}
%
%
% \section{General support for tagged output}
%
%  \DescribeMacro\SuspendTagging
%  \DescribeMacro\ResumeTagging
%  \DescribeMacro\tag_suspend:n
%  \DescribeMacro\tag_resume:n

%
%  The are places in code where it is import top stop any tagging
%  activities, e.g., when we are doing trial typesetting that it is
%  done several times. In such a case one must tag only the final
%  version that is actually used, otherwise tagging structures are
%  generated which then do not end up in the PDF and confuse the
%  mechanism. For this we have two commands that can be used in
%  packages: \cs{SuspendTagging} and \cs{ResumeTagging} (with corresponding
%  L3 programming layer commands). They are
%  available as part of the \LaTeX{} kernel, so that they can be
%  safely used in packages whether or not tagging is requested. They
%  all take a string argument that is used for debugging to easily
%  identify why tagging was suspended or restarted, for example, in
%  \pkg{tabularx} you find \verb=\SuspendTagging{tabularx}=. By default
%  these four commands do nothing.
%
%  The argument is used literally (in \cs{typeout} messages) without any
%  expansion when  debugging is turned on and otherwise it is not used at all.
%  This means it is safe to write something like
%  \verb=\SuspendTagging{\foo}= or even \verb=\SuspendTagging\foo=
%  which means \LaTeX{} has to parse only
%  a single token instead of putting a string of characters into the
%  argument. This means a tiny speed improvement but with many such
%  debugging strings\ldots
%
%
% \DescribeMacro\UseTaggingSocket
% \DescribeMacro\tag_socket_use:n
% \DescribeMacro\tag_socket_use:nn
% \DescribeMacro\tag_socket_use:nnn
% Given that we sometimes have to suspend tagging, it would be fairly
% inefficient to put different plugs into these sockets whenever that
% happens. We therefore offer \cs{UseTaggingSocket} which is like
% \cs{UseSocket} except that is expects a socket starting with
% \texttt{tagsupport/} but the socket name is specified without
% this prefix, i.e.,
% \begin{quote}
%   \verb=\UseTaggingSocket{foo}=      $\to$
%   \verb=\UseSocket{tagsupport/foo}=
% \end{quote}.
%
% Beside being slightly shorter, the big advantage is that this way
% we can change \cs{UseTaggingSocket} to do nothing by switching a boolean
% instead of changing the plugs of the tagging support sockets back and forth.
%
% Usually, these sockets have (beside the default plug defined for every socket)
% one additional plug defined and directly assigned. This plug is used when
% tagging is active.
% There may be more plugs, e.g., tagging with special debugging or special behavior
% depending on the class or PDF version etc., but right now it is usually just on or off.
%
% When tagging is suspended they all have the same predefined behavior:
% The sockets with zero arguments do nothing. The sockets with one argument
% gobble their argument. The sockets with two arguments
% will drop their first argument and pass the second unchanged.
%
% It is possible to use the tagging support sockets with
% \cs{UseSocket} directly, but in this case the socket remains active
% if \cs{SuspendTagging} is in force. There may be reasons for doing
% that but in general we expect to always use \cs{UseTaggingSocket}.
%
% \DescribeMacro\UseExpandableTaggingSocket
% \DescribeMacro\tag_socket_use_expandable:n
%  For special cases like in some \cs{halign} contexts we need a fully expandable
%  version of the command. For these cases, \cs{UseExpandableTaggingSocket} can be
%  used. To allow being expandable, it does not output any debugging information
%  if \cs{DebugSocketsOn} is in effect and therefore should be avoided whenever possible.
%
%  The L3 programming layer versions \cs{tag_socket_use_expandable:n},
%  \cs{tag_socket_use:n}, \cs{tag_socket_use:nn}, and \cs{tag_socket_use:nnn}
%  are slightly more efficient than
%  \cs{UseTaggingSocket} because they do not have to determine how
%  many arguments the socket takes when disabling it.
%
% \DescribeMacro\MathCollectTrue
% \DescribeMacro\MathCollectFalse
% The tagging of math has to collect/grab the math first. This is not wanted
% for all uses of \verb+$+. These command allow to control the behavior of the
% math shift token. Without the  math tagging code they do nothing. Their behavior
% with the math tagging code is documented in latex-lab-math.pdf 
%  
% 
% \MaybeStop{}
%
%
% \section{Implementation}
%
%
%
%  \begin{macro}{\tag_suspend:n,\tag_resume:n,\SuspendTagging,\ResumeTagging}
%
%    In the kernel, these commands get dummy definitions so that
%    they can be used without harm in packages. The real definition is
%    used when tagging gets enabled.
%    \begin{macrocode}
\cs_new_eq:NN \tag_suspend:n \use_none:n
\cs_new_eq:NN \tag_resume:n  \use_none:n
\cs_new_protected:Npn \SuspendTagging #1 { \tag_suspend:n {#1} }
\cs_new_protected:Npn \ResumeTagging #1  { \tag_resume:n {#1} }
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\tag_socket_use:n,
%                \tag_socket_use:nn,
%                \tag_socket_use:nnn,
%                \tag_socket_use_expandable:n,
%                \UseTaggingSocket,
%                \UseExpandableTaggingSocket,
%               }
%    Again this is not the final definition for the kernel; it is just
%    a version to get going while some parts of the kernel support are
%    still missing.
% \changes{v1.0l}{2024/11/21}{Define \cs{tag_if_active:TF} conditionals here (github/1558)}
%    \begin{macrocode}
\prg_new_conditional:Npnn \tag_if_active: { p , T , TF, F }
     { \prg_return_false: }
%    \end{macrocode}
%    Dummy definitions in the kernel.
%    These definitions will get updated in \pkg{tagpdf}.
%    The default in the kernel is simply to get rid of the first argument, while
%    the second argument is preserved if present:
% \changes{v1.0k}{2024/10/21}{Changed behavior of two argument tagging sockets when disabled.}
% \changes{v1.0k}{2024/10/21}{Added expandable variants}
%    \begin{macrocode}
\cs_new:Npn \tag_socket_use_expandable:n #1 { }
\cs_new_protected:Npn \tag_socket_use:n #1 { }
\cs_new_protected:Npn \tag_socket_use:nn #1#2 { }
\cs_new_protected:Npn \tag_socket_use:nnn #1#2#3 { #3 }
\cs_new_protected:Npn \UseTaggingSocket #1 {
  \int_case:nnF
      { \int_use:c { c__socket_tagsupport/#1_args_int } }
         {
           0 \prg_do_nothing:
           1 \use_none:n
           2 \use_ii:nn
%    \end{macrocode}
%    We do not expect tagging sockets with more than one or two
%    arguments, so for now we only provide those.
%    \begin{macrocode}
         }
         \ERRORusetaggingsocket  % that should get a proper error message
}
%    \end{macrocode}
%    The same as an expandable command:
%    \begin{macrocode}
\cs_new:Npn \UseExpandableTaggingSocket #1 {
  \int_case:nnF
      { \int_use:c { c__socket_tagsupport/#1_args_int } }
         {
           0 \prg_do_nothing:
           1 \use_none:n
           2 \use_ii:nn
         }
         \ERRORusetaggingsocket  % that should get a proper error message
}
%    \end{macrocode}
%
%  \end{macro}
%
% \subsection{Math collection}
% The documentation is in latex-lab-math.
% 
% \begin{macro}{\MathCollectTrue,\MathCollectFalse}
%    \begin{macrocode}
\cs_new_protected:Npn\MathCollectTrue{}
\cs_new_protected:Npn\MathCollectFalse{}
%    \end{macrocode}
% \end{macro}
% 
% \subsection{Tagging sockets}
% This collects tagging sockets that should be generally available
% so that they can also be used even if the tagging code is not loaded.
%
% \subsubsection{Tagging support for paragraph setup}
%
% Paragraphs are tagged through the code in the para/hooks. This code is sometimes
% adjusted, e.g. to produce a \enquote{flattened} paragraph or to use a different tag.
% Sockets related to such code parts are collected here.
%
%  \begin{macro}{\l__tag_block_flattened_level_int}
%    The block code needs to know if they are nested blockenvs inside
%    a flattened environment. For this it uses a counter. Inside some contexts,
%    e.g. at the begin of a minipage or a footnote this counter must be reset.
%    We therefore define the counter here so that we can use it in the following
%    socket.
%    \begin{macrocode}
\int_new:N \l__tag_block_flattened_level_int
%    \end{macrocode}
%  \end{macro}
%
% \begin{socketdecl}{tagsupport/para/restore}
% This socket restores the para related settings to their default. It
% should be used in places where ``normal'' paragraph tagging must be ensured, for example
% at the begin of a footnote.
%    \begin{macrocode}
\NewSocket{tagsupport/para/restore}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{plugdecl}{default}
% \changes{v1.0i}{2024/10/10}{Restore also paratagging (tagging/723)}
%    \begin{macrocode}
\NewSocketPlug{tagsupport/para/restore}{default}
 {
   \tl_set:Nn    \l__tag_para_main_tag_tl {text-unit}
   \tl_set_eq:NN \l__tag_para_tag_tl\l__tag_para_tag_default_tl
   \bool_set_false:N\l__tag_para_flattened_bool
   \int_zero:N   \l__tag_block_flattened_level_int
   \bool_set_true:N \l__tag_para_bool
 }
\AssignSocketPlug{tagsupport/para/restore}{default}
%    \end{macrocode}
% \end{plugdecl}
%
% \begin{socketdecl}{tagsupport/para/begin,tagsupport/para/end}
% These sockets are currently defined in tagpdf. They overwrite
% definitions in the latex-lab-block code. There is also a simpler
% definition that probably should be a general socket too.
% TODO: move this into lttagging.
% \end{socketdecl}

% \subsubsection{Tagging socket for targets}

% \begin{socketdecl}{tagsupport/refstepcounter}
% When tagging is active we want to track the current structure number
% when targets are set. This will be mostly used in \cs{refstepcounter}
% but also if targets are set manually.
%    \begin{macrocode}
\NewSocket{tagsupport/recordtarget}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{plugdecl}{kernel (tagsupport/recordtarget)}
%    \begin{macrocode}
%
\NewSocketPlug{tagsupport/recordtarget}{kernel}
  {
    \tl_if_blank:VF \@currentHref
    {
      \prop_gput:Nee
        \g__tag_struct_dest_num_prop
        {\@currentHref}
        {\tag_get:n{struct_num}}
    }
  }
\AssignSocketPlug{tagsupport/recordtarget}{kernel}
\ExplSyntaxOff
%    \end{macrocode}
% \end{plugdecl}
% \subsubsection{Tagging sockets for toc}

% \begin{socketdecl}{tagsupport/toc/contentsline/before,
%  tagsupport/toc/contentsline/after}
%  Tagging sockets at the begin and end of contentsline.
%  They receive \emph{all} contentsline arguments as one argument
%  in four brace groups. The socket code should then use the parts it needs.
%    \begin{macrocode}
\NewSocket{tagsupport/toc/contentsline/before}{1}
\NewSocket{tagsupport/toc/contentsline/after}{1}
%    \end{macrocode}
%    
% \changes{v1.0n}{2025/02/16}{Initialize tagging sockets with 1 or 2 arguments}
%    \begin{macrocode}
\AssignSocketPlug{tagsupport/toc/contentsline/before}{noop}
\AssignSocketPlug{tagsupport/toc/contentsline/after}{noop}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/toc/starttoc/before,
%                    tagsupport/toc/starttoc/after}
% Tagging sockets for the begin and end of start of \cs{@starttoc}.
% They take one argument, the extension.
%    \begin{macrocode}
\NewSocket{tagsupport/toc/starttoc/before}{1}
\NewSocket{tagsupport/toc/starttoc/after}{1}
%    \end{macrocode}
%    
% \changes{v1.0n}{2025/02/16}{Initialize tagging sockets with 1 or 2 arguments}
%    \begin{macrocode}
\AssignSocketPlug{tagsupport/toc/starttoc/before}{noop}
\AssignSocketPlug{tagsupport/toc/starttoc/after}{noop}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/toc/leaders/before,
%                    tagsupport/toc/leaders/after}
% Tagging sockets to make the dot leaders an artifact.
% They do not take an argument.
%    \begin{macrocode}
\NewSocket{tagsupport/toc/leaders/before}{0}
\NewSocket{tagsupport/toc/leaders/after}{0}
%    \end{macrocode}
% \end{socketdecl}
% 
% \subsubsection{Tagging support for marginpar}
% 
% \begin{socketdecl}{tagsupport/marginpar/begin,tagsupport/marginpar/end}
% \changes{v1.0n}{2025/02/21}{move marginpar sockets}
%    \begin{macrocode}
\NewSocket{tagsupport/marginpar/begin}{0}
\NewSocket{tagsupport/marginpar/end}{0}
%    \end{macrocode}
% \end{socketdecl}

%
% \subsubsection{Tagging support for table/tabular packages}
%
% The code uses a number of sockets to inject the tagging
% commands. These can be easily set to a noop-plug in case the
% automated tagging is not wanted.
%
% \begin{socketdecl}{tagsupport/tbl/cell/begin,
%                    tagsupport/tbl/cell/end,
%                    tagsupport/tbl/pcell/end,
%                    tagsupport/tbl/pcell/end,
%                    tagsupport/tbl/row/begin,
%                    tagsupport/tbl/row/end,
%                   }
%     At first sockets for the begin and end of cells and table rows:
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/cell/begin}{0}
\NewSocket{tagsupport/tbl/cell/end}{0}
%    \end{macrocode}
%
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/row/begin}{0}
\NewSocket{tagsupport/tbl/row/end}{0}
%    \end{macrocode}
%    Multi-line cells have their own sockets (as they start out in
%    vertical mode and need different treatment).
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/pcell/begin}{0}
\NewSocket{tagsupport/tbl/pcell/end}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/tbl/init}
%    This socket should be at the begin of the table, inside a group.
%    It is used for settings such as disabling para-tagging inside the
%    table.  This socket
%    can perhaps be merged later into the begin-sockets.
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/init}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/tbl/init/celldata}
%    This socket is used in \cs{tbl_init_cell_data_for_table}, the command
%    that stores and initialize cell data to handle nested tables.
%    It can be used to restore similar tagging related values
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/init/celldata}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/tbl/finalize}
%    To fine tune the structure (change cells to header cells, remove
%    unwanted structures, move a foot to the end, etc.). We also need a
%    socket that is executed at the end of the table but \emph{before}
%    all the variables are restored to the outer or default values.
%    The code in the socket can make assignments, but probably
%    shouldn't do typesetting and not write whatsits.
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/finalize}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/tbl/restore/celldata}
%    This socket is used in \cs{tbl_restore_outer_cell_data:}, the command
%    that restores cell data when quitting a nested table. It can be used to restore
%    similar tagging related values
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/restore/celldata}{0}
%    \end{macrocode}
% \end{socketdecl}

% \begin{socketdecl}{tagsupport/tbl/colspan}
%    This socket is used to manage spanning cells, e.g., a
%    \cs{multicolumn}. It expects one argument (the number of cells
%    spanned) and if tagging is enabled set appropriate tag attributes
%    in the background. We probably need a similar socket for row
%    spans eventually.
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/colspan}{1}
%    \end{macrocode}
%    
% \changes{v1.0n}{2025/02/16}{Initialize tagging sockets with 1 or 2 arguments}
%    \begin{macrocode}
\AssignSocketPlug{tagsupport/tbl/colspan}{noop}
%    \end{macrocode}
% \end{socketdecl}
%


% \begin{socketdecl}{tagsupport/tbl/hmode/begin,
%                    tagsupport/tbl/hmode/end,
%                    tagsupport/tbl/vmode/begin,
%                    tagsupport/tbl/vmode/end
%                   }
%
%    These sockets are used in the begin and end code of environments,
%    to allow a fast enabling and disabling of the tagging. We
%    distinguish between tables that can be used inside paragraphs and
%    standalone tables such as \env{longtable} that are always in
%    vertical mode.
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/hmode/begin}{0}
\NewSocket{tagsupport/tbl/hmode/end}{0}
\NewSocket{tagsupport/tbl/vmode/begin}{0}
\NewSocket{tagsupport/tbl/vmode/end}{0}
%    \end{macrocode}
% \end{socketdecl}
%
%
%
% \begin{socketdecl}{tagsupport/tbl/longtable/init,
%                    tagsupport/tbl/longtable/finalize}
%    \env{longtable} needs its own sockets to fine tune the structure.
%    Simply switching the plug in the previous socket interferes with
%    enabling/disabling the tagging.
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/longtable/init}{0}
\NewSocket{tagsupport/tbl/longtable/finalize}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/tbl/longtable/head,
%                    tagsupport/tbl/longtable/foot}
%    Header and footer boxes need special handling because they are repeatedly
%    used.
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/longtable/head}{0}
\NewSocket{tagsupport/tbl/longtable/foot}{0}
%    \end{macrocode}
% \end{socketdecl}
%
%
% \begin{socketdecl}{tagsupport/tbl/leaders/begin,
%                    tagsupport/tbl/leaders/end}
%    Sockets around leaders such as rules or dotted lines, that should
%    be tagged as artifacts, used, for example, in \cs{cline}.
% \changes{v1.0c}{2024/07/13}{Sockets for \cs{cline} leaders added (tagging/134)}
%    \begin{macrocode}
\NewSocket{tagsupport/tbl/leaders/begin}{0}
\NewSocket{tagsupport/tbl/leaders/end}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \subsubsection{Tagging Support for floats}
%
% \begin{socketdecl}{tagsupport/float/hmode/begin,
%                    tagsupport/float/hmode/end}
%  These sockets are used if the float is called in
%  hmode.
% \changes{v1.0h}{2024/09/13}{Sockets for floats added}
%    \begin{macrocode}
\NewSocket{tagsupport/float/hmode/begin}{0}
\NewSocket{tagsupport/float/hmode/end}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/float/begin,
%                    tagsupport/float/end}
%  These sockets start and stop the float structure.
%    \begin{macrocode}
\NewSocket{tagsupport/float/begin}{0}
\NewSocket{tagsupport/float/end}{0}
%    \end{macrocode}
% \end{socketdecl}
%
%
% \begin{socketdecl}{tagsupport/caption/begin,
%                    tagsupport/caption/end}
%  These sockets are used in \cs{@makecaption}.
%  They open and close the \texttt{Caption} structure.
%  Their default plugs assume that they are used in
%  vmode. The argument of the begin socket is
%  the structure number of the parent float. If it is
%  empty the current structure number is used.
%    \begin{macrocode}
\NewSocket{tagsupport/caption/begin}{1}
\NewSocket{tagsupport/caption/end}{0}
%    \end{macrocode}
%    
% \changes{v1.0n}{2025/02/16}{Initialize tagging sockets with 1 or 2 arguments}
%    \begin{macrocode}
\AssignSocketPlug{tagsupport/caption/begin}{noop}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/caption/label/begin,
%                    tagsupport/caption/label/end}
%  These sockets are used in \cs{@makecaption} around the
%  label. Their default plugs ensure that
%  the label is outside the paragraph and that
%  the rest of the caption uses flattened para mode. If the
%  caption is not in a hbox, the \texttt{para/begin}
%  socket should follow to properly start the paragraph.
%    \begin{macrocode}
\NewSocket{tagsupport/caption/label/begin}{0}
\NewSocket{tagsupport/caption/label/end}{0}
%    \end{macrocode}
% \end{socketdecl}
%
%
% \subsection{Tagging support for output routines}
%
% \changes{v1.0m}{2025/02/14}{Tagging support for output routines added}
%
% \begin{socketdecl}{tagsupport/build/page/header}
% \changes{v1.0m}{2025/02/14}{Tagging socket added}
% \begin{socketdecl}{tagsupport/build/page/footer}
% \changes{v1.0m}{2025/02/14}{Tagging socket added}
%    These sockets receive the formatted running header/footer in its
%    second argument (the first is not used) following the convention
%    of tagging sockets, i.e., only the second argument is processed
%    if tagging is not actove
%    \begin{macrocode}
\NewSocket{tagsupport/build/page/header}{2}
\NewSocket{tagsupport/build/page/footer}{2}
%    \end{macrocode}
%    
% \changes{v1.0n}{2025/02/16}{Initialize tagging sockets with 1 or 2 arguments}
%    \begin{macrocode}
\NewSocketPlug{tagsupport/build/page/header}{transparent}{#2}
\NewSocketPlug{tagsupport/build/page/footer}{transparent}{#2}
%    \end{macrocode}
%    
%    \begin{macrocode}
\AssignSocketPlug{tagsupport/build/page/header}{transparent}
\AssignSocketPlug{tagsupport/build/page/footer}{transparent}
%    \end{macrocode}
% \end{socketdecl}
% \end{socketdecl}
%
%
%
% \begin{socketdecl}{tagsupport/build/column/outputbox}
% \changes{v1.0m}{2025/02/14}{Tagging socket added}
%    This socket is used to add any missing tagging structures to the
%    \cs{@outputbox} box, if necessary.
%    \begin{macrocode}
\NewSocket{tagsupport/build/column/outputbox}{0}
%    \end{macrocode}
% \end{socketdecl}
%
%
%
% \begin{socketdecl}{tagsupport/build/column/footins}
% \changes{v1.0m}{2025/02/14}{Tagging socket added}
%    This socket is used to add any missing tagging structures to the
%    \cs{footins} box, if necessary.
%    \begin{macrocode}
\NewSocket{tagsupport/build/column/footins}{0}
%    \end{macrocode}
% \end{socketdecl}
%
%
%
%
%
%
% \begin{socketdecl}{tagsupport/page@sofar}
%
%  This socket is declared and used in the \pkg{multicol} output routines.
%  Only listed for reference.
%    \begin{macrocode}
%\NewSocket{tagsupport/page@sofar}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \subsection{Tagging support for math}
%
% \subsubsection{General sockets}
%
% The following sockets are the main math sockets.
%
% \changes{v1.0n}{2025/02/19}{Moved math sockets into lttagging}
%    \begin{macrocode}
\NewSocket{tagsupport/math/inline/begin}{0}
\NewSocket{tagsupport/math/inline/end}{0}
\NewSocket{tagsupport/math/inline/formula/begin}{2}
\NewSocket{tagsupport/math/inline/formula/end}{0}
\NewSocket{tagsupport/math/display/begin}{0}
\NewSocket{tagsupport/math/display/end}{0}
\NewSocket{tagsupport/math/display/formula/begin}{2}
\NewSocket{tagsupport/math/display/formula/end}{0}
\NewSocket{tagsupport/math/display/tag/begin}{0}
\NewSocket{tagsupport/math/display/tag/end}{0}
%    \end{macrocode}
% Assign transparent socket to the sockets with two arguments:
%    \begin{macrocode}
\NewSocketPlug{tagsupport/math/inline/formula/begin}{transparent}{#2}
\AssignSocketPlug{tagsupport/math/inline/formula/begin}{transparent}
\NewSocketPlug{tagsupport/math/display/formula/begin}{transparent}{#2}
\AssignSocketPlug{tagsupport/math/display/formula/begin}{transparent}
%    \end{macrocode}
%
% \subsubsection{Sockets specific for luamml}
% \paragraph{Save sockets}
%  These sockets are wrappers around the \cs{luamml_save:...} commands.
%  They take an argument which should contain the argument of the save command.
%
% \begin{socketdecl}{tagsupport/math/luamml/save/nn}
%  The argument should contain the two arguments of the command.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/save/nn}{1}
\AssignSocketPlug{tagsupport/math/luamml/save/nn}{noop}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/math/luamml/save/nNn}
%  The argument should contain the three arguments of the command.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/save/nNn}{1}
\AssignSocketPlug{tagsupport/math/luamml/save/nNn}{noop}
%    \end{macrocode}
% \end{socketdecl}
%
% \paragraph{Socket to annotate}
% 
% \begin{socketdecl}{tagsupport/math/luamml/annotate/false}
%  These socket can be used for content that should be annotated
%  with \texttt{core=false}
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/annotate/false}{2}
\NewSocketPlug{tagsupport/math/luamml/annotate/false}{transparent}{#2}
\AssignSocketPlug{tagsupport/math/luamml/annotate/false}{transparent}
%    \end{macrocode}
% \end{socketdecl}
% 
% \paragraph{Array sockets} These sockets will be used in \pkg{array} to
% add luamml support to the array environment.
%
% \begin{socketdecl}{tagsupport/math/luamml/array/save}
%
%  This socket will be used in \cs{endarray}. The plug is set by luamml.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/array/save}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/math/luamml/array/finalize}
%
%  This socket will be used in \cs{endarray}. The plug is set by luamml.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/array/finalize}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/math/luamml/array/initcol}
%  This socket will be used in \cs{@classz}. The plug is set by luamml.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/array/initcol}{0}
%    \end{macrocode}
% \end{socketdecl}
%
%
% \begin{socketdecl}{tagsupport/math/luamml/array/finalizecol}
%  This socket will be used in \cs{@classz}. The plug is set by luamml.
% The argument sets the type of the column.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/array/finalizecol}{1}
\AssignSocketPlug{tagsupport/math/luamml/array/finalizecol}{noop}
%    \end{macrocode}
% \end{socketdecl}
%
% \paragraph{Alignment environments}
% Multiline environments like \texttt{align}, \texttt{multline} or \texttt{gather}
% are tagged as \texttt{mtable}.
%
% \begin{socketdecl}{tagsupport/math/luamml/mtable/finalizecol}
% This sockets is used at the end of alignment cells and adds them to
% the row. The argument passes a type like \texttt{last} or \texttt{box}.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/finalizecol}{1}
\AssignSocketPlug{tagsupport/math/luamml/mtable/finalizecol}{noop}
%    \end{macrocode}
% \end{socketdecl}
% 
% 
% \begin{socketdecl}{tagsupport/math/luamml/mtable/finalize}
% This sockets is used at the end of alignment environment to finalize the
% mtable code. It should be used normally with \cs{UseExpandableTaggingSocket}.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/finalize}{1}
\AssignSocketPlug{tagsupport/math/luamml/mtable/finalize}{noop}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/math/luamml/mtable/aligncol}
% This sockets is used in multline to add attributes
% describing the alignment to the left and right.
% It takes an argument, the alignment.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/aligncol}{1}
\AssignSocketPlug{tagsupport/math/luamml/mtable/aligncol}{noop}
%    \end{macrocode}
% \end{socketdecl}

% 
% \begin{socketdecl}{tagsupport/math/luamml/mtable/innertable/save}
% This socket is used in \cs{endaligned} to save the table. It takes no argument.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/innertable/save}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/math/luamml/mtable/smallmatrix/save}
% This socket is used in \cs{endsmallmatrix} to save the table. It takes no argument.
% TODO: Check if this socket and the innertable socket can/should be merged into a more
% generic version.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/smallmatrix/save}{0}
%    \end{macrocode}
% \end{socketdecl}
% 
% \begin{socketdecl}{tagsupport/math/luamml/mtable/innertable/finalize}
% This socket is used e.g. in \cs{endsmallmatrix} and \cs{gathered} to finalize the table. 
% It takes no argument.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/innertable/finalize}{0}
%    \end{macrocode}
% \end{socketdecl}
% 
% \begin{socketdecl}{tagsupport/math/luamml/mtable/tag/save}
% This socket is used to save a tag for later use.
% has been save before.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/tag/save}{0}
%    \end{macrocode}
% \end{socketdecl}

% \begin{socketdecl}{tagsupport/math/luamml/mtable/tag/set}
% This socket should be used when a tag is placed. It inserts a tag that
% has been save before.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/mtable/tag/set}{0}
%    \end{macrocode}
% \end{socketdecl}
% 
% \paragraph{mbox socket}
%
% \begin{socketdecl}{tagsupport/math/luamml/hbox}
%  This socket is used around \cs{hbox}
%  inside an \cs{mbox}, \cs{makebox} and \cs{text} and annotates
%  the content if the \cs{hbox} is used inside math.
%  The real plug is set by luamml but a default plug is defined
%  here so that the socket can also be used if luamml is not used.
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/hbox}{2}
\NewSocketPlug{tagsupport/math/luamml/hbox}{transparent}{#2}
\AssignSocketPlug{tagsupport/math/luamml/hbox}{transparent}
%    \end{macrocode}
% \end{socketdecl}
%
%
% \paragraph{math phantom sockets}
% \changes{1.0m}{2025-01-27}{add sockets for math phantom commands}
% \begin{socketdecl}{tagsupport/math/luamml/finph@nt}
%  This socket handles the annotation of \cs{finph@nt}
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/finph@nt}{2}
\NewSocketPlug{tagsupport/math/luamml/finph@nt}{transparent}{#2}
\AssignSocketPlug{tagsupport/math/luamml/finph@nt}{transparent}
%    \end{macrocode}
% \end{socketdecl}
%
% \begin{socketdecl}{tagsupport/math/luamml/finph@nt}
%  This socket handles the annotation of \cs{finsm@sh}
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/finsm@sh}{2}
\NewSocketPlug{tagsupport/math/luamml/finsm@sh}{transparent}{#2}
\AssignSocketPlug{tagsupport/math/luamml/finsm@sh}{transparent}
%    \end{macrocode}
% \end{socketdecl}
% \paragraph{Artifact root sign}
%
% \begin{socketdecl}{tagsupport/math/luamml/artifact}
% Unicode characters like a root sign should be marked as artifacts
% to avoid duplication, e.g., in derivation if mathml
% structure elements are used that imply the meaning.
%
%    \begin{macrocode}
\NewSocket{tagsupport/math/luamml/artifact}{0}
%    \end{macrocode}
% \end{socketdecl}
%
% \section{For lttab.dtx parked here for now}
%
%
%    \begin{macrocode}
%<@@=tbl>
\ExplSyntaxOn
%    \end{macrocode}
%
%
% \subsection{Variables for row, column and span counting}
%
%  This part needs a decision on names for various integer registers
%  as well as a decision if those should be also made available for
%  \LaTeXe{}-style packages in form of 2e names and or as
%  non-internals for the L3 programming layer.
%
%  At the moment they are all internal but this probably has to change.
%
%  \begin{macro}{
%     \g_@@_col_int,
%     \g_@@_row_int,
%     \g_@@_span_tl,
%     \g_@@_table_cols_tl}
%
%    \cs{g_@@_row_int} holds the current row number in the table. The
%    value \texttt{0} means we haven't yet processed the table
%    preamble (or in case of longtable are just in front of the next
%    chunk to be processed). It is incremented by every \cs{cr}
%    including the one ending the table preamble.
%
%    TODO: due to the gymnastics needed inside the longtable code the
%    row counter is directly exposed there rather than hidden by
%    interfaces. This needs changing when it is decided how to manage
%    these counters.
%
%    \cs{g_@@_col_int} holds the current column number. The value
%    \texttt{0} means we have not yet started the table or just finished a table row
%    (with \verb=\\= typically); any other positive value means we
%    are currently typesetting a cell in that column in some row
%    (denoted by the \cs{g_@@_row_int}).
%
%    In a \cs{multicolumn} it holds the column number of the first
%    spanned column and \cs{g_@@_span_tl} the info how many cells are
%    spanned.
%
%    \cs{g_@@_span_tl} is normally \texttt{1} except in a
%    \cs{multicolumn} cell.
%    \begin{macrocode}
\int_new:N \g_@@_col_int
\int_new:N \g_@@_row_int
\tl_new:N  \g_@@_span_tl
\tl_new:N  \g_@@_table_cols_tl

\tl_gset:Nn \g_@@_span_tl {1}
\tl_gset:Nn \g_@@_table_cols_tl {0}  % indicates outer level
%    \end{macrocode}
%  \end{macro}
%
%
%  \begin{macro}{\l_@@_saved_col_tl,\l_@@_saved_row_tl,
%                \l_@@_saved_span_tl,\l_@@_saved_table_cols_tl}
%
%    Saving the outer values if we are nesting tables is necessary (as
%    the above variables are globally altered). For this we always use
%    token lists because they don't change and we do not need to blow
%    additional integer registers.
%    \begin{macrocode}
\tl_new:N \l_@@_saved_col_tl
\tl_new:N \l_@@_saved_row_tl
\tl_new:N \l_@@_saved_span_tl
\tl_new:N \l_@@_saved_table_cols_tl

\tl_set:Nn \l_@@_saved_col_tl{0}
\tl_set:Nn \l_@@_saved_row_tl{0}
\tl_set:Nn \l_@@_saved_span_tl{1}
\tl_set:Nn \l_@@_saved_table_cols_tl{0}  % indicates outer level
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\g_@@_missingcells_int}
% This will contain the number of missing cells in a row:
%    \begin{macrocode}
\int_new:N \g_@@_missing_cells_int
%    \end{macrocode}
%  \end{macro}
%
%
%
%
%
% \subsection{Tracing/debugging}
%
%  \begin{macro}{\DebugTablesOn,\DebugTablesOff}
%
%    \begin{macrocode}
\def\DebugTablesOn{
  \cs_set_eq:NN \@@_trace:n \typeout
}
\def\DebugTablesOff{
  \cs_set_eq:NN \@@_trace:n \use_none:n
}
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new_eq:NN \@@_trace:n \use_none:n
%    \end{macrocode}
%  \end{macro}
%
%
%
% \subsection{Interface commands}
%
% All interface commands for the cell number determination have to be
% public on some level because they are needed in other packages as
%  well, e.g., longtable. We may or may not also want to provide 2e
%  style names for them.
%
%  \begin{macro}{\tbl_update_cell_data:}
%    Updating cell data in columns after the first means we have to
%    increment the \cs{g_@@_col_int} by the span count of the previous
%    cell (in case it was a \cs{multicolumn}) and then reset the
%    \cs{g_@@_span_tl} to one (as the default).
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_update_cell_data: {
        \int_gadd:Nn \g_@@_col_int { \g_@@_span_tl }
        \tl_gset:Nn  \g_@@_span_tl {1}
}
%    \end{macrocode}
%  \end{macro}
%
%
%
%  \begin{macro}{\tbl_count_table_cols:}
%    Current implementation of \cs{@mkpream} uses the scratch counter
%    \cs{count@} to keep track of the number of toks registers it needs
%    (2 per column), but this can't be used as it counts also
%    insertions made with \verb+!{}+ and \verb+@{}+.
%    So similar as does longtable for \cs{LT@cols} we count the
%    numbers of ampersands instead.
%    \begin{macrocode}
\cs_new:Npn \tbl_count_table_cols:  {
  \seq_set_split:NnV\l_@@_tmpa_seq {&}\@preamble
  \tl_gset:Ne \g_@@_table_cols_tl { \seq_count:N \l_@@_tmpa_seq }
  \@@_trace:n { ==>~ Table~ has~ \g_@@_table_cols_tl \space columns }
}
%    \end{macrocode}
%  \end{macro}
%
%
%  \begin{macro}{\l_@@_tmpa_seq}
%
%    \begin{macrocode}
\seq_new:N \l_@@_tmpa_seq
%    \end{macrocode}
%  \end{macro}
%
%
%
%  \begin{macro}{\tbl_count_missing_cells:n}
%
%    We might have the situation that some table package has not
%    implemented the \cs{tbl_count_table_cols:} in which case
%    \cs{g_@@_table_cols_tl} would always be zero and we would get an
%    error below when we try to determine the missing cells, so bypass
%    that calculation if we aren't doing tagging (there the packages
%    should have the proper code added). Recall that this is code,
%    that is called by \verb=\\= and an old table packagee might rely
%    on whatever the \LaTeX{} kernel offers here.
%    \begin{macrocode}
\cs_new:Npn \tbl_count_missing_cells:n #1 {
  \tag_if_active:T {
    \int_compare:nNnT \g_@@_col_int > 0
      {
        \int_gset:Nn \g_@@_missing_cells_int
            {
              \g_@@_table_cols_tl
            - \g_@@_col_int
            - \g_@@_span_tl
            + 1
            }
        \int_compare:nNnT \g_@@_missing_cells_int < 0 \ERRORmissingcells % should not happen
        \@@_trace:n{==>~
          (#1)~
          This~ row~ needs~
          \int_use:N \g_@@_missing_cells_int \space
          additional~ cell(s)
        }
      }
  }
}
%    \end{macrocode}
%  \end{macro}
%
%
%  \begin{macro}{\tbl_save_outer_table_cols:}
%
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_save_outer_table_cols: {
  \tl_set_eq:NN \l_@@_saved_table_cols_tl  \g_@@_table_cols_tl
}
%    \end{macrocode}
%  \end{macro}
%
%
%  \begin{macro}{\tbl_init_cell_data_for_table:}
%
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_init_cell_data_for_table: {
  \tl_set:No \l_@@_saved_col_tl {\int_use:N \g_@@_col_int }
  \tl_set:No \l_@@_saved_row_tl {\int_use:N \g_@@_row_int }
  \tl_set_eq:NN \l_@@_saved_span_tl  \g_@@_span_tl
%
  \@@_trace:n { ==>~ saved~cell~data:~
                \l_@@_saved_row_tl,
                \l_@@_saved_col_tl,
                \l_@@_saved_span_tl \space
                (
                \int_compare:nNnTF \l_@@_saved_table_cols_tl = 0
                    { outer~ level }
                    { max:~ \l_@@_saved_table_cols_tl }
                )
          }
%    \end{macrocode}
% Tagging has to initialize cell data too.
%    \begin{macrocode}
  \UseTaggingSocket{tbl/init/celldata}
%    \end{macrocode}
%    These are the initial values when starting a table:
%    \begin{macrocode}
  \int_gzero:N \g_@@_row_int
  \int_gzero:N \g_@@_col_int
  \tl_gset:Nn  \g_@@_span_tl {1}
}
%    \end{macrocode}
%  \end{macro}
%
%
%
%
%  \begin{macro}{\tbl_update_cell_data_for_next_row:}
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_update_cell_data_for_next_row: {
      \int_gincr:N \g_@@_row_int          % this row about to start
      \int_gzero:N \g_@@_col_int          % we are before first col
}
%    \end{macrocode}
%  \end{macro}
%
%
%  \begin{macro}{\tbl_init_cell_data_for_row:}
%    If we start processing a cell in the first column  we set
%    \cs{g_@@_col_int} to \texttt{1} as we are no longer "at" but "in"
%    the first column. We also set \cs{g_@@_span_tl} to its default
%    value (not spanning cells).
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_init_cell_data_for_row: {
        \int_gset:Nn \g_@@_col_int {1}
        \tl_gset:Nn  \g_@@_span_tl {1}
}
%    \end{macrocode}
%  \end{macro}
%
%
%  \begin{macro}{\tbl_if_row_was_started:T,
%                \tbl_if_row_was_started:TF}
%    We use \cs{g_@@_col_int} equal zero to indicate that we are just
%    after a TR (i.e.n between rows or at the very beginning of the
%    table). Using the row
%    count is not so good as longtable may split the table in chunks.
%
%    These conditionals have to be expandable (i.e., unprotected) as
%    they are sometimes executed when \TeX{} is scanning inside a table.
%    \begin{macrocode}
\cs_new:Npn \tbl_if_row_was_started:T {
      \int_compare:nNnT \g_@@_col_int > 0
}
\cs_new:Npn \tbl_if_row_was_started:TF {
      \int_compare:nNnTF \g_@@_col_int > 0
}
%    \end{macrocode}
%  \end{macro}
%
%
%
%  \begin{macro}{\tbl_gzero_row_count:,\tbl_gincr_row_count:,\tbl_gdecr_row_count:}
%    This here is basically a temporary interface. What it will be in
%    the end depends on what we decide concerning exposing row and
%    column counters, if they stay internal we need something like
%    this here (perhaps using \texttt{gincr} etc, or perhaps some
%    other names in the first place).
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_gzero_row_count: {
  \int_gzero:N \g_@@_row_int
}
\cs_new_protected:Npn \tbl_gincr_row_count: {
  \int_gincr:N \g_@@_row_int
}
\cs_new_protected:Npn \tbl_gdecr_row_count: {
  \int_gdecr:N \g_@@_row_int
}
%    \end{macrocode}
%  \end{macro}
%
%
%  \begin{macro}{\tbl_inbetween_rows:}
%    Again name is not really brilliant so far.
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_inbetween_rows: {
       \int_gzero:N  \g_@@_col_int
}
%    \end{macrocode}
%  \end{macro}
%
%
%
%
%
%
%  \begin{macro}{\tbl_restore_outer_cell_data:}
%
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_restore_outer_cell_data: {
  \int_gset:Nn \g_@@_col_int { \l_@@_saved_col_tl }
  \int_gset:Nn \g_@@_row_int { \l_@@_saved_row_tl }
  \tl_gset_eq:NN \g_@@_span_tl \l_@@_saved_span_tl
  \tl_gset_eq:NN \g_@@_table_cols_tl   \l_@@_saved_table_cols_tl
  \UseTaggingSocket{tbl/restore/celldata}
  \@@_trace:n { ==>~ restored~cell~data:~
                \int_use:N \g_@@_row_int,
                \int_use:N \g_@@_col_int,
                \l_@@_saved_span_tl \space
                (
                \int_compare:nNnTF \g_@@_table_cols_tl = 0
                    { outer~ level }
                    { max:~ \g_@@_table_cols_tl }
                )
              }
}
%    \end{macrocode}
%  \end{macro}
%
%
%
%  \begin{macro}{\tbl_update_multicolumn_cell_data:n}
%    This macro updates \cs{g_@@_col_int} and \cs{g_@@_span_tl} inside
%    a \cs{multicolumn} and possibly calls the tagging socket
%    \texttt{tbl/row/begin}.
%    \begin{macrocode}
\cs_new_protected:Npn \tbl_update_multicolumn_cell_data:n #1 {
%    \end{macrocode}
%    We execute socket for tagging only if this \cs{multicolumn}
%    replaces the preamble of the first column. In that case we also have
%    to set \cs{g_@@_col_int} to 1 because this is no longer done in the
%    preamble for the cell either.
%    \begin{macrocode}
   \int_compare:nNnTF \g_@@_col_int = 0
       {
         \UseTaggingSocket{tbl/row/begin}
         \int_gset:Nn \g_@@_col_int {1}
       }
%    \end{macrocode}
%    If we are in a later column we use \cs{g_@@_span_tl} from the
%    previous column to update.
%    \begin{macrocode}
       {
         \int_gadd:Nn \g_@@_col_int { \g_@@_span_tl }
       }
%    \end{macrocode}
%    Then we set the span value so that it can be use in the next column.
%    \begin{macrocode}
   \tl_gset:Nn \g_@@_span_tl {#1}
}
%    \end{macrocode}
%  \end{macro}
%
%
%
%  \begin{macro}{\tbl_crcr:n}
%    This macro is used instead of the usual \cs{crcr} at the end of a
%    table. It is deliberately defined without protection because it
%    may get expanded by the scanning mechanism of low-level \TeX{}
%    after a final \cs{cr} (aka \verb=\\=) in the table. In that case
%    it shouldn't stop the expansion and the conditional inside will
%    be false, thus it just vanishes without doing anything. If there
%    are missing cells (in which case we also haven't see \cs{cr} yet)
%    the macro \cs{tbl_count_missing_cells:n} is executed and
%    then the row is finished with a final \cs{cr}.
%    \begin{macrocode}
\cs_new:Npn \tbl_crcr:n #1 {
    \int_compare:nNnT \g_@@_col_int > 0
        {
          \tbl_count_missing_cells:n {#1}
        }
%    \end{macrocode}
%    Even if we are at the start of a row we my have to do a \cs{cr},
%    so we do a \cs{crcr} always at the end.
% \changes{v1.0b}{2024/06/10}
%         {Always issue a \cs{crcr} even if we are at the start of a
%          row to avoid problems with tabulary and similar code}
%    \begin{macrocode}
    \crcr
}
%    \end{macrocode}
%  \end{macro}
%
%
%
%    \begin{macrocode}
\ExplSyntaxOff
%<@@=>
%    \end{macrocode}
%
% \changes{v1.0h}{2024/09/20}{moved \cs{@kernel@refstepcounter} into ltxref}
%    This is needed for \pkg{longtable} because \cs{refstepcounter} is
%    setting up a target when \pkg{hyperref} is loaded and we don't
%    want that in \pkg{longtable}.%%
%    Prevent longtable patching by hyperref until hyperref does so automatically:
%    \begin{macrocode}
\def\hyper@nopatch@longtable{}
%    \end{macrocode}
%
%
% Should there be a module?
%
%    \begin{macrocode}
%<latexrelease>\NewModuleRelease{2024/06/01}{lttagging}
%<latexrelease>                 {Tagging support}
%    \end{macrocode}
%
%
%
%
%    \begin{macrocode}
%<latexrelease>\IncludeInRelease{0000/00/00}{lttagging}%
%<latexrelease>                 {Undo tagging support}
%<latexrelease>
%<latexrelease>
%<latexrelease>
%<latexrelease>\EndModuleRelease
%    \end{macrocode}
%
%    \begin{macrocode}
%</2ekernel|latexrelease>
%    \end{macrocode}
%
% \Finale
%