From ac5a96ddb02c62d71eb214ed35e3be9a154d991f Mon Sep 17 00:00:00 2001
From: Norbert Preining <norbert@preining.info>
Date: Sat, 2 Oct 2021 03:03:32 +0000
Subject: CTAN sync 202110020303

---
 macros/latex/contrib/tabularray/tabularray.sty | 247 +++++++++++++++++++------
 1 file changed, 188 insertions(+), 59 deletions(-)

(limited to 'macros/latex/contrib/tabularray/tabularray.sty')

diff --git a/macros/latex/contrib/tabularray/tabularray.sty b/macros/latex/contrib/tabularray/tabularray.sty
index 2e3fa4eb5d..db63c63a3c 100644
--- a/macros/latex/contrib/tabularray/tabularray.sty
+++ b/macros/latex/contrib/tabularray/tabularray.sty
@@ -12,7 +12,7 @@
 
 \NeedsTeXFormat{LaTeX2e}
 \RequirePackage{expl3}
-\ProvidesExplPackage{tabularray}{2021-09-01}{2021N}
+\ProvidesExplPackage{tabularray}{2021-10-01}{2021P}
   {Typeset tabulars and arrays with LaTeX3}
 
 \RequirePackage{xparse}
@@ -52,6 +52,7 @@
 \cs_generate_variant:Nn \tl_const:Nn { ce }
 \cs_generate_variant:Nn \tl_log:n { x }
 \cs_generate_variant:Nn \tl_gput_right:Nn { Nf }
+\cs_generate_variant:Nn \tl_put_left:Nn { Nv }
 \prg_generate_conditional_variant:Nnn \clist_if_in:Nn { Nx } { TF }
 \prg_generate_conditional_variant:Nnn \prop_if_in:Nn { c } { T }
 \prg_generate_conditional_variant:Nnn \str_if_eq:nn { xn } { TF }
@@ -105,11 +106,13 @@
 \cs_new_eq:NN \__tblr_halign_command_c: \centering
 \cs_new_eq:NN \__tblr_halign_command_r: \raggedleft
 
-%% Some counters for row and column numbering
-\newcounter{rownum}
-\newcounter{colnum}
-\newcounter{rowcount}
-\newcounter{colcount}
+%% Some counters for row and column numbering.
+%% We may need to restore all LaTeX counters in measuring and building cells,
+%% so we must not define these counters with \newcounter command.
+\int_new:N \c@rownum
+\int_new:N \c@colnum
+\int_new:N \c@rowcount
+\int_new:N \c@colcount
 
 %% Some dimensions for row and column spacing
 \dim_new:N \abovesep
@@ -369,7 +372,10 @@
 \__tblr_data_new_key:nnn { cell } { halign }       { str }
 \__tblr_data_new_key:nnn { cell } { valign }       { str }
 \__tblr_data_new_key:nnn { cell } { background }   { str }
+\__tblr_data_new_key:nnn { cell } { foreground }   { str }
 \__tblr_data_new_key:nnn { cell } { font }         { str }
+\__tblr_data_new_key:nnn { cell } { mode }         { str }
+\__tblr_data_new_key:nnn { cell } { cmd }          { str }
 \__tblr_data_new_key:nnn { cell } { omit }         { int }
 \__tblr_data_new_key:nnn { cell } { @cell-width }  { dim }
 \__tblr_data_new_key:nnn { cell } { @cell-height } { dim }
