Skip to content

Measure column widths after character escaping (fixes #244) - #445

Open
IMGillusion wants to merge 1 commit into
astanin:masterfrom
IMGillusion:fix/244-escape-width
Open

Measure column widths after character escaping (fixes #244)#445
IMGillusion wants to merge 1 commit into
astanin:masterfrom
IMGillusion:fix/244-escape-width

Conversation

@IMGillusion

Copy link
Copy Markdown

Fixes #244.

tabulate(..., tablefmt="latex") escaped special characters (e.g. _ -> \_)
only at render time, after padding. Column widths were measured on the
unescaped text, so any escaped cell rendered wider than its measured
width and the & separators (and | in github tables) stopped lining
up across rows:

 col1              &   col2 \\
 first             &      1 \\
 a\_long\_second\_row &      2 \\

This applies the same per-character escaping before widths are measured
(after cell formatting, before alignment), then replaces the affected
row formatters with copies that carry no escape map so nothing is
escaped twice. The format objects are copied per call, so shared
module-level TableFormats are never mutated. latex_raw (no escape
rules) and all formats without an escape map are untouched.

  • new regression test test_latex_escaped_cell_width (issue example +
    asserts every & sits in the same column)
  • updated 3 existing expected outputs whose old values locked in the
    misaligned rendering (test_latex, test_latex_escape_special_chars,
    test_github_escape_pipe_character)

Full suite: 383 passed, 1 skipped (pytest with --doctest-modules,
including README.md doctests).

Formats with per-character escape rules (latex, github) escaped special
characters at render time, i.e. after padding, so escaped cells were
wider than the measured column widths and rows misaligned.

Apply the escaping before widths are measured, then drop the escape
rules from the row formatters to avoid escaping twice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Additional width for escaping characters is not considered in "latex" tablefmt

1 participant