32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
"""Make the math of article/sim_numerics.md valid LaTeX as well as MathJax: a sub- or
|
|
superscript that starts with a bare control word (n_\\max) needs braces in LaTeX (n_{\\max}).
|
|
The rendered formula is identical. Reports what it finds; --apply writes.
|
|
|
|
python -u fix_md_for_latex.py [--apply]
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sys
|
|
|
|
PATH = "article/sim_numerics.md"
|
|
# only argument-less control words (n_\max); a macro with an argument (x_\mathrm{far},
|
|
# r_\text{new}) is valid LaTeX as written, and bracing it would detach its argument
|
|
PAT = re.compile(r"([_^])(\\[A-Za-z]+)(?![A-Za-z{])")
|
|
|
|
|
|
def main():
|
|
with open(PATH, encoding="utf8") as fh:
|
|
text = fh.read()
|
|
hits = PAT.findall(text)
|
|
print(f"{len(hits)} bare control words in sub/superscripts:", sorted(set(h[1] for h in hits)))
|
|
if "--apply" in sys.argv:
|
|
text = PAT.sub(lambda m: f"{m.group(1)}{{{m.group(2)}}}", text)
|
|
with open(PATH, "w", encoding="utf8") as fh:
|
|
fh.write(text)
|
|
print("braced")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|