Fix Table column widths being truncated for the first and last columns - #4900
Fix Table column widths being truncated for the first and last columns#4900yyccPhil wants to merge 6 commits into
Conversation
for more information, see https://pre-commit.ci
behackl
left a comment
There was a problem hiding this comment.
Thanks for taking a look at this, much appreciated!
Please find two particular examples of Table usage below where the expected behaviour does not match the actual behaviour yet:
from manim import Table, Text
table = Table(
[["a", "b"], ["c", "d"]],
row_labels=[Text("r1"), Text("r2")],
col_labels=[Text("c1"), Text("c2")],
top_left_entry=Text(""),
h_buff=0.1,
include_outer_lines=True,
arrange_in_grid_config={
"col_widths": [2, 3, 3], # row-label column, then two data columns
"col_alignments": "ccc",
},
)
xs = sorted(line.get_center()[0] for line in table.get_vertical_lines())
widths = [xs[i + 1] - xs[i] for i in range(len(xs) - 1)]
print(widths)
This should produce widths [2.1, 3.1, 3.1], but doesn't because labels are not handled correctly yet.
Second example:
from manim import LEFT, Table
table = Table(
[["a"]],
h_buff=0.1,
include_outer_lines=True,
arrange_in_grid_config={
"col_widths": [3],
"cell_alignment": LEFT,
},
)
column = table.get_columns()[0]
actual_left = min(line.get_center()[0] for line in table.get_vertical_lines())
expected_left = column.get_left()[0] - table.h_buff / 2
print(actual_left, expected_left)
Text is not always centered in the cells, so it should not be treated as such; in particular the left grid line is shifted too much.
Could you please take a look, let me know whether or not you agree with the (in)correct behaviour for these two examples and see whether there is a nice and straightforward way to get this fixed?
Thanks again!
|
Hi @behackl ! Thanks for the detailed examples, I've pushed a fix. For the first example, I'd special-cased labelled tables to fall back to the old content-based behaviour, which was too conservative — For the second one, the helper I created only looked at I also added regression tests for both cases. |
Overview: What does this pull request change?
This fixes the first and last columns of a
Table(and its subclassesMathTable,IntegerTable, andDecimalTable) being truncated when explicitcol_widthsare passed througharrange_in_grid_config. Grid lines and cell borders now follow each column's arranged slot instead of the bounding box of its contents, so the requested column widths are honored for every column.Closes #3446
Motivation and Explanation: Why and how do your changes improve the library?
When
col_widthsmakes a column's slot wider than its contents,arrange_in_gridcenters the (narrower) content inside the slot. However,Table._add_vertical_lines()andTable.get_cell()anchored the outer grid lines and cell borders to the content extent (get_columns()[i].get_left()/get_right()± halfh_buff) rather than to the slot. The outer columns were therefore drawn narrower than requested, and because the anchor depended on the content width, the truncation was asymmetric (a column with narrow contents was clipped more).Reproduction from the issue, rendered at width 10 with
col_widths=[3] * 6:[1.00, 1.84, 1.85, 1.85, 1.85, 1.60]— first and last columns visibly clipped.[1.667] * 6— all columns equal,col_widthshonored.Before / after
Before:

After:

The change adds a small helper,
Table._get_column_x_edges(col_index), which returns the left/right x-coordinates of a column's arranged slot:get_cell()corners are unchanged to within 1e-9 for a default table.)col_alignments("l","c","r")._add_vertical_lines()(both outer and inner lines) andget_cell()now use this helper.Links to added or changed documentation pages
No documentation pages are changed by this PR.
Further Information and Comments
test_table_col_widths_are_honored, intests/module/mobject/test_table.py, modeled on the existing regression tests there. It fails onmainand passes with this change.col_widths, so the helper intentionally keeps the previous content-based behavior for labelled tables to avoid changing their output. I'm happy to extend the fix to labelled tables as well if you'd prefer — just let me know which you'd like.This is my first contribution to Manim, so any feedback is very welcome!
Acceptance criteria
test_table_col_widths_are_honored)tests/module/mobject/test_table.pysuite + the new test)ruff checkandruff format --checkpass)col_widthspath is byte-for-byte unchanged)Reviewer Checklist