Skip to content

Instantly share code, notes, and snippets.

@martin-ueding
Created May 3, 2020 15:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save martin-ueding/67a8b1ab5ab8c66f688f71f598c8b688 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright © 2015-2016 Martin Ueding <mu@martin-ueding.de>
# Licensed under The MIT License
import argparse
import jinja2
template_text = r'''
\documentclass[11pt, {{ lang }}]{scrartcl}
\usepackage{../../../header}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
{% for package in packages %}
\usepackage{ {{ package }} }
{% endfor %}
\pagestyle{empty}
\begin{document}
{{ snippet }}
\end{document}
'''
def main():
options = _parse_args()
template = jinja2.Template(template_text)
with open(options.infile) as f:
snippet = f.read()
if options.packages is None:
options.packages = []
rendered = template.render(lang=options.lang, snippet=snippet, packages=options.packages)
with open(options.outfile, 'w') as f:
f.write(rendered)
def _parse_args():
'''
Parses the command line arguments.
:return: Namespace with arguments.
:rtype: Namespace
'''
parser = argparse.ArgumentParser(description='Wraps a single environment into a standalone document.')
parser.add_argument('infile', help='Snippet LaTeX file with tikzpicture')
parser.add_argument('outfile', help='Standalone LaTeX file')
parser.add_argument('--lang', default='english', help='Language for babel, default is %(default)s')
parser.add_argument('--packages', nargs='*', help='Additional packages to load')
options = parser.parse_args()
return options
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment