Skip to content

Commit

Permalink
Minor bugfix table alignment with empty column
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Jan 5, 2024
1 parent cc55540 commit 4aa0129
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
27 changes: 27 additions & 0 deletions tests/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,30 @@ def test_tabular_math():

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()


def test_tabular_math_empty():
text = r"""
\begin{tabular}{lll}
\toprule
Foo & Bar & Baz \\
\midrule
$a$ & & $e$ \\
$c'$ & $d = 2$ & $f'$ \\
\bottomrule
\end{tabular}
"""

formatted = r"""
\begin{tabular}{lll}
\toprule
Foo & Bar & Baz \\
\midrule
$a$ & & $e$ \\
$c'$ & $d = 2$ & $f'$ \\
\bottomrule
\end{tabular}
"""

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()
16 changes: 12 additions & 4 deletions texplain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,10 +1160,18 @@ def _align(text: str, placeholders: dict[list[Placeholder]] = {}, maxwidth: int
line = re.split(r"((?<!\\)&)", lines[i])
line = line[:-1] + re.split(r"((?<!\\)\\\\)", line[-1])
line = list(filter(None, [i.strip() for i in line]))
if line[0] == "&":
line = [""] + line
lines[i] = line
cols = max(cols, len(line))
# ensure that there is always a string or empty string before "&""
icol = np.arange(len(line))
for j in np.argwhere(np.array(line) == "&").flatten():
if j == 0:
icol += 1
elif line[j - 1] == "&":
icol[j:] += 1
ret = ["" for _ in range(np.max(icol) + 1)]
for j in range(len(line)):
ret[icol[j]] = line[j]
lines[i] = ret
cols = max(cols, len(ret))

# compute the true with of each column
# (i.e. the width of the content of placeholders, not the width of the placeholder itself)
Expand Down

0 comments on commit 4aa0129

Please sign in to comment.