Skip to content

Instantly share code, notes, and snippets.

@alvinwan
Created September 1, 2016 06:07
Show Gist options
  • Save alvinwan/d4946e5117afef5156f06038d834cd47 to your computer and use it in GitHub Desktop.
Save alvinwan/d4946e5117afef5156f06038d834cd47 to your computer and use it in GitHub Desktop.
resolve imports and remove solutions to generate questions .tex
"""Generate LaTeX that only includes solutions for a particular TeX document.
Usage:
questions.py [--source=<src>] [--dest=<dest>]
"""
from TexSoup import TexSoup
from docopt import docopt
SOlUTION_COMMAND = 'sol'
IMPORT_COMMAND = 'subimport'
def main(tex):
"""Returns new TexSoup, resolving includes and removing solutions."""
soup = TexSoup(tex)
for subimport in soup.find_all(IMPORT_COMMAND):
path = ''.join(arg.value for arg in subimport.args)
subimport.replace(*main(open(path)).contents)
# check resolver works
assert len(list(soup.find_all(IMPORT_COMMAND))) == 0
for sol in soup.find_all(SOLUTION_COMMAND):
sol.delete()
return soup
if __name__ == '__main__':
arguments = docopt(__doc__, version='LaTeX Questions 1.0')
soup = main(open(arguments['--source'] or input('File:').strip()))
with open(arguments['--dest'] or input('Dest:').strip(), 'w') as f:
f.write(repr(soup))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment