From dee5ba4812741dd01e4c29b29395e85359ffd1eb Mon Sep 17 00:00:00 2001 From: Lafrite Date: Tue, 2 Sep 2014 11:20:09 +0200 Subject: [PATCH] split DS_gene from jinja2 config and start working on filters --- DS_gene.py | 26 ++++++++++------------- example/tpl_example.tex | 8 +++++-- texenv.py | 46 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 texenv.py diff --git a/DS_gene.py b/DS_gene.py index 836bc28..a9f549c 100755 --- a/DS_gene.py +++ b/DS_gene.py @@ -1,20 +1,13 @@ #!/usr/bin/env python # encoding: utf-8 -import jinja2, random, os +import os import sys import optparse from path import path -from pymath.random_expression import RdExpression -texenv = jinja2.Environment( - block_start_string = '\Block{', - # Gros WTF!! Si on le met en maj ça ne marche pas alors que c'est en maj dans le template... - block_end_string = '}', - variable_start_string = '\Var{', - variable_end_string = '}', - loader = jinja2.FileSystemLoader(os.path.abspath('.')) -) +from texenv import texenv +from pymath.random_expression import RdExpression def main(options): #template = report_renderer.get_template(options.template) @@ -42,10 +35,12 @@ def main(options): dest = path(str(subj) + output) with open( dest, 'w') as f: f.write(template.render( RdExpression = RdExpression , infos = {"subj" : subj})) - #os.system("pdflatex " + dest) - #if not options.dirty: - # os.system("rm *.aux *.log") + if not options.no_compil: + os.system("pdflatex " + dest) + + if not options.dirty: + os.system("rm *.aux *.log") cwd.cd() @@ -53,10 +48,11 @@ if __name__ == '__main__': parser = optparse.OptionParser() - parser.add_option("-t","--tempalte",action="store",type="string",dest="template", help="File with the template. The name should have the following form tpl_... .") + parser.add_option("-t","--template",action="store",type="string",dest="template", help="File with the template. The name should have the following form tpl_... .") parser.add_option("-o","--output",action="store",type="string",dest="output",help="Base name for output )") - parser.add_option("-n","--number_subjects", action="store",type="int", dest="num_subj", default = 1, help="The number of subjects to make") + parser.add_option("-N","--number_subjects", action="store",type="int", dest="num_subj", default = 1, help="The number of subjects to make") parser.add_option("-d","--dirty", action="store_true", dest="dirty", help="Do not clean after compilation") + parser.add_option("-n","--no-compile", action="store_true", dest="no_compil", help="Do not compile source code") (options, args) = parser.parse_args() diff --git a/example/tpl_example.tex b/example/tpl_example.tex index dbe7384..3ed6c88 100644 --- a/example/tpl_example.tex +++ b/example/tpl_example.tex @@ -1,5 +1,5 @@ -\documentclass[a4paper,10pt]{/media/documents/Cours/Prof/Enseignements/tools/style/classDS} -\usepackage{/media/documents/Cours/Prof/Enseignements/2013_2014} +\documentclass[a4paper,10pt]{/media/documents/Cours/Prof/Enseignements/Archive/2013-2014/tools/style/classDS} +\usepackage{/media/documents/Cours/Prof/Enseignements/Archive/2013-2014/2013_2014} % Title Page \titre{Calcul littéral et statistiques} @@ -26,6 +26,10 @@ Le barème est donné à titre indicatif, il pourra être modifié. A = \Var{ A } \\ B = \Var{ B } \end{equation*} + + Solutions: + \Var{A.simplify() | calculus} + \Var{B.simplify() | calculus(name = "B")} \end{Exo} diff --git a/texenv.py b/texenv.py new file mode 100644 index 0000000..5790502 --- /dev/null +++ b/texenv.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# encoding: utf-8 + +import jinja2, os + +# Definition of jinja syntax for latex +texenv = jinja2.Environment( + block_start_string = '\Block{', + # Gros WTF!! Si on le met en maj ça ne marche pas alors que c'est en maj dans le template... + block_end_string = '}', + variable_start_string = '\Var{', + variable_end_string = '}', + loader = jinja2.FileSystemLoader(os.path.abspath('.')) +) + +# Filters + +def do_calculus(steps, name = "A"): + """Display properly the calculus + + :param steps: list of steps + :returns: latex string ready to be endbeded + + """ + ans = "\\begin{eqnarray*}\n" + + ans += " \\\\ \n".join([name + " & = & " + s for s in steps]) + ans += "\n\\end{eqnarray*}\n" + return ans + + + +texenv.filters['calculus'] = do_calculus + + +if __name__ == '__main__': + from pymath.expression import Expression + exp = Expression("2/4 + 18") + print(do_calculus(exp.simplify())) + + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del