def test_document(): geometry_options = { "includeheadfoot": True, "headheight": "12pt", "headsep": "10pt", "landscape": True } doc = Document( default_filepath='default_filepath', documentclass='article', fontenc='T1', inputenc='utf8', lmodern=True, data=None, page_numbers=True, indent=False, document_options=["a4paper", "12pt"], geometry_options=geometry_options ) repr(doc) doc.append('Some text.') doc.change_page_style(style="empty") doc.change_document_style(style="plain") doc.add_color(name="lightgray", model="gray", description="0.6") doc.add_color(name="abitless", model="gray", description="0.8") doc.set_variable(name="myVar", value="1234") doc.set_variable(name="myVar", value="1234") doc.change_length(parameter=r"\headheight", value="0.5in") doc.generate_tex(filepath='') doc.generate_pdf(filepath='', clean=True)
def test_document(): geometry_options = { "includeheadfoot": True, "headheight": "12pt", "headsep": "10pt", "landscape": True } doc = Document(default_filepath='default_filepath', documentclass='article', fontenc='T1', inputenc='utf8', lmodern=True, data=None, page_numbers=True, indent=False, document_options=["a4paper", "12pt"], geometry_options=geometry_options) repr(doc) doc.append('Some text.') doc.change_page_style(style="empty") doc.change_document_style(style="plain") doc.add_color(name="lightgray", model="gray", description="0.6") doc.add_color(name="abitless", model="gray", description="0.8") doc.set_variable(name="myVar", value="1234") doc.set_variable(name="myVar", value="1234") doc.change_length(parameter=r"\headheight", value="0.5in") doc.generate_tex(filepath='') doc.generate_pdf(filepath='', clean=True)
def _build_drawing(self): doc = Document() with doc.create(TikZ()) as tikz_pic: # create a node node_options = { 'align': 'center', 'inner sep': '20pt', 'label': '{{270:{}}}'.format(self.module.name) } box = TikZNode(handle='module', options=TikZOptions('draw', 'rounded corners', **node_options)) tikz_pic.append(box) for port in self.module.ports: if port.direction == 'in': tikz_pic.append( TikZNode(text=port.name, options=TikZOptions(anchor='west'), at=TikZCoordinate(-1, 1))) break doc.generate_tex('test') return doc
def start(): info = get_json(f"../{defaults.get('jsonOutput')}") doc = Document(default_filepath=defaults.get('pdfPath')) for artist, artistListStuff in info.items(): with doc.create(Section(f'Artist: {artist}')): importLinedFigures(doc, artistListStuff['base_objects']) for subsection, subsectionList in artistListStuff['sub_objects'].items( ): with doc.create(Subsection(f'Folder: {subsection}')): importLinedFigures(doc, subsectionList) doc.append(NoEscape(r'\clearpage')) for subsubsection, subsubsectionList in artistListStuff[ 'sub_sub_objects'].items(): with doc.create(Subsubsection(f'Sub Folder: {subsubsection}')): importLinedFigures(doc, subsubsectionList) doc.append(NoEscape(r'\clearpage')) try: doc.generate_tex() doc.generate_pdf(clean_tex=False) print('done?') except Exception as e: print(f"OH NO") print(e)
def test_three(self): doc = Document() with doc.create(Figure()) as fig: with fig.create( SubFigure(width=NoEscape(r"0.48\textwidth"))) as left: name = "left" plt.figure() plt.plot(x, y, label="l") plt.legend() left.add_plot(name, caption=name) plt.close() with fig.create( SubFigure(width=NoEscape(r"0.48\textwidth"))) as right: name = "right" plt.figure() plt.plot(x, y, label="r") plt.legend() right.add_plot(name, caption=name) plt.close() fig.append("\n") with fig.create( SubFigure(width=NoEscape(r"0.48\textwidth"))) as left2: name = "left2" plt.figure() plt.plot(x, y, label="l2") plt.legend() left2.add_plot(name, caption=name) plt.close() fig.add_caption("full") doc.generate_tex("Latex/threesubfigures")
def write(self): opening = self.config['opening'].replace( '$organization', self.posting.get('organization')) closing = self.config['closing'].replace( '$organization', self.posting.get('organization')) output = Document(documentclass='letter') output.preamble.append(Command('signature', self.config['sender'])) output.preamble.append( Command('address', NoEscape(self.return_address()))) output.preamble.append(Command('date', NoEscape(r'\today'))) output.append(Command('begin', ['letter', self.employer_address()])) output.append(Command('opening', 'Dear Hiring Manager,')) output.append(opening + self.write_body()) output.append(NewLine()) output.append(NewLine()) output.append(closing) output.append(Command('closing', 'Sincerely,')) output.append(Command('end', 'letter')) filename = '{0}_{1}'.format( self.posting['organization'], ' '.join(process_input(self.posting['job_title']))).replace( ' ', '_').lower() output.generate_pdf(filename) output.generate_tex(filename)
def create_cs170_hw(num="0", author="", questions=[]): # Create document doc = Document() #Inclue packages doc.packages.append(Package('amsmath')) doc.packages.append(Package('amssymb')) doc.packages.append(Package('enumitem')) # Make title title = 'CS 170 - HW %d' % (num) doc.preamble.append(Command('title', bold(title))) doc.preamble.append(Command('author', author)) doc.preamble.append(Command('date', '')) doc.append(NoEscape(r'\maketitle')) # Instructions #with doc.create(Section("Instructions")) as section: # section.append("N/A") # Create questions for question in questions: name = question[0] parts = question[1] with doc.create(Section(name)): if (parts > 0): with doc.create( Enumerate(enumeration_symbol=r"(\alph*)")) as enum: for _ in range(0, parts): enum.add_item("") # Generate Latex file file_name = "cs170_hw" + str(num) doc.generate_tex(file_name) print("%s.tex generated!" % (file_name))
def create_adventure(filename: str, title_model: markovify.Text, chapter_model: markovify.Text, heading_model: markovify.Text, spoken_model: markovify.Text, paragraph_model: markovify.Text): doc = Document() doc.documentclass = Command( 'documentclass', options="letterpaper,twocolumn,openany,nodeprecatedcode", arguments=["dndbook"]) doc.preamble.append(Command('title', title_model.make_sentence())) doc.append(NoEscape(r'\maketitle')) doc.append(NoEscape(r'\tableofcontents')) for _ in range(random.randint(5, 12)): with doc.create(chapter(chapter_model.make_sentence())): for _ in range(random.randint(5, 15)): heading = heading_model.make_sentence() while heading is None: heading = heading_model.make_sentence() with doc.create(Section(heading)): with doc.create(DndReadAloud()): doc.append(spoken_model.make_sentence()) for _ in range(3, 9): doc.append(paragraph_model.make_sentence()) doc.generate_tex(filename)
def generate_pdf2(self, path, title) -> None: print('Generating pdf!') doc = Document(title) doc.default_filepath = path doc = self.create_tex(doc) doc.generate_pdf() doc.generate_tex() print('Done!')
def GenerateArrowDiagram(filepath, Parameters): doc = Document(documentclass="standalone", document_options=NoEscape("varwidth,convert={density=300,size=1080x800,outext=.png}")) doc.packages.append(Package('amsmath')) doc.packages.append(Package('tikz')) template = jenv.get_template('ArrowDiagram.tex') out = template.render(**Parameters) doc.append(NoEscape(out)) doc.generate_tex(filepath=filepath)
def create_appendix(stat_map): doc = Document('Appendixx') doc.append(NoEscape(r'\appendix')) with doc.create( Section(NoEscape('Appendix F: Conjectures for $s=3$ to 50'))): for s in range(3, 51): print('s: ', s) exceptions_list = [({}, 0)] num_exceptions = 0 for k in range(1, s): print('k: ', k) exceptions, start_k = find_exceptions(s, k, stat_map) num_exceptions += len(exceptions) exceptions_list.append((exceptions, start_k)) doc.append( NoEscape(r'\begin{conjecture}\label{co:' + str(s) + r'}')) doc.append( NoEscape( r'If $m=' + str(s) + r'k+i$ where $0\le i\le ' + str(s - 1) + '$ then $f(m,' + str(s) + r')$ depends only on $k,i$ via a formula, given below')) if num_exceptions == 0: doc.append('.') else: doc.append( NoEscape(', with ' + str(num_exceptions) + ' exceptions (we will note the exceptions).')) doc.append(NoEscape('\n')) doc.append( NoEscape(r'\noindent' + '\n' r'\text{\bf{Case 0:}} $m=' + str(s) + 'k+0$ with $k\ge 1$. Then $f(' + str(s) + 'k,' + str(s) + ')=1$.')) doc.append(NoEscape('\n')) for i in range(1, s): numer, denom = fc_pattern(i, s) if numer is not None: exceptions, start_k = exceptions_list[i] pattern_frac = r'\frac{' + numer + '}{' + denom + '}' doc.append(NoEscape(r'\noindent')) doc.append( NoEscape(r'\text{\bf{Case ' + str(i) + ':}} $m=' + str(s) + 'k+' + str(i) + r'$ with $k\ge ' + str(start_k) + r'$. Then $f(' + str(s) + 'k+' + str(i) + ',' + str(s) + ')=' + pattern_frac + '$.')) if len(exceptions) != 0: exp_str = ' (Exception: ' for exp in exceptions.keys(): q, exp_type = exceptions[exp] exp_str += r'$f(' + str(exp.numerator) + ',' + str(exp.denominator) + ')=' \ + str(q) + r'$ ' + exp_type + ', ' doc.append(NoEscape(exp_str.strip(', ') + '.)')) doc.append(NoEscape('\n')) doc.append( NoEscape(r'\end{conjecture}' + '\n\n' + '--------------------\n')) doc.generate_tex('Appendix')
def sols2tex(variables, file_name, user_input, autor): ''' Soluciones a documento TeX ''' from pylatex import Document, Section, Subsection, Command, Math, Tabular from pylatex.utils import italic, NoEscape doc = Document() doc.preamble.append(Command('title', 'Reporte pyENL')) doc.preamble.append(Command('author', autor)) doc.preamble.append(Command('date', NoEscape(r'\today'))) doc.append(NoEscape(r'\maketitle')) with doc.create(Section('Ecuaciones:')): # Por cada línea de user_input añadir un append y salto de línea #(si es comentario de usuario) comentario_user = False for eqn in user_input: if '<<' in eqn: # Entonces es un comentario de usuario comentario = eqn comentario = comentario.replace('<<', '') comentario = comentario.replace('>>', '') if comentario_user == True: doc.append('\n' + comentario) else: # Esto porque no hay necesidad de salto. doc.append(comentario) comentario_user = True else: # Entonces no es un comentario sino una ecuación, entonces # insertar con símbolos matemáticos. doc.append(Math(data=[eqn])) comentario_user = False # Ahora insertar los resultados with doc.create(Section('Resultados:')): # Acá viene una tabla con los resultados. with doc.create(Tabular('rc|cl')) as table: table.add_hline() table.add_row(('Variable', 'Valor', 'Descripción', 'Unidades')) table.add_hline(1, 4) table.add_empty_row() # Ahora por cada variable añadir la información requerida: for variable in variables: t_var = variable.name # Nombre variable t_val = variable.guess # Valor de la variable t_desc = variable.comment # Descripción de la variable t_unit = variable.units # Unidades de la variable table.add_row((t_var, t_val, t_desc, t_unit)) #table.add_row(('x', 5, 'Hola', 'm')) if file_name[-4::] == '.pdf': doc.generate_pdf(file_name[0:-4]) else: doc.generate_tex(file_name[0:-4])
def tikzpicture_to_tex_standalone(s, output): try: from pylatex import Document, NoEscape except: raise ("Please install package pylatex first\n\t pip install pylatex") if output.endswith(".tex"): output = output[:-4] doc = Document(documentclass="standalone") doc.preamble.append(NoEscape("\\usepackage{tikz}")) doc.preamble.append(NoEscape("\\usetikzlibrary{sbgn}")) doc.append(NoEscape(s)) doc.generate_tex(output)
def generate_latex(file_name, eqns=None): if eqns is None: eqns = [] doc = Document() with doc.create(Section('Automatic Conjectures')): doc.append('These are the conjectures detected by the algorithm.') for eqn in eqns: with doc.create(Alignat(numbering=False, escape=False)) as agn: agn.append(eqn) doc.generate_tex(file_name)
def generate_pdf(self, path, title) -> None: print('Generating pdf!') doc = Document(documentclass='book', document_options=['a4paper', '11pt']) doc.packages.append(Package('graphicx')) doc.default_filepath = path with doc.create(Section('Section 1')): doc.append(NoEscape(self.latex())) doc.generate_pdf() doc.generate_tex() # doc = self.create_tex() # doc.default_filepath = path # doc.generate_pdf(title, clean=True, clean_tex=True) print('Done!')
def test_document(): doc = Document( default_filepath="default_filepath", documentclass="article", fontenc="T1", inputenc="utf8", lmodern=True, data=None, ) doc.append("Some text.") doc.generate_tex(filepath="") doc.generate_pdf(filepath="", clean=True)
def test_document(): doc = Document(default_filepath='default_filepath', documentclass='article', fontenc='T1', inputenc='utf8', author='', title='', date='', data=None, maketitle=False) doc.append('Some text.') doc.generate_tex(filepath='') doc.generate_pdf(filepath='', clean=True)
def doit(outfile='summary', ndim=3, action=True): if action == False: print(outfile, ndim) return if ndim == 3: resfile = '05aperture_results_3d.txt' statfile = '3d_cluster_statistics.txt' elif ndim == 2: resfile = '05aperture_results_2d.txt' statfile = 'cluster_statistics.txt' # generate blank LaTeX doc geometry_options = {"tmargin": "1cm", "lmargin": "2cm"} doc = Document(geometry_options=geometry_options) # insert some introductory material dirname = os.getcwd() doc.append('Clustering output, found in') doc.append(dirname) # get the K-means section of results table and insert into the document with doc.create(Section('K-means results')): tmptex, ncl_list = get_stats(resfile, 'kmeans', oldcols_results, newcols_results, None, None) doc.append(tmptex) doc.append(NewPage()) # add the individual K-means runs add_sections_to_doc(ncl_list, 'kmeans', doc, statfile, ndim) # now the intro material for meanshift # get the results table and insert into the document with doc.create(Section('Meanshift results')): tmptex, ncl_list = get_stats(resfile, 'meanshift', oldcols_res_ms, newcols_res_ms, None, None) doc.append(tmptex) doc.append(NewPage()) # add the individual meanshift runs add_sections_to_doc(ncl_list, 'meanshift', doc, statfile, ndim) # turn the LaTex into a PDF doc.generate_tex(filepath=outfile) doc.generate_pdf(outfile, clean_tex=False) # all done! return
def create_latex_file(pokemon, list_of_normal_ivs, list_of_boosted_ivs): geometry_options = { "landscape": True, "margin": "0.5in", "headheight": "20pt", "headsep": "10pt", "includeheadfoot": True } doc = Document(page_numbers=True, geometry_options=geometry_options) doc.add_color('color100', 'rgb', rgb_to_dumb_string(0, 207, 194)) doc.add_color('color98', 'rgb', rgb_to_dumb_string(60, 141, 1)) doc.add_color('color96', 'rgb', rgb_to_dumb_string(72, 165, 6)) doc.add_color('color93', 'rgb', rgb_to_dumb_string(242, 183, 8)) doc.add_color('color91', 'rgb', rgb_to_dumb_string(246, 96, 0)) section = Section(f'{pokemon.name} IV Chart') table = Tabular('|c|c|c|c|c|c|', row_height=1.5) table.add_hline() table.add_row((MultiColumn( 6, align='|c|', data=TextColor('white', pokemon.name), color='black'), ), ) table.add_row( (MultiColumn(1, align='|c', data='Lv20'), MultiColumn(1, align='c|', data='Lv25'), MultiColumn(3, align='c|', data='IV'), MultiColumn(1, align='|c|', data='%'))) for index in range(len(list_of_normal_ivs)): n_iv = list_of_normal_ivs[index] b_iv = list_of_boosted_ivs[index] row_data = (f'{n_iv.cp}', f'{b_iv.cp}', f'{n_iv.attack}', f'{n_iv.defense}', f'{n_iv.stamina}', f'{n_iv.perfection_percent_rounded}') table.add_hline() table.add_row(row_data, color=color_by_iv(n_iv.perfection_percent_rounded)) section.append(table) doc.append(section) doc.generate_tex(f'latex/{pokemon.name}') doc.generate_pdf(f'pdf/{pokemon.name}')
def test_document(): doc = Document( default_filepath='default_filepath', documentclass='article', fontenc='T1', inputenc='utf8', lmodern=True, data=None, ) repr(doc) doc.append('Some text.') doc.generate_tex(filepath='') doc.generate_pdf(filepath='', clean=True)
def test_single_sentence_pdf_no_glyphs(self): content = "Hello, world." pdf_file = Document(default_filepath=self.strip_pdf_file_extension( self.output_pdf), page_numbers=False) pdf_file.append(content) pdf_file.generate_tex() pdf_file.generate_pdf(clean_tex=False) normalised_html = generate_normalised_html(self.output_pdf) soup = BeautifulSoup(normalised_html, 'html.parser') section = soup.section assert content in str(section.string)
def createTeX(file): doc = Document(file, documentclass='extarticle', document_options=('12pt', 'a4paper')) parse_text('fat.txt') fill_document(doc) cprint('Rendering PDF!', 'green') doc.generate_pdf(clean_tex=True, compiler='pdflatex') doc.generate_tex() print(colored('PDF created:', 'red'), file + '.pdf') cprint('Cleaning repository!', 'cyan') if os.path.exists('FAT.tex'): os.remove('FAT.tex') cprint('Finished!', 'magenta', attrs=['bold'])
def generate_heuristic_tex(G, matchings, output_dir, stats_filename): """ print statistics for the hospital proposing heuristic as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() M_s = matching_algos.stable_matching_hospital_residents( graph.copy_graph(G)) with doc.create(Subsection('Size statistics')): with doc.create(Tabular('|c|c|c|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio', 'block-R', 'rank-1', 'deficiency')) for desc in matchings: M = matchings[desc] sig = signature(G, M) bp = matching_utils.unstable_pairs(G, M) msize = matching_utils.matching_size(G, M) table.add_hline() table.add_row(( desc, msize, len(bp), len(bp) / (m - msize), len(blocking_residents(bp)), sum_ranks(sig, (1, )), #sum_ranks(sig, (1, 2, 3)), total_deficiency(G, M_s))) table.add_hline() stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def generate_report(project): file_path = "projects/{0}/{0}".format(project.id) # Basic document geometry_options = {"head": "40pt", "margin": "1in", "bottom": "1in"} doc = Document(default_filepath=file_path, geometry_options=geometry_options) doc.packages.append(Package('listings')) doc.packages.append(Package('color')) doc.append( NoEscape(r'''\definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{backcolour}{rgb}{0,0,0} \definecolor{mycolor}{rgb}{1,1,1} \lstdefinestyle{mystyle}{ backgroundcolor=\color{backcolour}, commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\tiny\color{codegray}, stringstyle=\color{codepurple}, basicstyle=\footnotesize\color{mycolor}, breakatwhitespace=false, breaklines=true, captionpos=b, keepspaces=true, numbers=left, numbersep=5pt, showspaces=false, showstringspaces=false, showtabs=false, tabsize=2, inputencoding=latin1 } \lstset{style=mystyle} ''')) doc.preamble.append(Command('title', project.name)) doc.preamble.append(Command('author', 'Anonymous author')) doc.preamble.append(Command('date', NoEscape(r'\today'))) doc.append(NoEscape(r'\maketitle')) for finding in project.findings: add_finding(doc, finding) # fill_document(doc) doc.generate_tex()
class PDFReport(object): def __init__(self): self.doc = self.begin_pdf() def begin_pdf(self): geometry_options = { "tmargin": "1cm", "lmargin": "1cm", "landscape": True } self.doc = Document(geometry_options=geometry_options) self.doc.packages.append(Package('float')) return self.doc def append_solution(self, solution_id, problem, solution): with self.doc.create(Multicols(arguments=[2])): image_filename = os.path.join( os.path.dirname('.'), 'images/solution_%d.png' % solution_id) with self.doc.create( Section('Problem with solution %d' % solution_id, label="problem%d" % solution_id)): with self.doc.create( Subsection('TSNE representation', label="tsne%d" % solution_id)): with self.doc.create(Figure(position='H')) as tsne: tsne.add_image(image_filename, width=NoEscape(r'11cm')) tsne.add_caption('TSNE 2d feature space') self.doc.append(VerticalSpace(NoEscape(r"\fill"))) with self.doc.create( Subsection('Problem and Solution', label="solution%d" % solution_id)): self.doc.append(problem) with self.doc.create(Verbatim()): self.doc.append(solution) self.doc.append(VerticalSpace(NoEscape(r"\fill"))) self.doc.append(NewPage()) return self.doc def generate_pdf(self, name): self.doc.generate_pdf(name, clean_tex=False) def generate_tex(self, name): self.doc.generate_tex(name)
def test_document(): doc = Document( default_filepath='default_filepath', documentclass='article', fontenc='T1', inputenc='utf8', author='', title='', date='', data=None, maketitle=False ) doc.append('Some text.') doc.generate_tex(filepath='') doc.generate_pdf(filepath='', clean=True)
def generate_hr_tex(G, matchings, output_dir, stats_filename): """ print statistics for the resident proposing stable, max-cardinality popular, and popular amongst max-cardinality matchings as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() with doc.create(Subsection('general statistics')): with doc.create(Tabular('|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio')) for desc in matchings: M = matchings[desc] sig = signature(G, M) msize = matching_utils.matching_size(G, M) bp = matching_utils.unstable_pairs(G, M) table.add_hline() table.add_row((desc, msize, len(bp), len(bp)/(m - msize))) table.add_hline() # statistics w.r.t. set A stats_for_partition(G, matchings, doc) # statistics w.r.t. set B # stats_for_partition(G, matchings, doc, False) stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def generate_hr_tex(G, matchings, output_dir, stats_filename): """ print statistics for the resident proposing stable, max-cardinality popular, and popular amongst max-cardinality matchings as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() with doc.create(Subsection('general statistics')): with doc.create(Tabular('|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio')) for desc in matchings: M = matchings[desc] sig = signature(G, M) msize = matching_utils.matching_size(G, M) bp = matching_utils.unstable_pairs(G, M) table.add_hline() table.add_row((desc, msize, len(bp), len(bp) / (m - msize))) table.add_hline() # statistics w.r.t. set A stats_for_partition(G, matchings, doc) # statistics w.r.t. set B # stats_for_partition(G, matchings, doc, False) stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def doit(outfile = 'summary', ndim=3, action=True): if action == False: print(outfile, ndim) return if ndim == 3: resfile = '05aperture_results_3d.txt' statfile = '3d_cluster_statistics.txt' elif ndim == 2: resfile = '05aperture_results_2d.txt' statfile = 'cluster_statistics.txt' # generate blank LaTeX doc geometry_options = {"tmargin": "1cm", "lmargin": "2cm"} doc = Document(geometry_options=geometry_options) # insert some introductory material dirname = os.getcwd() doc.append('Clustering output, found in') doc.append(dirname) # get the K-means section of results table and insert into the document with doc.create(Section('K-means results')): tmptex, ncl_list = get_stats(resfile, 'kmeans', oldcols_results, newcols_results, None, None) doc.append(tmptex) doc.append(NewPage()) # add the individual K-means runs add_sections_to_doc(ncl_list, 'kmeans', doc, statfile, ndim) # now the intro material for meanshift # get the results table and insert into the document with doc.create(Section('Meanshift results')): tmptex, ncl_list = get_stats(resfile, 'meanshift', oldcols_res_ms, newcols_res_ms, None, None) doc.append(tmptex) doc.append(NewPage()) # add the individual meanshift runs add_sections_to_doc(ncl_list, 'meanshift', doc, statfile, ndim) # turn the LaTex into a PDF doc.generate_tex(filepath=outfile) doc.generate_pdf(outfile, clean_tex=False) # all done! return
def generateExam(self): doc = Document('basic') doc.documentclass = Command( 'documentclass', options=['12pt'], arguments=['exam'], ) doc.packages.append(Package('amsmath')) doc.preamble.append(Command('pagestyle', 'headandfoot')) doc.preamble.append( Command( 'firstpageheader', NoEscape( r"""%s : %s \\ \today}{}{Name: \underline{\hspace{2.5in}}""" % (self.course, self.subject)))) doc.append( Command( 'center', Command( 'fbox', Command('fbox', 'NO CALCULATORS OR EXTERNAL RESOURCES')))) doc.append(Command('begin', 'questions')) for mcq in self.mcqs: doc.append(NoEscape(r'\question ' + mcq.prompt)) doc.append(Command('begin', 'checkboxes')) for ans in mcq.answers: doc.append(Command('choice', ans)) for frq in self.frqs: doc.append(NoEscape(r'\question ' + NoEscape(frq.prompt))) doc.append(Command('vspace', frq.spacing + 'in')) doc.append(Command('end', 'questions')) big_code = pyqrcode.create(np.array_str(self.answerKey), mode='binary') big_code.png('code.png') with doc.create(Figure(position='b!')) as code: code.add_image('code.png', width='50px') doc.generate_pdf(clean_tex=False, compiler='pdfLaTeX') doc.generate_tex()
def plot_circuit(circuit): tex = circuit_to_latex_using_qcircuit(circuit) doc = Document(documentclass='standalone', document_options=NoEscape( 'border=25pt,convert={density=300,outext=.png}')) doc.packages.append(Package('amsmath')) doc.packages.append(Package('qcircuit')) doc.append(NoEscape(tex)) tmp_folder = mkdtemp() doc.generate_tex(tmp_folder + '/circuit') proc = subprocess.Popen( ['pdflatex', '-shell-escape', tmp_folder + '/circuit.tex'], cwd=tmp_folder) proc.communicate() image = plt.imread(tmp_folder + '/circuit.png') shutil.rmtree(tmp_folder) plt.axis('off') return plt.imshow(image)
def generate_comparison(dims, algorithm_names, algorithm_results, mode='COST'): # Creating the document from_mode = '' if mode == 'COST': doc = Document('latex_tables/generated_table_cost_comparison') section = Section("Table of Costs") from_mode = "{:.2f}" if mode == 'TIME': doc = Document('latex_tables/generated_table_time_comparison') section = Section("Table of Times") from_mode = "{:.2e}" # Setting tabular property tabular_header = 'c' for i in algorithm_names: tabular_header += 'c' # Creation of the Table table = Tabular(tabular_header) table.add_row((MultiColumn(len(algorithm_names) + 1, align='c', data='Costs'), )) table.add_hline() table.add_hline() table.add_row(['Nodi'] + algorithm_names) table.add_hline() for i in range(len(dims)): table.add_row((dims[i], from_mode.format(algorithm_results[0][i]), from_mode.format(algorithm_results[1][i]), from_mode.format(algorithm_results[2][i]), from_mode.format(algorithm_results[3][i]), from_mode.format(algorithm_results[4][i]))) # We close the table table.add_hline() # And finally we compose and generate the new document with the embedded table section.append(table) doc.append(section) doc.generate_tex()
def test_single_sentence_pdf_with_fi_glyph(self): # LaTex font renders the fi in file as a single glyph, which is then read back as a # single unicode character. content = "Hello, world. I am a PDF file" pdf_file = Document(default_filepath=self.strip_pdf_file_extension( self.output_pdf), page_numbers=False) pdf_file.append(content) pdf_file.generate_tex() pdf_file.generate_pdf(clean_tex=False) normalised_html = generate_normalised_html(self.output_pdf) soup = BeautifulSoup(normalised_html, 'html.parser') section = soup.section assert content in str(section.string)
def generate_heuristic_tex(G, matchings, output_dir, stats_filename): """ print statistics for the hospital proposing heuristic as a tex file :param G: graph :param matchings: information about the matchings """ # create a tex file with the statistics doc = Document('table') # add details about the graph, |A|, |B|, and # of edges n1, n2, m = len(G.A), len(G.B), sum(len(G.E[r]) for r in G.A) with doc.create(Subsection('graph details')): with doc.create(Tabular('|c|c|')) as table: table.add_hline() table.add_row('n1', n1) table.add_hline() table.add_row('n2', n1) table.add_hline() table.add_row('m', m) table.add_hline() M_s = matching_algos.stable_matching_hospital_residents(graph.copy_graph(G)) with doc.create(Subsection('Size statistics')): with doc.create(Tabular('|c|c|c|c|c|c|c|')) as table: table.add_hline() table.add_row(('description', 'size', 'bp', 'bp ratio', 'block-R', 'rank-1', 'deficiency')) for desc in matchings: M = matchings[desc] sig = signature(G, M) bp = matching_utils.unstable_pairs(G, M) msize = matching_utils.matching_size(G, M) table.add_hline() table.add_row((desc, msize, len(bp), len(bp)/(m - msize), len(blocking_residents(bp)), sum_ranks(sig, (1,)), #sum_ranks(sig, (1, 2, 3)), total_deficiency(G, M_s))) table.add_hline() stats_abs_path = os.path.join(output_dir, stats_filename) doc.generate_pdf(filepath=stats_abs_path, clean_tex='False') doc.generate_tex(filepath=stats_abs_path)
def mk_tex(self, filename): # create he latex doc doc = Document() # Check to avoid index error and create the table border lines if self.table != []: size = len(self.table[0]) header = '|l' * size header += '|' else: print('No values assigned to the table') return None # Fill the table and create a tex file in the New_Files Folder with doc.create(Tabular(header)) as table: table.add_hline() for row in self.table: table.add_row(row) table.add_hline() doc.generate_tex('../New_Files/' + filename) print("Succesfully Created", '../New_Files/' + filename + '.tex')
def translate(self, filename: str): doc = Document('basic') doc.append(NoEscape(r'\noindent')) doc.append(NoEscape("\\newcommand{\err}[1]{\\textit{#1}}")) # ! for block in self.blocks: doc.append(NoEscape(block.getOutput() + '\\\\')) babel = Package('babel', 'english, russian') fontenc = Package('fontenc', 'T2A') inputenc = Package('inputenc', 'utf8') doc.packages.items = [fontenc, inputenc, babel] doc.generate_tex(filename) try: doc.generate_pdf(filename, clean_tex=False) except Exception as e: print(e)
def main(argv): doclink = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hl:o:n:",["doclink=","output=","toolname="]) except getopt.GetoptError: sys.exit(2) global defaultname for opt, arg in opts: if opt == '-h': print("paperpp.py -l <document class link> -o <outputfile> -n <name>") sys.exit() elif opt in ("-l", "--doclink"): doclink = arg elif opt in ("-n", "--toolname"): defaultname = arg elif opt in ("-o", "--output"): outputfile = arg documentcls=download_doc_class(doclink) print("Initialize docclass:",documentcls) paperfile = currdir+ os.sep +"paper" macrofile = currdir+ os.sep +"macro.tex" with open(macrofile,"w+") as f: f.write("\\newcommand{\\toolname}{"+defaultname+"}") name = current_user() print("Current_user:"******".tex",paperfile+".tex.copy") #doc.generate_pdf(paperfile) shutil.copy2(paperfile+".tex.copy",paperfile+".tex") os.remove(paperfile+".tex.copy")
for identifier in identifiers: color = { "eval7_concat_noopt": "red", "eval7_concat_opt": "blue" }[identifier] name = { "eval7_concat_noopt": "no optimisation", "eval7_concat_opt": "concat flattening" }[identifier] plot.append(Plot(name = name, options = "very thick,color={}".format(color), coordinates = coordinates[identifier])) name = "eval7_concat_compiletime" print("Generating {}.pdf".format(name)) doc.generate_pdf("../tex/{}".format(name), compiler = "D:/Software/texlive/bin/win32/pdflatex.exe") print("Generating {}.tex".format(name)) doc.generate_tex("../tex/{}".format(name)) print("Done {}.".format(name)) print("") # for identifier in identifiers: # print("Preparing {}".format(identifier)) # doc = Document() # doc.packages.append(Package("geometry", options = ["tmargin=1cm", "lmargin=1cm"])) # for s in series: # with doc.create(Section("Series: {}".format(s["name"]))): # algorithms = ["fifo", "lru", "mru", "specialised"] # # algorithms = ["mru", "specialised", "specialisedOrig"]
# Document doc = Document( default_filepath='default_filepath', documentclass='article', fontenc='T1', inputenc='utf8', author='', title='', date='', data=None, maketitle=False ) doc.append('Some text.') doc.generate_tex(filepath='') doc.generate_pdf(filepath='', clean=True) # SectionBase s = Section(title='', numbering=True, data=None) # Math m = Math(data=None, inline=False) # Tabular t = Tabular(table_spec='|c|c|', data=None, pos=None) t.add_hline(start=None, end=None) t.add_row(cells=(1, 2), escape=False)
class Invoice: def __init__(self, id=None, subject=None, client=None, seller=None, items=None, offer=False): self.id = str(id) self.subject = subject # Rechnungsbeschriebung self.client = client # Kundendaten self.me = seller # Verkäufer self.discount = 0 # Rabatt self.items = items if items is not None else [] self.filename = self.id + '-' + time.strftime('%Y') self.documentclass = None self.docoptions = 'DIN,pagenumber=false,parskip=half,fromalign=right,fromphone=true,fromfax=false,fromrule=false,fontsize=12pt' self.doc = None if offer is True: self.setoffer() else: self.categroy = ['Rechnung', 'Rechnungsnummer'] self.statictext = { 'tdef': '\\begin{spreadtab}{{tabularx}{\linewidth}{lXrr}}', 'thead': '@ Anzahl & @ Beschreibung & @ Einzelpreis & @ Gesamtpreis \\\\ \\hline', 'temptyrow': '@ & @ & @ & @ \\\\', 'tsep': '\\\\ \\hline \\hline \\\\', 'tsum': ' & & @ Nettobetrag Gesamt & :={sum(d2:[0,-3])} \\euro \\\\', 'tvat': ' & & @ MwSt. 19\% & :={[0,-1]*0.19+0.00} \\euro \\\\', 'ttotal': ' & & @ Bruttobetrag Gesamt & :={sum([0,-2]:[0,-1])} \\euro \\\\' } def setoffer(self): self.categroy = ['Angebot', 'Angebotsnummer'] def setuplatex(self): self.filename = self.id + '-' + time.strftime('%Y') self.documentclass = Command('documentclass', arguments='scrlttr2', options=self.docoptions) self.doc = Document(self.filename, documentclass=self.documentclass, fontenc='T1', inputenc='utf8') self.doc.preamble.append(Command('input', latex_preamble)) self.doc.preamble.append(Command('LoadLetterOption', 'template')) self.doc.preamble.append(Command('setkomavar', arguments='subject', extra_arguments=self.subject)) self.doc.preamble.append(Command('setkomavar', arguments='yourmail', options=self.categroy[1], extra_arguments=self.filename)) # Falls man Kundennummer implementieren möchte. # %\setkomavar{yourref}[Ihre Kundennummer]{263} def generate(self): self.cli_input_details() # Details in Dokument eintragen self.setuplatex() # Latex konfigurieren. self.cli_input_items() # Items abfragen self.discount = input(_('Ermäßigung in %: [0%] ')) self.discount = 0 if self.discount == '' else int(self.discount) multi = round(1-self.discount/100, 2) self.statictext['tdiscount'] = NoEscape(' & & @ Ermäßigung ' + str(self.discount) + '\% & :={[0,-1]*' + str(multi) + '+0.00} \\euro \\\\') self.fill_document() # Latex füllen. self.doc.generate_pdf(settings.latex['output_folder'] + self.filename, compiler=pdflatex, silent=latex_silent) if latex_output: self.doc.generate_tex(self.filename) def cli_input_details(self): print(_('Bitte Rechnungsdetails angeben: ')) self.id = input(self.categroy[1] + ': ') self.subject = input(_('Betreff: ')) def cli_input_items(self): i = input(_('Anzahl an Positionen? ')) for i in range(0, int(i)): new_item = Item() self.items.append(new_item) separator() def additems(self): for item in self.items: tail = '\\euro & :={[-3,0]*[-1,0]} \\euro \\\\' self.doc.append(NoEscape(str(item.qt) + ' & @ ' + item.desc + item.desc2 + ' & :={' + str(item.price) + '} ' + tail)) def fill_document(self): self.doc.append(Command('begin', arguments='letter', extra_arguments=self.client.getaddress())) self.doc.append(Command('opening', ' ')) self.doc.append(UnsafeCommand('vspace', '-1.0cm')) self.doc.append(Command('STautoround*', '2')) # Round 2 decimals self.doc.append(Command('STsetdecimalsep', ',')) # Decimal separator sign self.doc.append(NoEscape(self.statictext['tdef'])) # Table definition self.doc.append(NoEscape(self.statictext['thead'])) # Table head self.doc.append(NoEscape(self.statictext['temptyrow'])) # Empty row self.additems() # All the items self.doc.append(NoEscape(self.statictext['tsep'])) # Seperator row self.doc.append(NoEscape(self.statictext['tsum'])) # Sum of all items if self.discount != 0: self.doc.append(NoEscape(self.statictext['tdiscount'])) self.doc.append(NoEscape(self.statictext['tvat'])) # VAT self.doc.append(NoEscape(self.statictext['ttotal'])) # Total = VAT + sum self.doc.append(Command('end', 'spreadtab')) # End of table self.doc.append(Command('end', 'letter')) # End of document
class LatexDoc: """ LatexDoc类用来创建、操作latex文档,生成pdf文档 """ def __init__(self,documentclass='article',options=None,document=None): if document is None: self.doc = Document(documentclass=documentclass,options=options) else: self.doc = document.doc def preamble_append(self,content): """ 添加内容到latex文档中 :param str content: latex格式字符串 :return: 无返回值 """ self.doc.preamble.append(NoEscape(content)) def append(self,content): """ 添加内容到latex文档中 :param str content: latex格式字符串 :return: 无返回值 """ self.doc.append(NoEscape(content)) def add_section(self,section_name,level=1): """ 添加章节 :param str section_name: 章节名称 :param int level: 章节级别,1表示最高级,2其次,3最次。 :return: 无返回值 """ if level < 2: self.append(''.join(['\\section*{',section_name,'}'])) elif level < 3: self.append(''.join(['\\subsection*{',section_name,'}'])) else: self.append(''.join(['\\subsubsection*{',section_name,'}'])) def add_list(self,lists,type=1): """ 添加列表 :param list lists: 列表名称 :param int type: 列表类型 :return: 无返回值 """ if type == 1: items = Itemize() elif type == 2: items = Enumerate() elif type == 3: items = Description() else: items = Itemize() for item in lists: items.add_item(item) self.doc.append(items) def add_table(self, data=None): """ 添加表格 :param list data: 表格数据 :return: 无返回值 """ nrow = len(data) ncol = len(data[0]) tabsize = '|' + '|'.join(['c']*ncol) + '|' mtable = Tabular(tabsize) for i in range(nrow): mtable.add_hline() mtable.add_row(tuple([str(item) for item in data[i]])) mtable.add_hline() self.doc.append(Command('begin',arguments='center')) self.doc.append(mtable) self.doc.append(Command('end',arguments='center')) def add_pretty_table(self,data=None,caption='Table',width='90mm'): """ 添加漂亮的表格 :param list data: 表格数据 :param str caption: 表格名称 :param str width: 表格宽度 :return: 无返回值 """ ncol = len(data[0]) self.append(''.join(['\\begin{tabularx}{',width,'}{','X'*ncol,'}'])) self.append('\\toprule') self.append(''.join(['\\multicolumn{',str(ncol),'}{c}{',caption,'} \\\\'])) self.append(''.join(['\\cmidrule(r){1-',str(ncol),'}'])) self.append(''.join([' & '.join([str(item) for item in data[0]]),' \\\\'])) self.append('\\midrule') for item in data[1:]: self.append(''.join([' & '.join([str(unit) for unit in item]),' \\\\'])) self.append('\\bottomrule') self.append('\\end{tabularx}') def add_figure(self,file=None,caption=None,width='240px'): """ 添加图形 :param str file: 图形文件路径 :param str caption: 图形标题 :param str width: 图形宽度 :return: 无返回值 """ graph = Figure(position='h!') graph.add_image(file, width=width) if caption is not None: graph.add_caption(caption) self.doc.append(graph) def add_package(self,name,options=None): """ 添加latex包 :param str name: latex包的名字 :param list options: latex包的参数 :return: 无返回值 """ self.doc.packages.append(Package(name=name,options=options)) def generate_tex(self,filepath=None): """ 创建.tex文件 :param str filepath: 文件路径名称 :return: 无返回值 """ self.doc.generate_tex(filepath=filepath) def generate_pdf(self,filepath=None): """ 创建pdf文件 :param str filepath: 文件路径名称 :return: 无返回值 """ self.doc.generate_pdf(filepath=filepath)
class TopicLatexGenerator: def __init__(self, lda_based_context): self.lda_based_context = lda_based_context self.doc =\ Document(Constants.ITEM_TYPE + '-topic-models-nouns-complete-3') self.num_cols = Constants.TOPIC_MODEL_STABILITY_NUM_TERMS self.num_topics = Constants.TOPIC_MODEL_NUM_TOPICS self.rgb_tuples = None self.automatic_context_topic_colors = None self.keyword_context_topic_colors = None self.manual_context_topic_colors = None self.automatic_context_topic_ids = None self.keyword_context_topic_ids = None self.manual_context_topic_ids = None self.automatic_context_topic_words = None self.keyword_context_topic_words = None self.manual_context_topic_words = None self.headers = None self.topic_words_map = None self.table_format = '|c|' + 'c|' * (self.num_cols + 1) self.tagger = nltk.PerceptronTagger() self.tag_count_map = {'NN': 0, 'JJ': 0, 'VB': 0} self.init_colors() self.init_headers() self.init_topic_words() self.init_topic_ids() self.doc.packages.append(Package('color')) new_comm = UnsafeCommand( 'newcommand', '\exampleCommand', options=4, extra_arguments=r'\colorbox[rgb]{#1,#2,#3}{#4} \color{black}') self.doc.append(new_comm) new_comm2 = UnsafeCommand('tiny') self.doc.append(new_comm2) def init_colors(self): golden_ratio = 0.618033988749895 hsv_tuples = [((x * golden_ratio) % 1.0, 0.5, 0.95) for x in range(self.num_topics)] self.rgb_tuples = map(lambda hsv: colorsys.hsv_to_rgb(*hsv), hsv_tuples) color_index = 0 self.automatic_context_topic_colors = {} for topic in self.lda_based_context.context_rich_topics: topic_id = topic[0] self.automatic_context_topic_colors[topic_id] = color_index # self.rgb_tuples[color_index] color_index += 1 # # color_index = 0 # self.keyword_context_topic_colors = {} # for topic_id in range(self.num_topics): # topic_score = split_topic( # self.lda_based_context.topic_model.print_topic( # topic_id, topn=self.num_cols)) # if topic_score['score'] > 0: # self.keyword_context_topic_colors[topic_id] = color_index # color_index += 1 color_index = 0 self.manual_context_topic_colors = {} for topic in context_words[Constants.ITEM_TYPE]: self.manual_context_topic_colors[topic] = color_index color_index += 1 def init_headers(self): self.headers = ['ID', 'Ratio'] for column_index in range(self.num_cols): self.headers.append('Word ' + str(column_index + 1)) def init_topic_words(self): # pass self.topic_words_map = \ extract_topic_words( self.lda_based_context, range(self.num_topics)) def init_topic_ids(self): self.automatic_context_topic_ids = [ topic[0] for topic in self.lda_based_context.context_rich_topics] # # self.keyword_context_topic_ids = [] # for topic_id in range(self.num_topics): # topic_score = split_topic( # self.lda_based_context.topic_model.print_topic( # topic_id, topn=self.num_cols)) # if topic_score['score'] > 0: # self.keyword_context_topic_ids.append(topic_id) self.manual_context_topic_ids = range(len(context_words[Constants.ITEM_TYPE])) def create_automatic_context_topics(self): with self.doc.create(Section( Constants.ITEM_TYPE.title() + ' context-rich topic models (automatic)')): num_context_topics = len(self.lda_based_context.context_rich_topics) with self.doc.create(LongTabu(self.table_format)) as table: table.add_hline() table.add_row(self.headers, mapper=bold_mapper) table.add_hline() for topic in self.lda_based_context.context_rich_topics: topic_id = topic[0] ratio = topic[1] color_id = self.automatic_context_topic_colors[topic_id] color = self.rgb_tuples[color_id] id_cell = str(topic_id) + str('|||') + str(color[0]) + \ '|||' + str(color[1]) + '|||' + str(color[2]) ratio_cell = str(ratio) + str('|||') + str(color[0]) + \ '|||' + str(color[1]) + '|||' + str(color[2]) row = [id_cell, ratio_cell] # row = [str(topic_id + 1)] topic_words =\ self.lda_based_context.print_topic( topic_id, self.num_cols).split(' + ') for word in topic_words: word_color = word + str('|||') + str(color[0]) +\ '|||' + str(color[1]) + '|||' +\ str(color[2]) row.append(word_color) # row.extend(topic_words) table.add_row(row, mapper=background_color_mapper) table.add_hline() self.doc.append(UnsafeCommand('par')) self.doc.append( 'Number of context-rich topics: %d' % num_context_topics) def create_keyword_context_topics(self): with self.doc.create(Section( Constants.ITEM_TYPE.title() + ' context-rich topic models (keyword)')): num_context_topics = 0 with self.doc.create(Tabular(self.table_format)) as table: table.add_hline() table.add_row(self.headers, mapper=bold_mapper) table.add_hline() for topic_id in range(self.num_topics): topic_score = split_topic( self.lda_based_context.topic_model.print_topic( topic_id, topn=self.num_cols)) if topic_score['score'] > 0: color_id = self.keyword_context_topic_colors[topic_id] color = self.rgb_tuples[color_id] id_cell = str(topic_id)+str('|||')+str(color[0]) + \ '|||'+str(color[1])+'|||'+str(color[2]) row = [id_cell] for column_index in range(self.num_cols): word = topic_score['word' + str(column_index)] word_color = word+str('|||')+str(color[0])+'|||' + \ str(color[1])+'|||'+str(color[2]) row.append(word_color) table.add_row(row, mapper=background_color_mapper) num_context_topics += 1 table.add_hline() self.doc.append(UnsafeCommand('par')) self.doc.append( 'Number of context-rich topics: %d' % num_context_topics) def create_manual_context_topics(self): with self.doc.create(Section( Constants.ITEM_TYPE.title() + ' context-rich topic models (manual)')): with self.doc.create(Tabular(self.table_format)) as table: table.add_hline() table.add_row(self.headers, mapper=bold_mapper) table.add_hline() for topic in context_words[Constants.ITEM_TYPE]: color_id = self.manual_context_topic_colors[topic] color = self.rgb_tuples[color_id] id_cell = str(topic)+str('|||')+str(color[0]) + \ '|||'+str(color[1])+'|||'+str(color[2]) row = [id_cell] for word in list(context_words[Constants.ITEM_TYPE][topic])[:self.num_cols + 1]: word_color = word+str('|||')+str(color[0])+'|||' + \ str(color[1])+'|||'+str(color[2]) row.append(word_color) table.add_row(row, mapper=background_color_mapper) table.add_hline() self.doc.append(UnsafeCommand('par')) # self.doc.append( # 'Number of context-rich topics: %d' % # len(context_words[Constants.ITEM_TYPE])) self.doc.append(ColorBoxCommand( arguments=Arguments( 1, 1, 1, 'Number of context-rich topics: ' + str(len(context_words[Constants.ITEM_TYPE]))))) def create_reviews(self): with self.doc.create(Section('Reviews')): with self.doc.create(Subsection('A subsection')): sg_map = {'yes': 'specific', 'no': 'generic'} review_index = 0 # full_records = ETLUtils.load_json_file( # Constants.FULL_PROCESSED_RECORDS_FILE) records_file = Constants.DATASET_FOLDER +\ 'classified_' + Constants.ITEM_TYPE + '_reviews.json' full_records = ETLUtils.load_json_file(records_file) for record in full_records: with self.doc.create(Subsection( 'Review %d (%s)' % ( (review_index + 1), sg_map[record['specific']]))): # self.build_text(record[Constants.TEXT_FIELD]) # for doc_part in self.build_text( # record[Constants.TEXT_FIELD]): for doc_part in self.build_text_automatic(record): self.doc.append(doc_part) review_index += 1 def generate_pdf(self): self.create_automatic_context_topics() # self.create_keyword_context_topics() # self.create_manual_context_topics() self.create_reviews() self.doc.generate_pdf() self.doc.generate_tex() def build_text(self, review): words = extract_words(review) doc_parts = [] new_words = [] for word in words: word_found = False for topic_id in self.automatic_context_topic_ids: if word in self.topic_words_map[topic_id]: self.tag_word(word) doc_parts.append(' '.join(new_words)) # doc_parts.append('topic: %d word: %s' % (topic_id, word)) color_id = self.automatic_context_topic_colors[topic_id] color = self.rgb_tuples[color_id] # doc_parts.append(ColorBoxCommand( # arguments=Arguments( # color[0], color[1], color[2], word))) new_words.append( '\\colorbox[rgb]{' + str(color[0]) + ',' + str(color[1]) + ',' + str(color[2]) + '}{' + word + '}') # new_words = [] word_found = True break if not word_found: new_words.append(word) print('new_words', new_words) self.doc.append(NoEscape(' '.join(new_words))) # return doc_parts def build_text_automatic(self, record): text = record[Constants.TEXT_FIELD] sentences = nlp_utils.get_sentences(text) lemmatized_words = [] for sentence in sentences: lemmatized_words.append(nlp_utils.lemmatize_sentence( sentence, nltk.re.compile(''), min_length=1, max_length=100)) doc_parts = [] itemize = Itemize() for sentence in lemmatized_words: new_words = [] itemize.add_item('') for tagged_word in sentence: tag = tagged_word[1] word = tagged_word[0] singular = pattern.text.en.singularize(word) word_found = False # if tag == 'VBD': # new_words.append( # '\\colorbox[rgb]{0.5,0.5,0.5}{' + word + '}') # word_found = True # # if tag.startswith('PRP'): # new_words.append( # '\\colorbox[rgb]{0.85,0.85,0.85}{' + word + '}') # word_found = True for topic_id in self.automatic_context_topic_ids: if word in self.topic_words_map[topic_id]: # if singular in context_words[Constants.ITEM_TYPE][topic]: self.tag_word(word) color_id = self.automatic_context_topic_colors[topic_id] color = self.rgb_tuples[color_id] new_words.append( '\\colorbox[rgb]{' + str(color[0]) + ',' + str(color[1]) + ',' + str(color[2]) + '}{' + word + '}') word_found = True break if not word_found: new_words.append(word) itemize.append(NoEscape(' '.join(new_words))) doc_parts.append(itemize) return doc_parts def build_text_manual(self, record): text = record[Constants.TEXT_FIELD] sentences = nlp_utils.get_sentences(text) lemmatized_words = [] for sentence in sentences: lemmatized_words.append(nlp_utils.lemmatize_sentence( sentence, nltk.re.compile(''), min_length=1, max_length=100)) doc_parts = [] itemize = Itemize() for sentence in lemmatized_words: new_words = [] itemize.add_item('') for tagged_word in sentence: tag = tagged_word[1] word = tagged_word[0] singular = pattern.text.en.singularize(word) word_found = False if tag == 'VBD': new_words.append('\\colorbox[rgb]{0.5,0.5,0.5}{'+ word + '}') word_found = True if tag.startswith('PRP'): new_words.append('\\colorbox[rgb]{0.85,0.85,0.85}{' + word + '}') word_found = True for topic in context_words[Constants.ITEM_TYPE]: if singular in context_words[Constants.ITEM_TYPE][topic]: color_id = self.manual_context_topic_colors[topic] color = self.rgb_tuples[color_id] new_words.append( '\\colorbox[rgb]{' + str(color[0]) + ',' + str(color[1]) + ',' + str(color[2]) + '}{' + word + '}') word_found = True break if not word_found: new_words.append(word) itemize.append(NoEscape(' '.join(new_words))) doc_parts.append(itemize) return doc_parts def tag_word(self, word): tagged_word = self.tagger.tag([word])[0] word_tag = tagged_word[1] if word_tag.startswith('NN'): self.tag_count_map['NN'] += 1 elif word_tag.startswith('JJ'): self.tag_count_map['JJ'] += 1 elif word_tag.startswith('VB'): self.tag_count_map['VB'] += 1 else: if word_tag not in self.tag_count_map: self.tag_count_map[word_tag] = 0 self.tag_count_map[word_tag] += 1 def get_topic_statistics(self, topic_ids): tags_dict = {'NN': 0, 'JJ': 0, 'VB': 0} topic_words = set() for topic_id in topic_ids: topic_words.update(self.topic_words_map[topic_id]) print(topic_words) for word in topic_words: tagged_word = self.tagger.tag([word])[0] word_tag = tagged_word[1] if word_tag.startswith('NN'): tags_dict['NN'] += 1 elif word_tag.startswith('JJ'): tags_dict['JJ'] += 1 elif word_tag.startswith('VB'): tags_dict['VB'] += 1 else: if word_tag not in tags_dict: tags_dict[word_tag] = 0 tags_dict[word_tag] += 1 print(tags_dict) total_count = 0.0 for tag_count in tags_dict.values(): total_count += tag_count print('nouns percentage: %f%%' % (tags_dict['NN'] / total_count * 100)) return tags_dict
ymin = min([min([max([s["function"](h, ncm, cm) for s in series]) for (h, ncm, cm) in data[algorithm]]) for algorithm in algorithms]) ymax = max([max([max([s["function"](h, ncm, cm) for s in series]) for (h, ncm, cm) in data[algorithm]]) for algorithm in algorithms]) if ymin < 0: ymin *= 1.1 else: ymin *= 0.9 if ymax > 0: ymax *= 1.1 else: ymax *= 0.9 ymin, ymax = int(ymin), int(ymax) for algorithm in algorithms: currentData = data[algorithm] options = "title={}, height=5cm, width=5cm, grid=major, xmin=(-50), xmax=({}), ymin=({}), ymax=({})".format(algorithm, xmax, ymin, ymax) with doc.create(TikZ()): with doc.create(Axis(options = options)) as plot: for s in series: color = s["color"] f = s["function"] coordinates = [(t, f(h, ncm, cm)) for (t, (h, ncm, cm)) in enumerate(currentData)] plot.append(Plot(options = "very thick,color={}".format(color), coordinates = coordinates)) print("Generating {}.pdf".format(identifier)) doc.generate_pdf("../tex/{}".format(identifier), compiler = "D:/Software/texlive/bin/win32/pdflatex.exe") print("Generating {}.tex".format(identifier)) doc.generate_tex("../tex/{}".format(identifier)) print("Done {}.".format(identifier)) print("")
def create_reading_section(document, medium): with document.create(Subsection('Reading')): with document.create(Itemize()) as itemize: try: df = pd.read_csv(os.path.join(medium, 'reading.csv')) except pd.errors.EmptyDataError: return for _, row in df.iterrows(): title = row.Title link = row.Link itemize.add_item(italic(title)) itemize.append(NewLine()) arguments = Arguments(link, link) itemize.append(Command('href', arguments=arguments)) for medium in ['books', 'papers']: with document.create(Section(medium.capitalize())): if medium == 'books': create_read_section(document, medium) create_reading_section(document, medium) elif medium == 'papers': create_read_section(document, medium) document.generate_tex('read') document.generate_pdf('read', compiler='pdflatex', clean_tex=False)
# Document doc = Document( default_filename='default_filename', documentclass='article', fontenc='T1', inputenc='utf8', author='', title='', date='', data=None, maketitle=False ) doc.append('Some text.') doc.generate_tex(filename='') doc.generate_pdf(filename='', clean=True) # SectionBase s = Section(title='', numbering=True, data=None) # Math m = Math(data=None, inline=False) # Table t = Table(table_spec='|c|c|', data=None, pos=None, table_type='tabular') t.add_hline(start=None, end=None) t.add_row(cells=(1, 2), escape=False)
""" with doc.create(Section("A section")): doc.append("Some regular text and some ") doc.append(italic("italic text. ")) with doc.create(Subsection("A subsection")): doc.append("Also some crazy characters: $&#{}") if __name__ == "__main__": # Basic document doc = Document("basic") fill_document(doc) doc.generate_pdf() doc.generate_tex() # Document with `\maketitle` command activated doc = Document() doc.preamble.append(Command("title", "Awesome Title")) doc.preamble.append(Command("author", "Anonymous author")) doc.preamble.append(Command("date", NoEscape(r"\today"))) doc.append(NoEscape(r"\maketitle")) fill_document(doc) doc.generate_pdf("basic_maketitle", clean=False) # Add stuff to the document with doc.create(Section("A second section")):
def create_pdf(recipes): doc = Document() doc.packages.append(Package("hyperref", options="hidelinks")) doc.append("\\begin{titlepage}\n" "\\centering\n" "{\\scshape\\Huge\\textbf The Greater Book of Transmutation \\par}\n" "\\vspace{1.5cm}\n" "{\\scshape\\large How to make almost anything in a few easy steps \\par}\n" "\\vspace{5.5cm}\n" "{\\large A Procedurally Generated DIY Book \\par}\n" "{\\large for the \href{https://github.com/dariusk/NaNoGenMo-2015}{NaNoGenMo 2015} \\par}\n" "\\vspace{0.5cm}\n" "{\\large by \\href{http://dragonlab.de}{Tobias Wehrum} \\par}\n" "\\vfill\n" "{\\scriptsize using data by: \\par}\n" "\\vspace{0.3cm}\n" "{\\scriptsize\\href{http://web.usf.edu/FreeAssociation}{Nelson, D. L., McEvoy, C. L., \\& Schreiber, T. A. (1998). The University of South Florida word association, rhyme, and word fragment norms}\\par}\n" "\\vspace{0.3cm}\n" "{\\scriptsize\\href{https://www.englishclub.com/vocabulary/nouns-uncountable-list.htm}{English Club: Uncountable Nouns List}\\par}\n" "\\vspace{0.3cm}\n" "{\\scriptsize\\href{http://archives.nd.edu/whitaker/dictpage.htm}{LATIN-ENGLISH DICTIONARY WORDLIST Version 1.97FC by William Whitaker}\\par}\n" "\\vspace{0.5cm}\n" "\\end{titlepage}\n" "\n" "\\setcounter{tocdepth}{1}\n" "\\renewcommand*\\contentsname{How to make...}\n" "\\tableofcontents\n" "\\newpage"); for r in recipes: r.print_to_doc(doc) try: os.remove("TheGreaterBookOfTransmutation.toc") except Exception as err: traceback.print_tb(err.__traceback__) try: doc.generate_pdf("TheGreaterBookOfTransmutation") except Exception as err: traceback.print_tb(err.__traceback__) try: doc.generate_tex("TheGreaterBookOfTransmutation") except Exception as err: traceback.print_tb(err.__traceback__)