tabularray icon indicating copy to clipboard operation
tabularray copied to clipboard

Adjusting `rightsep` from within `cmd`

Open mikkelee opened this issue 1 year ago • 5 comments

I'm working on some code that ends up putting a zero-width box at the end of the cell text (to align values, similar to SI units aligning at the decimal-point). I can use \SetCell directly in the table, but it appears to be a no-op if I use it inside a column-cmd. Is this correctly understood & expected behavior?

\documentclass{article}

\usepackage{tabularray}

\ExplSyntaxOn

\NewColumnType {Z} { Q[r, cmd=\test_tblr_cell:n] }

\cs_new_protected:Nn \test_tblr_cell:n
  {
    \SetColumn{ rightsep+ = 1em } % appears to do nothing
    #1
  }

\ExplSyntaxOff

\begin{document}

\begin{tblr}{|Z|}
	\SetColumn{ rightsep+ = 2em } a \\ % appears to work
	b \\
	c \\
	d \\
	e \\
\end{tblr}

\end{document}

Output only has 2em added, I would expect at least 7 (2 from within cell a + 1 per cmd-invocation): Screenshot 2024-06-13 at 19 03 04

Thanks! :)

mikkelee avatar Jun 13 '24 17:06 mikkelee

This is expected behavior.

\SetColumn is one of table commands which must be directly "seen" (not hidden in other non-table commands nor in cmd code) at the beginning of cells. When cmd code is executed, all table commands are already disabled.

Using

\cs_new_protected:Nn \test_tblr_cell:n
  {
    \ShowCommand{\SetColumn}
    #1
  }

shows

> \SetColumn=document command:
  #1:o
  #2:m
->.
<recently read> }

I wonder why you need to set rightsep on a per-cell basis. Won't it enlarge rightsep n times (at least, because tabularray may process each cell multiple times even in the measuring phrase) for a n-row table?

The process option and (specially defined) \columnSetStyle command provided by functional library may help. It's documented in package manual (2024A, 2024-02-16), sec. 5.5.2 Inner key process in action.

muzimuzhi avatar Jun 13 '24 19:06 muzimuzhi

I don't need to increase it as in the example, that was just testing to see if I could increase it at all from within cmd, which you explain well that I cannot.

Basically, my command does a bunch of stuff, including taking a measurement and saving it if it is higher than the previous measurement — ie. the widest — and I would like to use that measurement to increase the rightsep before final typesetting. A bit hard to explain without code at hand, I can try to put up something more tangible over the weekend.

mikkelee avatar Jun 13 '24 20:06 mikkelee

It seems to me that in your case the table column won't automatically widen but one has to cheat it by enlarging rightsep of that column?

I'm working on some code that ends up putting a zero-width box at the end of the cell text (to align values, similar to SI units aligning at the decimal-point).

Seems so, from the "zero-width box at the end of the cell text". As the amount of extra rightsep is computable, maybe you can assign some widths to such zero-width boxes.

muzimuzhi avatar Jun 13 '24 21:06 muzimuzhi

It seems to me that in your case the table column won't automatically widen but one has to cheat it by enlarging rightsep of that column?

Yes, exactly what I meant :)

Seems so, from the "zero-width box at the end of the cell text". As the amount of extra rightsep is computable, maybe you can assign some widths to such zero-width boxes.

I suppose I could always use a box of a certain width, but it didn't look very "nice" in my experiments (everything was "pushed over").

My current code looks like this, lines 291f is where the width is calculated & line 389 is where I intended to use it: https://github.com/mikkelee/gen-fracmath/commit/5357f95636a8a6ade060982afa29f92cac540d10#diff-e753ad74d446f25b2e10096e7dea78c2d046e9b88defee3f4d0064fe37a0a1eeR291

mikkelee avatar Jun 14 '24 04:06 mikkelee

Alright, I got it to work by adding this at the end of my cmd:

\int_compare:nNnT { \c@rownum } = { \arabic{rowcount} } {
	\columnSetStyle { \c@colnum } { rightsep = \l__fracmath_current_overhang_dim }
}

Thanks for the pointer to the functional library and \columnSetStyle :)

mikkelee avatar Jun 25 '24 13:06 mikkelee