motchy869
11/7/2018 - 3:44 AM

LaTeX用のマクロをKaTeX用に変換する

LaTeX用のマクロをKaTeX用に変換する

#!/usr/bin/env python3
#coding: utf-8

import re
import sys

def main():
	pat = re.compile(r"\\newcommand\{?\\([1-9A-Za-z]*)\}?(\[([1-9])\])?\{([^\n]*)\}")
	try:
		while True:
			m = pat.search(input())
			if m is not None:
				cmd_name = m.group(1)
				num_args = m.group(3)
				definition = m.group(4)
				if num_args is not None:
					num_args = int(num_args)
					print("\\gdef\\", cmd_name, "".join(["#"+str(i) for i in range(1,num_args+1)]), "{", definition, "}", sep="")
				else:
					print("\\gdef\\", cmd_name, "{", definition, "}", sep="")
	except EOFError:
		pass

if __name__ == "__main__":
	main()