diff --git a/tabularray.sty b/tabularray.sty index 38e70a4..134f96c 100644 --- a/tabularray.sty +++ b/tabularray.sty @@ -3676,6 +3676,7 @@ \tl_const:Nn \c__tblr_cell_math_style_tl { \relax } \tl_const:Nn \c__tblr_cell_imath_style_tl { \textstyle } \tl_const:Nn \c__tblr_cell_dmath_style_tl { \displaystyle } +\box_new:N \l__tblr_measured_cell_box %% 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 @@ -3728,19 +3729,28 @@ { \__tblr_data_item:nen { column } {#2} { width } } } } + \box_clear:N \l__tblr_measured_cell_box \dim_compare:nNnT { \l__tblr_w_tl } < { 0pt } % column width unset { - \__tblr_save_counters:n { cell } - \bool_if:NTF \l__tblr_cell_math_mode_bool + %% To keep \lTblrMeasuringBool correct we need measure=vbox from varwidth. + %% As an experiment, we implement it only when hook library is loaded. + \__tblr_lib_if_used:nTF { hook } { - %% Note that font = \boldmath will increase cell width (issue #137) - \hbox_set:Nn \l_tmpa_box { \l__tblr_f_tl \l__tblr_c_tl } - \tl_set:Nx \l__tblr_w_tl { \dim_eval:n { \box_wd:N \l_tmpa_box } } + \__tblr_build_vcell_with_vbox: } { - \__tblr_get_cell_size_with_box: + \__tblr_save_counters:n { cell } + \bool_if:NTF \l__tblr_cell_math_mode_bool + { + %% Note that font = \boldmath will increase cell width (issue #137) + \hbox_set:Nn \l_tmpa_box { \l__tblr_f_tl \l__tblr_c_tl } + \tl_set:Nx \l__tblr_w_tl { \dim_eval:n { \box_wd:N \l_tmpa_box } } + } + { + \__tblr_get_cell_size_with_box: + } + \__tblr_restore_counters:n { cell } } - \__tblr_restore_counters:n { cell } } \tl_put_left:NV \l__tblr_c_tl \l__tblr_f_tl \tl_set:Nx \l__tblr_cell_fg_tl @@ -3751,6 +3761,23 @@ \group_end: } +%% Build cell vbox with varwidth and use it later when hook library is loaded +\cs_new_protected:Npn \__tblr_build_vcell_with_vbox: + { + \hbox_set:Nn \l__tblr_measured_cell_box + { + \begin{varwidth}{\paperwidth} + \TblrParboxRestore + \cs:w __tblr_halign_command_ \g__tblr_cell_halign_tl : \cs_end: + \__tblr_leave_vmode: + \l__tblr_f_tl + \l__tblr_c_tl + \end{varwidth} + } + \tl_set:Nx \l__tblr_w_tl { \dim_eval:n { \box_wd:N \l__tblr_measured_cell_box } } + } + +%% Measure cell width only and build it later when hook library is not loaded \cs_new_protected:Npn \__tblr_get_cell_size_with_box: { \tl_if_eq:NnTF \l__tblr_inner_spec_measure_tl { vbox } @@ -3808,7 +3835,16 @@ \cs_new_protected:Npn \__tblr_get_vcell_and_sizes:NN #1 #2 { \group_begin: - \vbox_set:Nn \l_tmpb_box { \__tblr_make_vcell_text:NN #1 #2 } + \box_if_empty:NTF \l__tblr_measured_cell_box + { \vbox_set:Nn \l_tmpb_box { \__tblr_make_vcell_text:NN #1 #2 } } + { + \vbox_set:Nn \l_tmpb_box + { + \dim_set:Nn \tex_hsize:D { \l__tblr_w_tl } + \noindent + \hbox_unpack_drop:N \l__tblr_measured_cell_box + } + } \vbox_set_top:Nn \l_tmpa_box { \vbox_unpack:N \l_tmpb_box } \__tblr_process_stretch: \dim_gset:Nn \g__tblr_cell_wd_dim { \box_wd:N \l_tmpb_box }