\UseTblrLibrary{booktabs}
\UseTblrLibrary{varwidth}
\ExplSyntaxOn
\NewExpandableDocumentCommand \flmxCellsMaxWidth { } { 0.96 \linewidth }
\NewDocumentEnvironment { flmxCells } { O { } +b }
{ \__flmx_cells:nn {#1} {#2} }
{ }
\cs_new_protected:Npn \__flmx_cells:nn #1#2
{
\__flmx_set_keys:n {#1}
\__flmx_find:nn { cells } { }
\use:e
{
\exp_not:v { \l__flmx_found_tl _begin }
\exp_not:n {#2}
\exp_not:v { \l__flmx_found_tl _end }
}
}
\tl_new:N \l__flmx_cells_options_tl
\clist_new:N \l__flmx_cells_specs_clist
\prop_new:N \l__flmx_cell_prop
\tl_new:N \l__flmx_cell_value_tl
\tl_new:N \l__flmx_cell_span_tl
\tl_new:N \l__flmx_cell_keys_tl
\seq_new:N \l__flmx_cell_words_seq
\int_new:N \l__flmx_cell_row_int
\int_new:N \l__flmx_cell_col_int
\int_new:N \l__flmx_cell_rows_int
\int_new:N \l__flmx_cell_cols_int
\box_new:N \l__flmx_cells_box
\int_new:N \l__flmx_cells_depth_int
\cs_generate_variant:Nn \tl_put_right:Nn { Nv }
\cs_generate_variant:Nn \clist_set:Nn { Nv }
\cs_new_protected:Npn \__flmx_cells_end: { }
\cs_new_protected:Npn \__flmx_cells_table:w #1 \__flmx_cells_end:
{
\__flmx_cells_options:
\bool_lazy_or:nnTF
{ \mode_if_inner_p: }
{ \int_compare_p:nNn { \l__flmx_cells_depth_int } > { 0 } }
{
\mode_if_vertical:T { \noindent }
\__flmx_cells_fit:n {#1}
}
{ \__flmx_cells_display:n {#1} }
}
\flmxDefineCellsDefault { \__flmx_cells_table:w } { \__flmx_cells_end: }
\cs_new_protected:Npn \__flmx_cells_display:n #1
{
\par
\begin { center }
\__flmx_cells_fit:n {#1}
\end { center }
}
\cs_new_protected:Npn \__flmx_cells_fit:n #1
{
\int_incr:N \l__flmx_cells_depth_int
\hbox_set:Nn \l__flmx_cells_box
{
\__flmx_cells_tblr:en
{
colspec = { * { \flmxKey { cols } } { l } } ,
\exp_not:V \l__flmx_cells_options_tl
}
{#1}
}
\dim_compare:nNnTF { \box_wd:N \l__flmx_cells_box } > { \flmxCellsMaxWidth }
{
\__flmx_cells_tblr:en
{
width = \flmxCellsMaxWidth ,
colspec = { * { \flmxKey { cols } } { X[-1,l] } } ,
\exp_not:V \l__flmx_cells_options_tl
}
{#1}
}
{ \mode_leave_vertical: \box_use_drop:N \l__flmx_cells_box }
}
\cs_new_protected:Npn \__flmx_cells_tblr:nn #1#2
{ \begin { tblr } {#1} \toprule #2 \bottomrule \end { tblr } }
\cs_generate_variant:Nn \__flmx_cells_tblr:nn { en }
\cs_new_protected:Npn \__flmx_cells_options:
{
\tl_set:Nn \l__flmx_cells_options_tl { hspan = minimal , measure = vbox }
\flmxIfKey { cells }
{
\clist_set:Nv \l__flmx_cells_specs_clist { l__flmx_key_cells_tl }
\clist_map_function:NN \l__flmx_cells_specs_clist \__flmx_cells_spec:n
}
{ }
}
\cs_new_protected:Npn \__flmx_cells_spec:n #1
{
\prop_clear:N \l__flmx_cell_prop
\keyval_parse:nnn { \use_none:n } { \prop_put:Nnn \l__flmx_cell_prop } {#1}
\int_set:Nn \l__flmx_cell_row_int { \__flmx_cell_number:nn { row } { 1 } }
\int_set:Nn \l__flmx_cell_col_int { \__flmx_cell_number:nn { col } { 1 } }
\int_set:Nn \l__flmx_cell_rows_int { \__flmx_cell_number:nn { row-span } { 1 } }
\int_set:Nn \l__flmx_cell_cols_int { \__flmx_cell_number:nn { col-span } { 1 } }
\tl_clear:N \l__flmx_cell_span_tl
\int_compare:nNnT { \l__flmx_cell_rows_int } > { 1 }
{ \tl_put_right:Ne \l__flmx_cell_span_tl { r = \int_use:N \l__flmx_cell_rows_int } }
\int_compare:nNnT { \l__flmx_cell_cols_int } > { 1 }
{
\tl_if_empty:NF \l__flmx_cell_span_tl { \tl_put_right:Nn \l__flmx_cell_span_tl { , } }
\tl_put_right:Ne \l__flmx_cell_span_tl { c = \int_use:N \l__flmx_cell_cols_int }
}
\tl_clear:N \l__flmx_cell_keys_tl
\prop_get:NnNT \l__flmx_cell_prop { align } \l__flmx_cell_value_tl
{
\str_case:VnT \l__flmx_cell_value_tl
{
{ left } { \tl_put_right:Nn \l__flmx_cell_keys_tl { halign = l , } }
{ center } { \tl_put_right:Nn \l__flmx_cell_keys_tl { halign = c , } }
{ right } { \tl_put_right:Nn \l__flmx_cell_keys_tl { halign = r , } }
}
{ }
}
\prop_if_in:NnT \l__flmx_cell_prop { header }
{ \tl_put_right:Nn \l__flmx_cell_keys_tl { font = \bfseries , } }
\prop_get:NnNT \l__flmx_cell_prop { styles } \l__flmx_cell_value_tl
{
\seq_set_split:NnV \l__flmx_cell_words_seq { ~ } \l__flmx_cell_value_tl
\seq_map_inline:Nn \l__flmx_cell_words_seq
{
\tl_if_exist:cT { l__flmx_cellstyle_##1_tl }
{
\tl_put_right:Nv \l__flmx_cell_keys_tl { l__flmx_cellstyle_##1_tl }
\tl_put_right:Nn \l__flmx_cell_keys_tl { , }
}
}
}
\bool_lazy_and:nnF
{ \tl_if_empty_p:N \l__flmx_cell_span_tl }
{ \tl_if_empty_p:N \l__flmx_cell_keys_tl }
{
\tl_put_right:Ne \l__flmx_cells_options_tl
{
, cell { \int_use:N \l__flmx_cell_row_int } { \int_use:N \l__flmx_cell_col_int }
= { \exp_not:V \l__flmx_cell_span_tl } { \exp_not:V \l__flmx_cell_keys_tl }
}
}
\prop_get:NnNT \l__flmx_cell_prop { header } \l__flmx_cell_value_tl
{ \str_if_eq:VnT \l__flmx_cell_value_tl { column } { \__flmx_cell_rule:n { below } } }
\prop_get:NnNT \l__flmx_cell_prop { rules } \l__flmx_cell_value_tl
{
\seq_set_split:NnV \l__flmx_cell_words_seq { ~ } \l__flmx_cell_value_tl
\seq_map_function:NN \l__flmx_cell_words_seq \__flmx_cell_rule:n
}
}
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }
\cs_generate_variant:Nn \str_case:nnT { V }
\cs_new:Npn \__flmx_cell_number:nn #1#2
{ \prop_if_in:NnTF \l__flmx_cell_prop {#1} { \prop_item:Nn \l__flmx_cell_prop {#1} } {#2} }
\cs_new_protected:Npn \__flmx_cell_rule:n #1
{
\str_case:nn {#1}
{
{ above }
{ \__flmx_cell_hline:n { \l__flmx_cell_row_int } }
{ below }
{ \__flmx_cell_hline:n { \l__flmx_cell_row_int + \l__flmx_cell_rows_int } }
{ left }
{ \__flmx_cell_vline:n { \l__flmx_cell_col_int } }
{ right }
{ \__flmx_cell_vline:n { \l__flmx_cell_col_int + \l__flmx_cell_cols_int } }
}
}
\cs_new_protected:Npn \__flmx_cell_hline:n #1
{
\tl_put_right:Ne \l__flmx_cells_options_tl
{
, hline { \int_eval:n {#1} }
= { \__flmx_cell_range:nn { \l__flmx_cell_col_int } { \l__flmx_cell_cols_int } }
{ 0.4pt , solid }
}
}
\cs_new_protected:Npn \__flmx_cell_vline:n #1
{
\tl_put_right:Ne \l__flmx_cells_options_tl
{
, vline { \int_eval:n {#1} }
= { \__flmx_cell_range:nn { \l__flmx_cell_row_int } { \l__flmx_cell_rows_int } }
{ 0.4pt , solid }
}
}
\cs_new:Npn \__flmx_cell_range:nn #1#2
{
\int_use:N #1
\int_compare:nNnT {#2} > { 1 } { - \int_eval:n { #1 + #2 - 1 } }
}
\ExplSyntaxOff