This script extracts contents of all verbatim environments from the LaTeX file specified on command line. Modified LaTeX code with \verbatiminput commands instead of verbatim is produced on the standard output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import re
import sys
def make_vgetter():
def counter():
i = 0
while True:
i += 1
yield i
cnt = counter()
def get_verbatim(m):
i = cnt.next()
name = 'verbout%03d.txt' % i
f = file(name, 'w')
f.write(m.group(1))
f.close()
return '\\verbatiminput{%s}' % name
return get_verbatim
if __name__ == "__main__":
verbatim = re.compile(r'\\begin\{verbatim\}\n(.+?)\n\\end\{verbatim\}',
re.S)
f = file(sys.argv[1])
s = f.read()
f.close()
print verbatim.sub(make_vgetter(), s)
|
I needed to move all verbatims out of a .tex file and produced this script for the task. A trivial generator, simple function currying, and a deliberate side effect of the function passed to re.sub() are involved.
Tags: text