| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #!/usr/bin/env python3
- import re
- import sys
- from pathlib import Path
- if len(sys.argv) != 3:
- print("Usage: preprocess_tex_for_word.py input.tex output.tex", file=sys.stderr)
- sys.exit(1)
- src = Path(sys.argv[1]).read_text(encoding="utf-8")
- text = src
- # Rename \includegraphics{file.ext} -> \includegraphics{file.png}
- text = re.sub(
- r'(\\includegraphics(?:\[[^\]]*\])?\{[^{}]+?)\.[^./{}\\]+(\})',
- r'\1.png\2',
- text
- )
- # Replace siunitx S[...] columns with plain right-aligned columns
- text = re.sub(r'S\[[^\]]*\]', 'r', text)
- # Replace bare S columns with r as well
- text = re.sub(r'(?<![A-Za-z])S(?![A-Za-z])', 'r', text)
- # Remove \resizebox{...}{...}{ ...tabular... }
- # Allow optional % after opening { and before closing }
- resizebox_tabular = re.compile(
- r'''
- \\resizebox\s*
- \{[^{}]*\}\s* # first arg
- \{[^{}]*\}\s* # second arg
- \{\s*%?\s* # opening body, optional %
- (\\begin\{tabular\}.*?\\end\{tabular\}) # inner tabular
- \s*%?\s*\} # optional % before closing }
- ''',
- re.DOTALL | re.VERBOSE
- )
- while True:
- new_text, n = resizebox_tabular.subn(r'\1', text)
- text = new_text
- if n == 0:
- break
- Path(sys.argv[2]).write_text(text, encoding="utf-8")
|