def format_header(self): if len(self.cv.basic.name) + len(self.cv.basic.surnames) > 0: with self.doc.create(pylatex.MiniPage(width='0.7\\textwidth', pos='c')): self.doc.append(Command('alternativeheadername', self.cv.basic.name + ' ' + self.cv.basic.surnames)) # self.format_skills() self.doc.append(pylatex.HFill()) with self.doc.create(pylatex.MiniPage(width='0.3\\textwidth', pos='c')): # self.doc.append(Command('mobi', self.cv.basic.disponibilite_geographique)) # self.doc.append(pylatex.NewLine()) if len(self.cv.contact.email) > 0: self.doc.append(Command('icon', ['At', 12, self.cv.contact.email])) if len(self.cv.contact.phone) > 0: self.doc.append(pylatex.NewLine()) self.doc.append(Command('icon', ['Phone', 12, self.cv.contact.phone])) if len(self.cv.basic.residence) > 0: self.doc.append(pylatex.NewLine()) self.doc.append(Command('icon', ['MapMarker', 12, str(self.cv.basic.residence)])) if len(self.cv.basic.disponibilite_geographique) > 0: self.doc.append(pylatex.NewLine()) self.doc.append(Command('mobi', ["Mobilité", self.cv.basic.disponibilite_geographique])) if len(self.cv.basic.biography) > 0: self.doc.append(pylatex.NewLine()) self.doc.append(Command('cvsect', ('Présentation'))) self.doc.append(self.cv.basic.biography) self.doc.append(pylatex.NewLine())
def add_page3(self): doc = self.m_doc doc.append("Influence of mesocrystal height") doc.append(pl.VerticalSpace("2cm")) doc.append("\n") with doc.create( pl.MiniPage(width=r"0.2\textwidth", height=r"0.25\textwidth", content_pos='t')): lines = data.split("\n") with doc.create(pl.Tabular('l l', row_height=0.8)) as table: myfont = [mono, tiny] for l in lines: parts = l.split(":") print(parts) if len(parts) == 2: table.add_row(parts[0], parts[1], mapper=myfont) elif len(parts) == 1: table.add_hline() table.add_row((l, " "), mapper=myfont) doc.append("\n") with doc.create( pl.MiniPage(width=r"0.8\textwidth", height=r"0.25\textwidth", content_pos='t')): doc.append( pl.Command('includegraphics', options='scale=0.8', arguments='meso.png')) doc.append("\n") doc.append(pl.NewPage())
def _format_hobbies(self): if self.cv.basic.hobbies: with self.doc.create( pylatex.MiniPage(width='0.5\\textwidth', pos='t')): self.doc.append(Command('cvsect', _('DEVELOPER_HOBBIES_TITLE'))) self.doc.append(pylatex.NewLine()) self.doc.append(self.cv.basic.hobbies)
def _format_languages(self): with self.doc.create(pylatex.MiniPage(width='0.5\\textwidth', pos='t')): self.doc.append(Command('cvsect', _('DEVELOPER_LANGUAGES_TITLE'))) self.doc.append(pylatex.NewLine()) for language_item in self.cv.languages: self.doc.append( pylatex.NoEscape('\\textbf{{{}}} - {}'.format( language_item.name, language_item.level))) self.doc.append(pylatex.NewLine())
def render_table_cell_latex(cell, c, top_space, btm_space, widths): cell = cell.walk(links_to_table_notes) cell_wrapper = lc.Span() content = utils.panflute2output(cell.content, format="latex") style = "" if cell.heading == 1: style = "\\sffamily " elif cell.heading == 2: style = "\\sffamily\\small " content = style + content cell_width = widths[c] if cell.col_span > 1: cell_width = sum(widths[c:c + cell.col_span]) minipage = pl.MiniPage( width="{}\\columnwidth".format(cell_width), pos="t", align="left", content_pos="t", ) minipage.append(pl.Command("setstretch", arguments="0.6")) if cell.vertical: minipage.append(lc.RotateBox(pl.NoEscape(content))) else: minipage.append(pl.NoEscape(content)) cell_wrapper.append(pl.Command("noindent")) cell_wrapper.append(minipage) if c == 0: if top_space: cell_wrapper.append(pl.Command("T")) if btm_space: cell_wrapper.append(pl.Command("B")) if cell.col_span > 1: margins = "@{}" if cell.rm_horizontal_margins else "" multicolumn = pl.MultiColumn(cell.col_span, align=pl.NoEscape(f"{margins}l{margins}"), data=cell_wrapper) return multicolumn elif not cell.covered: return cell_wrapper
def format_languages(self): if len(self.cv.languages) > 0: self.vertical_division_coefficient = 0.95 / ( (len(self.cv.languages) > 0) + (len(self.cv.certifications) > 0) + ( len(self.cv.informatique) > 0)) with self.doc.create( pylatex.MiniPage(width='{}\\textwidth'.format(self.vertical_division_coefficient), pos='t')): self.doc.append(Command('cvsect', ('Langues'))) self.doc.append(pylatex.NewLine()) for certification_item in self.cv.languages: self.doc.append( pylatex.NoEscape('\\textbf{{{}}}'.format(certification_item.name))) self.doc.append(pylatex.NewLine())
def _export_pdf(self, fname: str): geometry_options = {"margin": "1in"} doc = pylatex.Document(geometry_options=geometry_options) header = pylatex.PageStyle("header") with doc.create(pylatex.MiniPage(align="c")): doc.append( pylatex.HugeText(pylatex.utils.bold("Practice Sessions"))) doc.append(pylatex.LineBreak()) with header.create(pylatex.Foot("L")): header.append("@2019 Musicavis") with header.create(pylatex.Foot("R")): header.append(pylatex.simple_page_number()) doc.preamble.append(header) doc.change_document_style("header") for practice in self.practices: with doc.create( pylatex.Section( f"{practice.instrument.name.title()} ({practice.date:%a, %B %m %Y})" )): self._add_list_itemize_latex(doc, "Goals", practice.goals.all()) with doc.create(pylatex.Subsection("Exercises")): with doc.create(pylatex.Itemize()) as itemize: for x in practice.exercises.all(): itemize.add_item( f"{x.name} at {x.bpm_start}-{x.bpm_end}bpm for {x.minutes}m" ) self._add_list_itemize_latex(doc, "Improvements", practice.improvements.all()) self._add_list_itemize_latex(doc, "Positives", practice.positives.all()) with doc.create(pylatex.Subsection("Notes:")): if practice.notes: for note in practice.notes.split("\r\n"): doc.append(note) if note else doc.append( pylatex.NewLine()) doc.generate_pdf(fname.split(".pdf")[0], clean=True)
def df_to_pdf(df, out_file, print_index=True, debug=False, digit_round=1, caption=None, comma_separated_columns=[], gen_latex='False'): """ convert data frame to pdf/latex. used to create tables for paper/thesis. """ if digit_round is not None: df = df.round(digit_round) for column in comma_separated_columns: df[column] = df[column].map('{:,}'.format) table_columns = len(df.columns) + 1 if print_index else len(df.columns) if gen_latex: with open(table_path + out_file + '.tex', 'w') as f: f.write( df.to_latex(escape=False, index=print_index, column_format='c' * table_columns)) return doc = pl.Document(documentclass='standalone', document_options='varwidth') doc.packages.append(pl.Package('booktabs')) with doc.create(pl.MiniPage()): with doc.create(pl.Table(position='htbp')) as table: table.append(pl.Command('centering')) table.append( pl.NoEscape( df.to_latex(escape=False, index=print_index, column_format='c' * table_columns))) if caption is not None: table.add_caption(caption) if debug: return doc.dumps() doc.generate_pdf(output_path + out_file)
def __init__(self, path): # Declare data bank as empty dict self.mBank = {} # Set geometry options geometry_options = \ { "landscape": True, "heightrounded": True, "a4paper": True, "headheight": "10mm", "tmargin": "15mm", "bottom": "10mm", "right": "15mm", "left": "15mm" } # Create document self.mDoc = pyl.Document(geometry_options=geometry_options, default_filepath=path + "/report") # Declare packages # self.mDoc.packages.append(pyl.Package("lscape", options="pdftex")) self.mDoc.packages.append(pyl.Package("float")) self.mDoc.add_color("LightGray", "rgb", "0.83, 0.83, 0.83") self.mDoc.add_color("DarkGray", "rgb", "0.66, 0.66, 0.66") # Create header header = pyl.PageStyle("header") # Create left header with header.create(pyl.Head("L")) as leftHeader: with leftHeader.create( pyl.MiniPage(width=pyl.NoEscape(r"0.7\textwidth"), pos="c")) as logoWrapper: logoWrapper.append( pyl.StandAloneGraphic(image_options="width=120px", filename="../Images/logo.png")) # Create right header with header.create(pyl.Head("R")): header.append("Electronics Workshop") # Append header to document self.mDoc.preamble.append(header) self.mDoc.change_document_style("header") return
def render_facts(): """ Render facts to a latex document """ import pylatex from pylatex.base_classes.command import Options # NOQA import pyqrcode fact_data = load_facts() class MDFramed(pylatex.base_classes.Environment): _latex_name = 'mdframed' packages = [pylatex.Package('mdframed')] class SamePage(pylatex.base_classes.Environment): _latex_name = 'samepage' class ComposeContexts: def __init__(self, *contexts): self.contexts = contexts def __enter__(self): return [c.__enter__() for c in self.contexts] def __exit__(self, a, b, c): return [c.__exit__(a, b, c) for c in self.contexts[::-1]] # class NewUnicodeChar(pylatex.base_classes.CommandBase): # pass # Dont use fontenc, lmodern, or textcomp # https://tex.stackexchange.com/questions/179778/xelatex-under-ubuntu doc = pylatex.Document('fact_document', inputenc=None, page_numbers=False, indent=False, fontenc=None, lmodern=False, textcomp=False) doc.preamble.append(pylatex.Package('graphicx')) # For PNG images # doc.preamble.append(pylatex.Package('svg', options=dict(inkscapearea='page'))) # doc.preamble.append(pylatex.Command('title', 'Facts')) # doc.preamble.append(pylatex.Command('author', 'Anonymous author')) # doc.preamble.append(pylatex.Command('date', pylatex.NoEscape(r'\today'))) # doc.append(pylatex.NoEscape(r'\maketitle')) # doc.preamble.append(pylatex.Package('newunicodechar')) # doc.preamble.append(pylatex.NoEscape(r'\newunicodechar{±}{$\pm$}')) # doc.append(pylatex.NoEscape('13.787±0.020')) # print(doc.dumps()) # doc.generate_pdf(clean_tex=False, compiler='xelatex') # return QR_REFERENCE = True stop_flag = 0 image_dpath = ub.Path('~/misc/facts/images').expand().ensuredir() # image_dpath = for fact in ub.ProgIter(fact_data['facts']): contexts = ComposeContexts( # doc.create(SamePage()), doc.create(MDFramed()), doc.create(pylatex.MiniPage(width=r'0.99\textwidth'))) # with doc.create(pylatex.MiniPage(width=r'\textwidth')): with contexts: doc.append(pylatex.NoEscape(r'\paragraph{Fact:}')) text = ub.paragraph(fact['text']) if r'\[' in text: found = list( re.finditer( '(' + re.escape(r'\[') + '|' + re.escape(r'\]') + ')', text)) prev_x = 0 for a, b in ub.iter_window(found, step=2): part = text[prev_x:a.span()[0]] doc.append(part) ax, bx = a.span()[1], b.span()[0] part = pylatex.NoEscape(r'$' + text[ax:bx] + r'$ ') doc.append(part) prev_x = b.span()[1] part = text[prev_x:] doc.append(part) else: # if '$' in text: # parts = text.split('$') # for idx, p in enumerate(parts): # if idx % 2 == 1: # doc.append(pylatex.NoEscape('$' + p + '$ ')) # else: # doc.append(p) # else: doc.append(text) if QR_REFERENCE: doc.append('\n') num_refs = 0 for refline in fact['references'].split('\n'): if refline.startswith('http'): found = refline image_fname = ub.hash_data(found, base='abc')[0:16] + '.png' image_fpath = image_dpath / image_fname if not image_fpath.exists(): # pyqrcode.create(found).svg(fpath, scale=6) pyqrcode.create(found).png(str(image_fpath), scale=2) doc.append( pylatex.NoEscape(r'\includegraphics[width=90px]{' + str(image_fpath) + '}')) # doc.append(pylatex.NoEscape(r'\includesvg[width=120px]{' + fpath + '}')) num_refs += 1 if num_refs > 3: break else: doc.append(pylatex.NoEscape(r'\paragraph{References:}')) with doc.create(pylatex.Itemize()) as itemize: for refline in fact['references'].split('\n'): if refline: refline = refline.strip() itemize.add_item(refline) doc.append(pylatex.NoEscape(r'\bigskip')) if stop_flag: break print(doc.dumps()) print('generate pdf') doc.generate_pdf(str(ub.Path('~/misc/facts/fact_document').expand()), clean_tex=True)
def _format_header(self): self.doc.append( Command('headername', self.cv.basic.name + ' ' + self.cv.basic.surnames)) self.doc.append( pylatex.base_classes.Arguments( pylatex.NoEscape('\\\\ \\huge' + ' ' + self.cv.basic.profession))) self.doc.append(pylatex.NewLine()) with self.doc.create(pylatex.MiniPage(width='0.60\\textwidth', pos='c')): self.doc.append(Command('cvsect', _('DEVELOPER_BIOGRAPHY_TITLE'))) self.doc.append(pylatex.NewLine()) self.doc.append(self.cv.basic.biography) self._format_skills() self.doc.append(pylatex.HFill()) with self.doc.create(pylatex.MiniPage(width='0.30\\textwidth', pos='c')): self.doc.append( Command('icon', ['MapMarker', 12, self.cv.basic.residence])) self.doc.append(pylatex.NewLine()) self.doc.append( Command('icon', ['Phone', 12, self.cv.contact.phone])) self.doc.append(pylatex.NewLine()) self.doc.append(Command('icon', ['At', 12, self.cv.contact.email])) if self.cv.contact.personal_website or self.cv.contact.twitter or self.cv.contact.linkedin or \ self.cv.contact.github: self.doc.append(pylatex.NewLine()) self.doc.append(pylatex.VerticalSpace('0.25cm')) self.doc.append(pylatex.NewLine()) if self.cv.contact.personal_website: self.doc.append( Command('icon', [ 'Globe', 12, Command('href', [ self.cv.contact.personal_website.href, self.cv.contact.personal_website.anchor ]) ])) self.doc.append(pylatex.NewLine()) if self.cv.contact.twitter: self.doc.append( Command('icon', [ 'Twitter', 12, Command('href', [ self.cv.contact.twitter.href, self.cv.contact.twitter.anchor ]) ])) self.doc.append(pylatex.NewLine()) if self.cv.contact.linkedin: self.doc.append( Command('icon', [ 'Linkedin', 12, Command('href', [ self.cv.contact.linkedin.href, self.cv.contact.linkedin.anchor ]) ])) self.doc.append(pylatex.NewLine()) if self.cv.contact.github: self.doc.append( Command('icon', [ 'Github', 12, Command('href', [ self.cv.contact.github.href, self.cv.contact.github.anchor ]) ])) self.doc.append(pylatex.VerticalSpace('0.50cm')) self.doc.append(pylatex.NewLine())