Skip to content

Commit 679cb47

Browse files
committed
Refactor math_symbol_table.py to dynamically determine column number for symbol tables(Issue #26143)
- Remove hardcoded column numbers; now columns are auto-calculated based on symbol length and page width constraints. Future changes to symbol lists require no manual column adjustment.
1 parent def8fa4 commit 679cb47

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

doc/sphinxext/math_symbol_table.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,42 @@
99

1010
symbols = [
1111
["Lower-case Greek",
12-
4,
1312
(r"\alpha", r"\beta", r"\gamma", r"\chi", r"\delta", r"\epsilon",
1413
r"\eta", r"\iota", r"\kappa", r"\lambda", r"\mu", r"\nu", r"\omega",
1514
r"\phi", r"\pi", r"\psi", r"\rho", r"\sigma", r"\tau", r"\theta",
1615
r"\upsilon", r"\xi", r"\zeta", r"\digamma", r"\varepsilon", r"\varkappa",
1716
r"\varphi", r"\varpi", r"\varrho", r"\varsigma", r"\vartheta")],
1817
["Upper-case Greek",
19-
4,
2018
(r"\Delta", r"\Gamma", r"\Lambda", r"\Omega", r"\Phi", r"\Pi", r"\Psi",
2119
r"\Sigma", r"\Theta", r"\Upsilon", r"\Xi")],
2220
["Hebrew",
23-
6,
2421
(r"\aleph", r"\beth", r"\gimel", r"\daleth")],
2522
["Latin named characters",
26-
6,
2723
r"""\aa \AA \ae \AE \oe \OE \O \o \thorn \Thorn \ss \eth \dh \DH""".split()],
2824
["Delimiters",
29-
5,
3025
_mathtext.Parser._delims],
3126
["Big symbols",
32-
5,
3327
_mathtext.Parser._overunder_symbols | _mathtext.Parser._dropsub_symbols],
3428
["Standard function names",
35-
5,
3629
{fr"\{fn}" for fn in _mathtext.Parser._function_names}],
3730
["Binary operation symbols",
38-
4,
3931
_mathtext.Parser._binary_operators],
4032
["Relation symbols",
41-
4,
4233
_mathtext.Parser._relation_symbols],
4334
["Arrow symbols",
44-
4,
4535
_mathtext.Parser._arrow_symbols],
4636
["Dot symbols",
47-
4,
4837
r"""\cdots \vdots \ldots \ddots \adots \Colon \therefore \because""".split()],
4938
["Black-board characters",
50-
6,
5139
[fr"\{symbol}" for symbol in _mathtext_data.tex2uni
5240
if re.match(bb_pattern, symbol)]],
5341
["Script characters",
54-
6,
5542
[fr"\{symbol}" for symbol in _mathtext_data.tex2uni
5643
if re.match(scr_pattern, symbol)]],
5744
["Fraktur characters",
58-
6,
5945
[fr"\{symbol}" for symbol in _mathtext_data.tex2uni
6046
if re.match(frak_pattern, symbol)]],
6147
["Miscellaneous symbols",
62-
4,
6348
r"""\neg \infty \forall \wp \exists \bigstar \angle \partial
6449
\nexists \measuredangle \emptyset \sphericalangle \clubsuit
6550
\varnothing \complement \diamondsuit \imath \Finv \triangledown
@@ -71,7 +56,34 @@
7156
\lambdabar \L \l \degree \danger \maltese \clubsuitopen
7257
\i \hermitmatrix \sterling \nabla \mho""".split()],
7358
]
74-
59+
def calculate_best_columns(symbols_list, max_line_length, min_columns, max_columns):
60+
"""
61+
Calculate the best number of columns to fit the symbols within the given constraints.
62+
63+
Parameters:
64+
symbols_list (list): A list of symbols to be displayed.
65+
max_line_length (int): The maximum allowed length of a line.
66+
min_columns (int): The minimum number of columns to consider.
67+
max_columns (int): The maximum number of columns to consider.
68+
69+
Returns:
70+
int: The best number of columns that fits within the constraints.
71+
"""
72+
best_columns = min_columns
73+
# Calculate the best number of columns
74+
for columns in range(max_columns, min_columns - 1, -1):
75+
flag = False
76+
for i in range(0, len(symbols_list), columns):
77+
line_length = sum(len(symbol) for symbol in symbols_list[i:i + columns])+len(symbols_list[i:i + columns]) - 1
78+
# Check if the line length is less than or equal to max_line_length
79+
if line_length > max_line_length:
80+
flag = True
81+
break
82+
if not flag:
83+
best_columns = columns
84+
break
85+
max_cell_width = max(len(sym) for sym in symbols_list)
86+
return min(best_columns, len(symbols_list),max_line_length // max_cell_width)
7587

7688
def run(state_machine):
7789

@@ -86,15 +98,16 @@ def render_symbol(sym, ignore_variant=False):
8698
return f'\\{sym}' if sym in ('\\', '|', '+', '-', '*') else sym
8799

88100
lines = []
89-
for category, columns, syms in symbols:
101+
for category, syms in symbols:
90102
syms = sorted(syms,
91103
# Sort by Unicode and place variants immediately
92104
# after standard versions.
93105
key=lambda sym: (render_symbol(sym, ignore_variant=True),
94106
sym.startswith(r"\var")),
95107
reverse=(category == "Hebrew")) # Hebrew is rtl
96108
rendered_syms = [f"{render_symbol(sym)} ``{sym}``" for sym in syms]
97-
columns = min(columns, len(syms))
109+
max_line_length = 95
110+
columns = calculate_best_columns(rendered_syms, max_line_length=max_line_length, min_columns=4, max_columns=6)
98111
lines.append("**%s**" % category)
99112
lines.append('')
100113
max_width = max(map(len, rendered_syms))
@@ -128,13 +141,12 @@ def setup(app):
128141
metadata = {'parallel_read_safe': True, 'parallel_write_safe': True}
129142
return metadata
130143

131-
132144
if __name__ == "__main__":
133145
# Do some verification of the tables
134146

135147
print("SYMBOLS NOT IN STIX:")
136148
all_symbols = {}
137-
for category, columns, syms in symbols:
149+
for category, syms in symbols:
138150
if category == "Standard Function Names":
139151
continue
140152
for sym in syms:

0 commit comments

Comments
 (0)