Last active
April 24, 2021 16:02
-
-
Save mtesseracted/9107768aa5d8018ec4b63c59ce7ca41a to your computer and use it in GitHub Desktop.
Find chemical symbols and surround them with \ce{ Symbol }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Find chemical symbols and surround them with \ce{ Symbol } | |
# Problem words: I, HOW, In, degrees K. Add words to exlist to ignore them. | |
# Also find lines of the type: '(a) .*' and surround them with '\wrongchoice{ (a) .*}' | |
import re, sys | |
if len(sys.argv) < 2 : | |
print('Usage:> {} <filename>'.format(sys.argv[0])) | |
sys.exit(1) | |
ptable =" H He " | |
ptable+=" Li Be B C N O F Ne " | |
ptable+=" Na Mg Al Si P S Cl Ar " | |
ptable+=" K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr " | |
ptable+=" Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe " | |
ptable+=" Cs Ba La Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn " | |
ptable+=" Fr Ra Ac Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og " | |
ptable+=" Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu " | |
ptable+=" Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr " | |
exlist = ['C','I','In','K','HOW'] # exclude these words from being replaced | |
orsyms = '|'.join(ptable.split()) | |
resyms = re.compile(r'\b'+'((?:(?:{})\d*)+)'.format(orsyms)+r'\b') | |
rewrng = re.compile(r'^\s*\([a-d]\).*') | |
latexfile=sys.argv[1] | |
with open(latexfile,'r') as fd: | |
for line in fd: | |
for m in list(set(resyms.findall(line))): | |
if m not in exlist : | |
line = re.sub(r'\b'+m+r'\b', r'\ce{'+m+r'}', line) | |
if rewrng.match(line) : | |
line = r'\wrongchoice{ '+line[:-1:]+r'}'+'\n' | |
print(line,end='') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment