lvjr / tabularray

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

[Feature Request] Python NumPy-like cell selection among some other ideas to improve interface #330

Closed kalekje closed 1 year ago

kalekje commented 1 year ago

I think a very useful and intuitive way to trim the syntax would be to allow for Python's NumPy-like indexing. Except one important difference is that tblr would begin indexing at 1 and include the end-point (Python starts at 0 and excludes the end-point). So let's provide some examples:

Instead of cell{1}{1}=..., why not something like (1,1)=...? No more need for cell and excessive usse of {}

Now, we can try some more complicated NumPy-like slices/selections. For example: ((1,2),(1,2))= would select the 2x2 top-left cells, 4 in total. (1:3, 1:3)= would select the 3x3 top-left cells, 9 in total. ((1:3,5), (1:3,5))= would add the 5th row and col to the mix, so 16 cells total selected.

We can also provide a "step", shown after the second :. Numbers can be left blank between :, and the "defaults" are 1: -1 :1, so if the first int is blank, it becomes one, if the second is blank, it's -1 which means last, and the third, the "step" defaults to 1. (1::2, : )= would select every second row (in NumPy, : means all.

In NumPy, commas delimit dimensions. Since tables are always 2d, I would propose the following (X1,Y1,X2,Y2,...) which would allow for 'chaining' cell selections. Noting that in NumPy, we can also select cells from the end by using negative ints. (1:3,1:3, -3:-1,-3:-1)= would select 18 cells in total. The top-left 3x3 cells, and the bottom-right 3x3. ((1,-1),(1,-1))= would select the four corner cells. (1,2, 3,4)= would pick cells 1,2 and 3,4.

Additionally, I personally believe the syntax is too bound by {} and could be improved. Why not after the = sign, allow for kv input. Example: (1,1)={r=2,c=2,l,t} first cell is a 2x2 multi, top left aligned. Heck, why not take it a step further and allow RxC syntax? This could be equivalent to: (1,1)={2x2,l,t} with RegEx matching, this is an easy feat. I don't even see why one would need commas here. Could also allow negatives in the x syntax. (1,1)={2x2lt} could work for all cases since valign and halign have unique values. Then we could allow other keywords in here for styling, ex: (1,1)={2x2lt,cmd=\bfseries} I would also suggest this x syntax and quick way for cell alignment could also be used for set cell. I do something similar with my lutabulartools package (which I will probably forego in favor of this package)

For more info: https://www.tutorialspoint.com/numpy/numpy_indexing_and_slicing.htm#:~:text=Numpy%20with%20Python&text=Three%20types%20of%20indexing%20methods,the%20built%2Din%20slice%20function.

lvjr commented 1 year ago

Sorry, I am quite satisfied with current interfaces of tabularray package. See also issue #199. You may define a new environment to convert your favorite interfaces into this interfaces.

I think a very useful and intuitive way to trim the syntax would be to allow for Python's NumPy-like indexing. Except one important difference is that tblr would begin indexing at 1 and include the end-point (Python starts at 0 and excludes the end-point). So let's provide some examples:

Instead of cell{1}{1}=..., why not something like (1,1)=...? No more need for cell and excessive usse of {}

Now, we can try some more complicated NumPy-like slices/selections. For example: ((1,2),(1,2))= would select the 2x2 top-left cells, 4 in total. (1:3, 1:3)= would select the 3x3 top-left cells, 9 in total. ((1:3,5), (1:3,5))= would add the 5th row and col to the mix, so 16 cells total selected.

It is cell{1-3,5}{1-3,5} in tabularray.

We can also provide a "step", shown after the second :. Numbers can be left blank between :, and the "defaults" are 1: -1 :1, so if the first int is blank, it becomes one, if the second is blank, it's -1 which means last, and the third, the "step" defaults to 1. (1::2, : )= would select every second row (in NumPy, : means all.

In NumPy, commas delimit dimensions. Since tables are always 2d, I would propose the following (X1,Y1,X2,Y2,...) which would allow for 'chaining' cell selections. Noting that in NumPy, we can also select cells from the end by using negative ints. (1:3,1:3, -3:-1,-3:-1)= would select 18 cells in total. The top-left 3x3 cells, and the bottom-right 3x3. ((1,-1),(1,-1))= would select the four corner cells.

It is cell{1,Z}{1,Z} in tabularray.

(1,2, 3,4)= would pick cells 1,2 and 3,4.

Additionally, I personally believe the syntax is too bound by {} and could be improved. Why not after the = sign, allow for kv input. Example: (1,1)={r=2,c=2,l,t} first cell is a 2x2 multi, top left aligned. Heck, why not take it a step further and allow RxC syntax? This could be equivalent to: (1,1)={2x2,l,t} with RegEx matching, this is an easy feat. I don't even see why one would need commas here. Could also allow negatives in the x syntax. (1,1)={2x2lt} could work for all cases since valign and halign have unique values. Then we could allow other keywords in here for styling, ex: (1,1)={2x2lt,cmd=\bfseries} I would also suggest this x syntax and quick way for cell alignment could also be used for set cell. I do something similar with my lutabulartools package (which I will probably forego in favor of this package)

It is cell{1}{1}={r=2,c=2}{l,r,font=\bfseries} in tabularray. Please see the manual.

For more info: https://www.tutorialspoint.com/numpy/numpy_indexing_and_slicing.htm#:~:text=Numpy%20with%20Python&text=Three%20types%20of%20indexing%20methods,the%20built%2Din%20slice%20function.

kalekje commented 1 year ago

So Z selects the last one, but can it be used to select the second last item? Z2? How about the step? You've shown that cell will select the "outer product", but how about chaining selections? I think this NumPy syntax should certainly be considered... I think what I've shown is much more elegant.

lvjr commented 1 year ago

So Z selects the last one, but can it be used to select the second last item? Z2?

Please do read the manual first.

How about the step? You've shown that cell will select the "outer product", but how about chaining selections? I think this NumPy syntax should certainly be considered... I think what I've shown is much more elegant.

Please write your own environment and do it by yourself.