@@ -1634,11 +1640,14 @@
     f       .meta:n = { valign = f },
     wd      .code:n = \__tblr_cell_gput:ne { width } {#1},
     bg      .code:n = \__tblr_cell_gput:ne { background } {#1},
+    fg      .code:n = \__tblr_cell_gput:ne { foreground } {#1},
+    font    .code:n = \__tblr_cell_gput:nn { font } { #1 \selectfont },
+    mode    .code:n = \__tblr_cell_gput:nn { mode } {#1},
+    $       .meta:n = { mode = math },
+    $$      .meta:n = { mode = dmath },
+    cmd     .code:n = \__tblr_cell_gput:nn { cmd } {#1},
     preto   .code:n = \__tblr_cell_preto_text:n {#1},
     appto   .code:n = \__tblr_cell_appto_text:n {#1},
-    cmd     .code:n = \__tblr_cell_process_text:n {#1},
-    fg      .code:n = \__tblr_cell_preto_text:n { \color{#1} },
-    font    .code:n = \__tblr_cell_gput:nn { font } { #1 \selectfont },
     unknown .code:n = \__tblr_cell_unknown_key:V \l_keys_key_str,
   }
 
@@ -1686,23 +1695,6 @@
   }
 \cs_generate_variant:Nn \__tblr_cell_appto_text:nnn { nen, enn, een }
 
-\cs_new_protected:Npn \__tblr_cell_process_text:n #1
-  {
-    \__tblr_cell_process_text:een
-      { \int_use:N \c@rownum } { \int_use:N \c@colnum } {#1}
-  }
-
-\cs_new_protected:Npn \__tblr_cell_process_text:nnn #1 #2 #3
-  {
-    \tl_set:Nx \l__tblr_cell_text_tl
-      {
-        { \__tblr_spec_item:nn { text } { [#1][#2] } }
-      }
-    \tl_put_left:Nn \l__tblr_cell_text_tl {#3}
-    \__tblr_spec_gput:nnV { text } { [#1][#2] } \l__tblr_cell_text_tl
-  }
-\cs_generate_variant:Nn \__tblr_cell_process_text:nnn { nen, enn, een }
-
 \cs_new_protected:Npn \__tblr_cell_unknown_key:n #1
   {
     \regex_match:NnTF \c__tblr_is_color_key_regex {#1}
@@ -1911,13 +1903,16 @@
     h         .meta:n = { valign = h },
     f         .meta:n = { valign = f },
     bg        .code:n = \__tblr_column_gput_cell:nn { background } {#1},
-    fg        .code:n = \__tblr_preto_text_for_every_column_cell:n { \color{#1} },
+    fg        .code:n = \__tblr_column_gput_cell:nn { foreground } {#1},
     font      .code:n = \__tblr_column_gput_cell:nn { font } { #1 \selectfont },
+    mode      .code:n = \__tblr_column_gput_cell:nn { mode } {#1},
+    $         .meta:n = { mode = math },
+    $$        .meta:n = { mode = dmath },
+    cmd       .code:n = \__tblr_column_gput_cell:nn { cmd } {#1},
     wd        .code:n = \__tblr_column_gput:ne { width } { \dim_eval:n {#1} },
     co        .code:n = \__tblr_column_gput:ne { coefficient } {#1},
     preto     .code:n = \__tblr_preto_text_for_every_column_cell:n {#1},
     appto     .code:n = \__tblr_appto_text_for_every_column_cell:n {#1},
-    cmd       .code:n = \__tblr_process_text_for_every_column_cell:n {#1},
     leftsep   .code:n = \__tblr_column_gput:ne { leftsep } { \dim_eval:n {#1} },
     rightsep  .code:n = \__tblr_column_gput:ne { rightsep } { \dim_eval:n {#1} },
     colsep    .meta:n = { leftsep = #1, rightsep = #1},
@@ -1969,14 +1964,6 @@
       }
   }
 
-\cs_new_protected:Npn \__tblr_process_text_for_every_column_cell:n #1
-  {
-    \int_step_inline:nn { \c@rowcount }
-      {
-        \__tblr_cell_process_text:nen {##1} { \int_use:N \c@colnum } {#1}
-      }
-  }
-
 \regex_const:Nn \c__tblr_is_number_key_regex { ^[\+\-]? (\d+|\d*\.\d+)$ }
 
 \cs_new_protected:Npn \__tblr_column_unknown_key:n #1
@@ -2071,13 +2058,16 @@
     h         .meta:n = { valign = h },
     f         .meta:n = { valign = f },
     bg        .code:n = \__tblr_row_gput_cell:nn { background } {#1},
-    fg        .code:n = \__tblr_preto_text_for_every_row_cell:n { \color{#1} },
+    fg        .code:n = \__tblr_row_gput_cell:nn { foreground } {#1},
     font      .code:n = \__tblr_row_gput_cell:nn { font } { #1 \selectfont },
+    mode      .code:n = \__tblr_row_gput_cell:nn { mode } {#1},
+    $         .meta:n = { mode = math },
+    $$        .meta:n = { mode = dmath },
+    cmd       .code:n = \__tblr_row_gput_cell:nn { cmd } {#1},
     ht        .code:n = \__tblr_row_gput:ne { height } { \dim_eval:n {#1} },
     co        .code:n = \__tblr_row_gput:ne { coefficient } {#1},
     preto     .code:n = \__tblr_preto_text_for_every_row_cell:n {#1},
     appto     .code:n = \__tblr_appto_text_for_every_row_cell:n {#1},
-    cmd       .code:n = \__tblr_process_text_for_every_row_cell:n {#1},
     abovesep  .code:n = \__tblr_row_gput:ne { abovesep } { \dim_eval:n {#1} },
     belowsep  .code:n = \__tblr_row_gput:ne { belowsep } { \dim_eval:n {#1} },
     rowsep    .meta:n = { abovesep = #1, belowsep = #1},
@@ -2142,14 +2132,6 @@
       }
   }
 
-\cs_new_protected:Npn \__tblr_process_text_for_every_row_cell:n #1
-  {
-    \int_step_inline:nn { \c@colcount }
-      {
-        \__tblr_cell_process_text:enn { \int_use:N \c@rownum } {##1} {#1}
-      }
-  }
-
 \cs_new_protected:Npn \__tblr_row_unknown_key:n #1
   {
     \regex_match:NnTF \c__tblr_is_number_key_regex {#1}
@@ -3397,7 +3379,9 @@
 \cs_new_protected:Npn \__tblr_measure_cell_update_sizes:nnNNNN #1 #2 #3 #4 #5 #6
   {
     \__tblr_get_cell_alignments:nn {#1} {#2}
+    \__tblr_save_counters:n { trial }
     \hbox_set:Nn \l_tmpa_box { \__tblr_get_cell_text:nn {#1} {#2} }
+    \__tblr_restore_counters:n { trial }
     \__tblr_update_cell_size:nnNNNN {#1} {#2} #3 #4 #5 #6
     \__tblr_update_row_size:nnNNN {#1} {#2} #4 #5 #6
     \__tblr_update_col_size:nN {#2} #3
@@ -3416,13 +3400,45 @@
       { \__tblr_get_cell_text_real:nn { #1 } { #2 } }
   }
 
+\tl_new:N \l__tblr_cell_fg_tl
+\tl_new:N \l__tblr_cell_cmd_tl
+\tl_new:N \l__tblr_cell_mode_tl
+\bool_new:N \l__tblr_cell_math_mode_bool
+\tl_const:Nn \l__tblr_cell_math_style_tl  { \relax }
+\tl_const:Nn \l__tblr_cell_imath_style_tl { \textstyle }
+\tl_const:Nn \l__tblr_cell_dmath_style_tl { \displaystyle }
+
 %% Get cell text, #1: row number, #2: column number
 %% If the width of the cell is not set, split it with \\ and compute the width
 %% Therefore we always get a vbox for any cell
 \cs_new_protected:Npn \__tblr_get_cell_text_real:nn #1 #2
   {
     \group_begin:
-    \tl_set:Nx \l__tblr_c_tl { \__tblr_spec_item:ne { text } {[#1][#2]} }
+    \tl_set:Nx \l__tblr_cell_cmd_tl
+      { \__tblr_data_item:neen { cell } {#1} {#2} { cmd } }
+    \tl_if_empty:NTF \l__tblr_cell_cmd_tl
+      { \tl_set:Nx \l__tblr_c_tl { \__tblr_spec_item:ne { text } {[#1][#2]} } }
+      {
+        \tl_set:Nx \l__tblr_c_tl { { \__tblr_spec_item:ne { text } {[#1][#2]} } }
+        \tl_put_left:NV \l__tblr_c_tl \l__tblr_cell_cmd_tl
+      }
+    \tl_set:Nx \l__tblr_cell_mode_tl
+      { \__tblr_data_item:neen { cell } {#1} {#2} { mode } }
+    \tl_if_empty:NT \l__tblr_cell_mode_tl
+      {
+        \bool_if:NTF \l__tblr_math_mode_bool
+          { \tl_set:Nn \l__tblr_cell_mode_tl { math } }
+          { \tl_set:Nn \l__tblr_cell_mode_tl { text } }
+      }
+    \tl_if_eq:NnTF \l__tblr_cell_mode_tl { text }
+      { \bool_set_false:N \l__tblr_cell_math_mode_bool }
+      {
+        \bool_set_true:N \l__tblr_cell_math_mode_bool
+        \tl_put_left:Nv \l__tblr_c_tl
+          { l__tblr_cell_ \l__tblr_cell_mode_tl _style_tl }
+        \tl_put_left:Nn \l__tblr_c_tl { $ }
+        \tl_put_right:Nn \l__tblr_c_tl { $ }
+      }
     \tl_set:Nx \l__tblr_f_tl { \__tblr_data_item:neen { cell } {#1} {#2} { font } }
     \tl_set:Nx \l__tblr_w_tl
       { \__tblr_data_item:neen { cell } {#1} {#2} { width } }
@@ -3437,15 +3453,17 @@
       }
     \dim_compare:nNnT { \l__tblr_w_tl } < { 0pt } % column width unset
       {
-        \bool_if:NTF \l__tblr_math_mode_bool
+        \bool_if:NTF \l__tblr_cell_math_mode_bool
           {
-            \hbox_set:Nn \l_tmpa_box { $\l__tblr_c_tl$ }
+            \hbox_set:Nn \l_tmpa_box { \l__tblr_c_tl }
             \tl_set:Nx \l__tblr_w_tl { \box_wd:N \l_tmpa_box }
           }
           {
             \__tblr_get_cell_size_with_box:
           }
       }
+    \tl_set:Nx \l__tblr_cell_fg_tl
+      { \__tblr_data_item:neen { cell } {#1} {#2} { foreground } }
     \tl_put_left:NV \l__tblr_c_tl \l__tblr_f_tl
     \__tblr_get_vcell_and_sizes:NN \l__tblr_c_tl \l__tblr_w_tl
     \group_end:
@@ -3453,9 +3471,11 @@
 
 \cs_new_protected:Npn \__tblr_get_cell_size_with_box:
   {
+    \__tblr_save_counters:n { cell }
     \tl_if_eq:NnTF \l__tblr_inner_spec_measure_tl { vbox }
       { \__tblr_get_cell_size_with_vbox: }
       { \__tblr_get_cell_size_with_hbox: }
+    \__tblr_restore_counters:n { cell }
   }
 
 %% Varwidth won't work as expected when \color command occurs in it,
@@ -3503,7 +3523,9 @@
 \cs_new_protected:Npn \__tblr_get_vcell_and_sizes:NN #1 #2
   {
     \group_begin:
+    \__tblr_save_counters:n { cell }
     \vbox_set_top:Nn \l_tmpa_box { \__tblr_make_vcell_text:NN #1 #2 }
+    \__tblr_restore_counters:n { cell }
     \vbox_set:Nn \l_tmpb_box { \__tblr_make_vcell_text:NN #1 #2 }
     \dim_gset:Nn \g__tblr_cell_wd_dim { \box_wd:N \l_tmpb_box }
     \dim_gset:Nn \g__tblr_cell_ht_dim
@@ -3555,8 +3577,9 @@
     \cs:w __tblr_halign_command_ \g__tblr_cell_halign_tl : \cs_end:
     \mode_leave_vertical:
     \box_use:N \l__tblr_strut_ht_box
-    \bool_if:NTF \l__tblr_math_mode_bool
-      { $#1$ }
+    \tl_if_empty:NF \l__tblr_cell_fg_tl { \exp_args:NV \color \l__tblr_cell_fg_tl }
+    \bool_if:NTF \l__tblr_cell_math_mode_bool
+      { #1 }
       { \__tblr_rescan_cell_tokens:N #1 }
     \box_use:N \l__tblr_strut_dp_box
   }
@@ -3571,6 +3594,8 @@
     \tl_if_empty:NTF \l__tblr_inner_spec_verb_tl
       { #1 }
       {
+        %% insert space characters after some control sequences first (issue #112)
+        \regex_replace_all:nnN { (\c{[A-Za-z]*}) ([A-Za-z]) } { \1 \  \2 } #1
         \regex_replace_all:nnN { . } { \c{string} \0 } #1
         \tl_set:Nx #1 { #1 \noexpand \empty }
         \exp_args:NV \tex_scantokens:D #1
@@ -4644,7 +4669,7 @@
 %% since we want a correct hangindent caption paragraph.
 
 \DefTblrTemplate { caption-tag } { empty } { }
-\DefTblrTemplate { caption-tag } { normal } { Table \hspace{0.25em} \thetable }
+\DefTblrTemplate { caption-tag } { normal } { \tablename\hspace{0.25em}\thetable }
 \SetTblrTemplate { caption-tag } { normal }
 
 \DefTblrTemplate { caption-sep } { empty } { }
@@ -5386,11 +5411,7 @@
     \vbox_set:Nn \l__tblr_table_box
       {
         \box_use:N \l__tblr_table_head_box
-        \hrule height ~ 0pt
-        \box_use:N \l__tblr_row_head_box
-        \hrule height ~ 0pt
-        \box_use:N \l__tblr_table_box
-        \hrule height ~ 0pt
+        \__tblr_cover_two_vboxes:NN \l__tblr_row_head_box \l__tblr_table_box
         \box_use:N \l__tblr_row_foot_box
         \hrule height ~ 0pt
         \box_use:N \l__tblr_table_foot_box
@@ -5399,6 +5420,22 @@
   }
 \cs_generate_variant:Nn \__tblr_build_page_table:nnn { nnx }
 
+%% To solve the problem of missing hlines of long tables in some PDF readers,
+%% We need to draw body rows before head rows (see issue #88).
+\cs_new_protected:Npn \__tblr_cover_two_vboxes:NN #1 #2
+  {
+    \dim_set:Nn \l_tmpa_dim { \box_ht:N #1 + \box_dp:N #1 }
+    \dim_set:Nn \l_tmpb_dim { \box_ht:N #2 + \box_dp:N #2 }
+    \skip_vertical:N \l_tmpa_dim
+    \hrule height ~ 0pt
+    \box_use:N #2
+    \skip_vertical:n { - \l_tmpa_dim - \l_tmpb_dim }
+    \hrule height ~ 0pt
+    \box_use:N #1
+    \skip_vertical:N \l_tmpb_dim
+    \hrule height ~ 0pt
+  }
+
 \cs_new_protected:Npn \__tblr_halign_whole:Nn #1 #2
   {
     \noindent
@@ -6451,6 +6488,72 @@
     \clist_map_inline:nn {#1} { \use:c { __tblr_use_lib_ ##1: } }
   }
 
+%% Library amsmath and environments +array, +matrix, +cases, ...
+
+\NewTblrLibrary { amsmath }
+  {
+    \RequirePackage { amsmath, environ }
+    \NewTblrEnviron { +array }
+    \SetTblrInner[+array]{colsep = 5pt}
+    \NewEnviron { +matrix } [1] [] {
+      \begin{+array}[expand = \BODY]{
+        column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
+        cells = {c}, ##1
+      }
+        \BODY
+      \end{+array}
+    }
+    \NewEnviron { +bmatrix } [1] [] {
+      \left[\begin{+array}[expand = \BODY]{
+        column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
+        cells = {c}, ##1
+      }
+        \BODY
+      \end{+array}\right]
+      \ignorespacesafterend
+    }
+    \NewEnviron { +Bmatrix } [1] [] {
+      \left\lbrace\begin{+array}[expand = \BODY]{
+        column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
+        cells = {c}, ##1
+      }
+        \BODY
+      \end{+array}\right\rbrace
+    }
+    \NewEnviron { +pmatrix } [1] [] {
+      \left(\begin{+array}[expand = \BODY]{
+        column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
+        cells = {c}, ##1
+      }
+        \BODY
+      \end{+array}\right)
+    }
+    \NewEnviron { +vmatrix } [1] [] {
+      \left\lvert\begin{+array}[expand = \BODY]{
+        column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
+        cells = {c}, ##1
+      }
+        \BODY
+      \end{+array}\right\rvert
+    }
+    \NewEnviron { +Vmatrix } [1] [] {
+      \left\lVert\begin{+array}[expand = \BODY]{
+        column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
+        cells = {c}, ##1
+      }
+        \BODY
+      \end{+array}\right\rVert
+    }
+    \NewEnviron { +cases } [1] [] {
+      \left\lbrace\begin{+array}[expand = \BODY]{
+        column{1} = {leftsep = 0pt}, column{Z} = {rightsep = 0pt},
+        colspec = {ll}, stretch = 1.2, ##1
+      }
+        \BODY
+      \end{+array}\right.
+    }
+  }
+
 %% Library booktabs and commands \toprule, \midrule, \bottomrule
 
 \NewTblrLibrary { booktabs }
@@ -6507,6 +6610,32 @@
       { \SetVspace { abovespace+ = (##1) / 2, belowspace+ = (##1) / 2 } }
   }
 
+%% Library counter for resetting all counters
+
+\tl_new:N \__tblr_saved_trial_counters_tl
+\tl_new:N \__tblr_saved_cell_counters_tl
+
+\cs_new_protected:Npn \__tblr_save_counters:n #1 { }
+\cs_new_protected:Npn \__tblr_restore_counters:n #1 { }
+
+%% We use code from tabularx package for resetting all LaTeX counters,
+%% where internal macro \cl@@ckpt looks like the following:
+%% \@elt{page} \@elt{equation} \@elt{enumi} \@elt{enumii} \@elt{enumiii} ...
+
+\NewTblrLibrary { counter }
+  {
+    \cs_set_protected:Npn \__tblr_save_counters:n ##1
+      {
+        \def \@elt ####1 { \global\value{####1} = \the\value{####1} \relax }
+        \tl_set:cx { __tblr_saved_ ##1 _counters_tl } { \cl@@ckpt }
+        \let \@elt = \relax
+      }
+    \cs_set_protected:Npn \__tblr_restore_counters:n ##1
+      {
+        \tl_use:c { __tblr_saved_ ##1 _counters_tl }
+      }
+  }
+
 %% Library diagbox and command \diagbox
 
 \NewTblrLibrary { diagbox }
@@ -6538,7 +6667,7 @@
 
 \cs_new_protected:Npn \__tblr_lib_diagbox_math_or_text:n #1
   {
-    \bool_if:NTF \l__tblr_math_mode_bool {$#1$} {#1}
+    \bool_if:NTF \l__tblr_cell_math_mode_bool {$#1$} {#1}
   }
 
 \box_new:N \l__tblr_diag_box
@@ -6566,7 +6695,7 @@
     \cs_new_protected:Npn \__tblr_siunitx_setcolumn:n ##1
       {
         \__tblr_column_gput_cell:nn { si } {##1}
-        \__tblr_process_text_for_every_column_cell:n { \TblrNum }
+        \__tblr_column_gput_cell:nn { cmd } { \TblrNum }
       }
     \NewDocumentCommand \TblrNum { m }
       {
-- 
cgit v1.2.3

