lvjr / tabularray

Typeset tabulars and arrays with LaTeX3
https://ctan.org/pkg/tabularray
258 stars 22 forks source link

Reimplement the stretch option #265

Closed qinglee closed 2 years ago

qinglee commented 2 years ago

Instead of adding struts, we can directly set the height and depth of the cell box.

There is a slight difference from the original behavior, which is to be expected, and I have updated the relevant test files.

lvjr commented 2 years ago
-  \__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 }
+  \vbox_set_top:Nn \l_tmpa_box { \vbox_unpack:N \l_tmpb_box }

Thank you! it is a great idea to use \vbox_unpack:N. I didn't know I can use it before. And the change will make the table building process run faster.

lvjr commented 2 years ago

I am wondering maybe we can step further by directly setting the heights and depths of rows instead of cells. That is to say, we may change the initial values of several internal data for rows:

In fact we need to use the larger value between \baselinskip and the row height (which is actually minimal total height) set by an user.

I think this would make tabularray run even faster since we don't need to adjust dimensions of all cells several times.

qinglee commented 2 years ago
-  \__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 }
+  \vbox_set_top:Nn \l_tmpa_box { \vbox_unpack:N \l_tmpb_box }

Thank you! it is a great idea to use \vbox_unpack:N. I didn't know I can use it before. And the change will make the table building process run faster.

I took a quick look at the code and it would improve some performance if we could use \hbox_unpack:N and \vbox_unpack:N flexibly where appropriate. The ultimate goal is to actually typeset each cell only once. This will completely fix issues as #231. Obviously, it is a big challenge.

qinglee commented 2 years ago

I am wondering maybe we can step further by directly setting the heights and depths of rows instead of cells. That is to say, we may change the initial values of several internal data for rows:

  • change @row-height from 0pt to stretch * \baselinskip
  • change @row-upper from 0pt to stretch * 0.7\baselinskip
  • change @row-lower from 0pt to stretch * 0.3\baselinskip

In fact we need to use the larger value between \baselinskip and the row height (which is actually minimal total height) set by an user.

I think this would make tabularray run even faster since we don't need to adjust dimensions of all cells several times.

Indeed, your solution is more efficient and natural. I do not fully grasp the internal mechanism of tabularray, so I can only provide a preliminary reference.

lvjr commented 2 years ago

I took a quick look at the code and it would improve some performance if we could use \hbox_unpack:N and \vbox_unpack:N flexibly where appropriate. The ultimate goal is to actually typeset each cell only once. This will completely fix issues as #231. Obviously, it is a big challenge.

For multicolumn cells and X-column cells, we need to change their widths. So I assume it would be quite difficult to typeset them only once.

lvjr commented 2 years ago

That is to say, we may change the initial values of several internal data for rows:

  • change @row-height from 0pt to stretch * \baselinskip
  • change @row-upper from 0pt to stretch * 0.7\baselinskip
  • change @row-lower from 0pt to stretch * 0.3\baselinskip

In fact we need to use the larger value between \baselinskip and the row height (which is actually minimal total height) set by an user.

Oh sorry, if user-set-total-height is greater than stretch*\baselineskip, only @row-height need to be enlarged, @row-upper and @row-lower remain the same as the above values.

lvjr commented 2 years ago

I tried to implement my proposal but found it is not enough to set row dimensions only:

image

\documentclass{article}
\usepackage{tabularray}
\SetTblrInner{rowsep=0pt}
\usepackage{array}
\begin{document}

\begin{tabular}{p{4cm}}
\hline
The quick brown fox jumps over the lazy dog abcd.\\
\hline
\end{tabular} 
\begin{tblr}{p{4cm}}
\hline
The quick brown fox jumps over the lazy dog abcd.\\
\hline
\end{tblr}

\bigskip

\begin{tabular}{b{4cm}}
\hline
pgrs pgrs pgrs pgrs pgrs pgrs pgrs The quick brown fox jumps over the lazy dog abcd.\\
\hline
\end{tabular} 
\begin{tblr}{b{4cm}}
\hline
pgrs pgrs pgrs pgrs pgrs pgrs pgrs The quick brown fox jumps over the lazy dog abcd.\\
\hline
\end{tblr} 

\end{document}

And your patch generated the expected result with the same code:

image