def get_test_description(doc, testnumber): doc.append(NewPage()) description = UnsafeCommand( 'input', 'test_description/test_{}.tex'.format(testnumber)) doc.append(description)
# Use our newly created command with different arguments doc.append(ExampleCommand(arguments=Arguments('blue', 'Hello', 'World!'))) doc.append(ExampleCommand(arguments=Arguments('green', 'Hello', 'World!'))) doc.append(ExampleCommand(arguments=Arguments('red', 'Hello', 'World!'))) with doc.create(Section('Custom environments')): doc.append(NoEscape( r""" The following is a demonstration of a custom \LaTeX{} environment using the mdframed package. """)) # Define a style for our box mdf_style_definition = UnsafeCommand('mdfdefinestyle', arguments=['my_style', ('linecolor=#1,' 'linewidth=#2,' 'leftmargin=1cm,' 'leftmargin=1cm')]) # Define the new environment using the style definition above new_env = UnsafeCommand('newenvironment', 'exampleEnvironment', options=2, extra_arguments=[ mdf_style_definition.dumps() + r'\begin{mdframed}[style=my_style]', r'\end{mdframed}']) doc.append(new_env) # Usage of the newly created environment with doc.create( ExampleEnvironment(arguments=Arguments('red', 3))) as environment: environment.append('This is the actual content')
from pylatex.base_classes import CommandBase from pylatex import UnsafeCommand, Package, NoEscape from data_generation.constants import BOX_COLOR_NAME, BOX_TEXT_COLOR_NAME class BoxedEquationCommand(CommandBase): _latex_name = "boxedEquation" packages = [Package("color")] def BoxedEquation(equation): return BoxedEquationCommand(NoEscape(equation)) boxed_equation_code = fr"""\colorbox{{{BOX_COLOR_NAME}}}{{\textcolor{{{BOX_TEXT_COLOR_NAME}}}{{#1}}}}""" BoxedEquationDefinition = UnsafeCommand("newcommand", r"\boxedEquation", options=1, extra_arguments=boxed_equation_code)
def _format_project_subtitle(self, project_item): container = self.MultiCommandContainer() href_escaped = cv_generator.utils.escape_link(project_item.link.href) anchor_escaped = pylatex.utils.escape_latex(project_item.link.anchor) container.append(Command('texttt', UnsafeCommand('href', [href_escaped, anchor_escaped]))) return container
doc.append(Command("settowidth", [Command("spacebox"), "8888888888"])) # \newlength{\spacebox} # \settowidth{\spacebox}{8888888888} ########################### ########################### # # Use our newly created command with different arguments # doc.append(ExampleCommand(arguments=Arguments('blue', 'Hello', 'World!'))) sepspace = UnsafeCommand('newcommand', r'\sepspace', extra_arguments=r""" \vspace*{1em} """) doc.append(sepspace) ########################### ########################### MyName = UnsafeCommand('newcommand', r'\MyName', options=1, extra_arguments=r""" \Huge \usefont{OT1}{phv}{b}{n} \hfill #1 \par \normalsize \normalfont
_latex_name = 'exampleCommand' packages = [Package('color')] # Create a new document doc = Document() with doc.create(Section('Custom commands')): doc.append(NoEscape( r""" The following is a demonstration of a custom \LaTeX{} command with a couple of parameters. """)) # Define the new command new_comm = UnsafeCommand('newcommand', '\exampleCommand', options=3, extra_arguments=r'\color{#1} #2 #3 \color{black}') doc.append(new_comm) # Use our newly created command with different arguments doc.append(ExampleCommand(arguments=Arguments('blue', 'Hello', 'World!'))) doc.append(ExampleCommand(arguments=Arguments('green', 'Hello', 'World!'))) doc.append(ExampleCommand(arguments=Arguments('red', 'Hello', 'World!'))) with doc.create(Section('Custom environments')): doc.append(NoEscape( r""" The following is a demonstration of a custom \LaTeX{} environment using the mdframed package. """)) # Define a style for our box
def tabela_previsao_orcamentaria(doc, enum, previsao_orcamentaria): item(doc, enum, NoEscape(r'PREVISÃO ORÇAMENTÁRIA: \\')) # TODO: blank=True para os DecimalField inscricoes = previsao_orcamentaria.inscricoes or 0 convenios = previsao_orcamentaria.convenios or 0 patrocinios = previsao_orcamentaria.patrocinios or 0 fonte_financiamento = previsao_orcamentaria.fonte_financiamento or 0 honorarios = previsao_orcamentaria.honorarios or 0 passagens = previsao_orcamentaria.passagens or 0 alimentacao = previsao_orcamentaria.alimentacao or 0 hospedagem = previsao_orcamentaria.hospedagem or 0 divulgacao = previsao_orcamentaria.divulgacao or 0 material_consumo = previsao_orcamentaria.material_consumo or 0 xerox = previsao_orcamentaria.xerox or 0 certificados = previsao_orcamentaria.certificados or 0 outros = previsao_orcamentaria.outros or 0 outros_especificacao = previsao_orcamentaria.outros_especificacao or '' # TODO: não estão sendo usadas # fundacao = previsao_orcamentaria.fundacao or '' # outro_orgao_gestor = previsao_orcamentaria.outro_orgao_gestor or '' total_receitas = (inscricoes + convenios + patrocinios + fonte_financiamento) total_despesas = (honorarios + passagens + alimentacao + hospedagem + divulgacao + material_consumo + xerox + certificados + outros) with doc.create(Tabularx('|' + 'X|' * 4, width_argument=WIDTH_ARGUMENT)) as tab: tab.add_hline() tab.add_row(MultiColumn(2, data=bold('Receitas'), align='|c|'), MultiColumn(2, data=bold('Despesas'), align='c|')) tab.add_hline() tab.add_row('Inscrições', inscricoes, 'Honorários', honorarios) tab.add_hline() tab.add_row('Convênios', convenios, 'Passagens', passagens) tab.add_hline() tab.add_row('Patrocínios', patrocinios, 'Alimentação', alimentacao) tab.add_hline() tab.add_row('Fonte(s) de financiamento', fonte_financiamento, 'Hospedagem', hospedagem) tab.add_hline() tab.add_row('', '', 'Divulgação', divulgacao) tab.add_hline() tab.add_row('', '', 'Material de consumo', material_consumo) tab.add_hline() tab.add_row('', '', 'Xerox', xerox) tab.add_hline() tab.add_row('', '', 'Certificados', certificados) tab.add_hline() tab.add_row('', '', 'Outros (especificar)', '{}\n{}'.format(str(outros), outros_especificacao)) tab.add_hline() tab.add_row(bold('Total'), total_receitas, MultiRow(2, data=bold('Total')), MultiRow(2, data=total_despesas)) doc.append(UnsafeCommand('cline', '1-2')) # TODO: não tem atributo para saldo previsto na classe PrevisaoOrcamentaria_CursoExtensao tab.add_row(bold('Saldo previsto'), '', '', '') tab.add_hline()
def set_up(self): """Add packages, set preliminary settings for this doc.""" # Add packages self.preamble.append(NoEscape(r'\usepackage[noprint,1to1]{booklet}')) self.preamble.append(Package('titlesec')) self.preamble.append(Package('pdfpages')) self.preamble.append(Package('makeidx')) self.preamble.append(Package('hyperref')) self.preamble.append(Package('graphicx')) self.preamble.append(Package('afterpage')) self.preamble.append(NoEscape(r'\graphicspath{ {resources/} }')) self.preamble.append(NoEscape(r'\source{\magstep0}{5.5in}{8.5in}')) self.preamble.append(NoEscape(r'\target{\magstep0}{11in}{8.5in}')) self.preamble.append(NoEscape(r'\setpdftargetpages')) self.preamble.append( NoEscape(r'\setulmarginsandblock{1.65cm}{1.65cm}{*}')) self.preamble.append(NoEscape(r'\setlrmarginsandblock{1cm}{1cm}{*}')) self.preamble.append(NoEscape(r'\checkandfixthelayout')) ### Custom commands # Use sections so that we can easily generate a TOC... but make them # invisible to keep things pretty. invis_section = UnsafeCommand( 'newcommand', '\invisiblesection', options=1, extra_arguments=r'\refstepcounter{section}' r'\sectionmark{#1}' r'\addcontentsline{toc}{section}{#1}') self.append(invis_section) add_song = UnsafeCommand( 'newcommand', '\song', options=2, extra_arguments=r'\invisiblesection{#1}' r'\includepdf[pages={1-},pagecommand=\thispagestyle{plain}]{#2}') self.append(add_song) blank_page = UnsafeCommand('newcommand', '\\blankpage', extra_arguments=r'\clearpage' r'\begingroup' r'\null' r'\thispagestyle{empty}' r'\addtocounter{page}{-1}' r'\hypersetup{pageanchor=false}' r'\clearpage' r'\endgroup') self.append(blank_page) # So we can change margins for a single page at a time self.preamble.append( NoEscape(r''' \newenvironment{changemargin}[2]{% \begin{list}{}{% \setlength{\topsep}{0pt}% \setlength{\leftmargin}{#1}% \setlength{\rightmargin}{#2}% \setlength{\listparindent}{\parindent}% \setlength{\itemindent}{\parindent}% \setlength{\parsep}{\parskip}% }% \item[]}{\end{list}} ''')) # Ignore chapter numbering in order for section numbering to be reasonable self.preamble.append( NoEscape(r'\renewcommand{\thesection}{\arabic{section}}')) # Title Info self.preamble.append(Command('title', 'Christmas Carols')) self.preamble.append(Command('author', 'compiled by Maia McCormick')) self.preamble.append(Command('date', NoEscape(r'\today'))) # Ignore page numbers until we get to the actual body self.append(NoEscape(r'\pagenumbering{gobble}')) # Silly cover image (will be in 'resources/coverImg.xxx') coverImg = r""" \begin{figure}[h] \vspace{3.0cm} \includegraphics[width=\textwidth]{coverImg} \centering \end{figure} """ self.append(NoEscape(r'\begin{changemargin}{1.5cm}{1.5cm}')) self.append(NoEscape(r'\maketitle')) self.append(NoEscape(coverImg)) self.append(NoEscape(r'\clearpage')) self.append(NoEscape(r'\tableofcontents')) self.append(NoEscape(r'\end{changemargin}')) self.append(NoEscape(r'\clearpage')) self.append(NoEscape( r'\blankpage')) # need this so the 2pg carols are on facing pages # Okay, show page numbers again self.append(NoEscape(r'\pagenumbering{arabic}'))
def processLaTex(): titlePage = r''' \renewcommand{\maketitle}{% \begin{titlepage} % Suppresses headers and footers on the title page \centering % Centre everything on the title page \scshape % Use small caps for all text on the title page \vspace*{\baselineskip} % White space at the top of the page %------------------------------------------------ % Title %------------------------------------------------ \rule{\textwidth}{1.6pt}\vspace*{-\baselineskip}\vspace*{2pt} % Thick horizontal rule \rule{\textwidth}{0.4pt} % Thin horizontal rule \vspace{0.75\baselineskip} % Whitespace above the title {\LARGE THE DICTIONARY \\ OF COLLECTED WORDS\\} % Title \vspace{0.75\baselineskip} % Whitespace below the title \rule{\textwidth}{0.4pt}\vspace*{-\baselineskip}\vspace{3.2pt} % Thin horizontal rule \rule{\textwidth}{1.6pt} % Thick horizontal rule \vspace{2\baselineskip} % Whitespace after the title block %------------------------------------------------ % Subtitle %------------------------------------------------ A Number of Fascinating and Fundamental Words Presented in a Dictionary Way % Subtitle or further description \vspace*{3\baselineskip} % Whitespace under the subtitle \vspace*{3\baselineskip} % Whitespace under the subtitle \vspace*{3\baselineskip} % Whitespace under the subtitle %------------------------------------------------ % Editor(s) %------------------------------------------------ Edited By \vspace{0.5\baselineskip} % Whitespace before the editors {\scshape\Large Arafat Hasan \\} % Editor list \vspace{0.5\baselineskip} % Whitespace below the editor list \textit{Mawlana Bhashani Science and Technology University \\ Tangail, Bangladesh} % Editor affiliation \vfill % Whitespace between editor names and publisher logo %------------------------------------------------ % Publisher %------------------------------------------------ %\plogo % Publisher logo %\vspace{0.3\baselineskip} % Whitespace under the publisher logo \the\year % Publication year %{\large publisher} % Publisher \end{titlepage} } ''' pdfMeta = r''' \usepackage[xetex, pdfauthor={Arafat Hasan}, pdftitle={The Dictionary of Collected Words}, pdfsubject={A Personal Dictionary}, pdfkeywords={Personal Dictionary}, pdfproducer={XeLaTeX on Ubuntu}, pdfcreator={XeLaTeX}]{hyperref} ''' geometry_options = {"top":"2.3cm","bottom":"2.0cm", "left":"2.5cm",\ "right":"2.0cm", "columnsep":"27pt"} doc = Document('TheDictionaryOfCollectedWords', geometry_options=geometry_options) doc.documentclass = Command( 'documentclass', options=['10pt', 'a4paper', 'twoside'], arguments=['book'], ) doc.packages.append(Package('palatino')) doc.packages.append(Package('microtype')) doc.packages.append(Package('multicol')) doc.packages.append(Package('fontspec')) doc.packages.append(Package('enumitem')) doc.packages.append(Package('graphicx')) doc.packages.append(Package('PTSerif')) doc.packages.append(Package('titlesec', NoEscape('bf, sf, center'))) doc.packages.append(Package('fancyhdr')) doc.preamble.append(Command('usepackage', \ NoEscape(r'xcolor'), 'svgnames')) doc.preamble.append(NoEscape(pdfMeta)) doc.preamble.append(NoEscape('%')) doc.preamble.append(NoEscape('%')) doc.preamble.append(Command('setmainfont', \ 'TeX Gyre Pagella', 'Numbers=OldStyle')) doc.preamble.append(Command('fancyhead', \ NoEscape(r'\textsf{\rightmark}'), 'L')) doc.preamble.append(Command('fancyhead', \ NoEscape(r'\textsf{\leftmark}'), 'R')) doc.preamble.append(NoEscape('%')) doc.preamble.append(NoEscape('%')) doc.preamble.append(Command('renewcommand', \ arguments=Arguments(NoEscape(r'\headrulewidth'), '1.4pt'))) doc.preamble.append(Command('fancyfoot', \ NoEscape(r'\textbf{\textsf{\thepage}}'), 'C')) doc.preamble.append(Command('renewcommand', \ arguments=Arguments(NoEscape(r'\footrulewidth'), '1.4pt'))) doc.preamble.append(Command('pagestyle', 'fancy')) doc.append(NoEscape(r'\setlength{\parindent}{-0.7em}')) new_comm = UnsafeCommand('newcommand', '\entry', options=7, \ extra_arguments=NoEscape(r'\textbf{#1}\markboth{#1}{#1}\ {{\fontspec{Doulos SIL} #2}}\ {{\fontspec{Kalpurush} \small {#3}}}\ {#4}\ {#5}\ {\textit{#6}}\ {#7}')) doc.preamble.append(new_comm) color_bullet = UnsafeCommand('newcommand', '\colorBulletS', options=1, \ extra_arguments=NoEscape(r'\colorbox[RGB]{171,171,171}{\makebox(11,2){\textcolor{white}{{\tiny \textbf{#1}}}}}')) doc.preamble.append(color_bullet) color_bullet = UnsafeCommand('newcommand', '\colorBullet', options=1, \ extra_arguments=NoEscape(r'\colorbox[RGB]{171,171,171}{\makebox(22, 1){\textcolor{white}{{\tiny \textbf{#1}}}}}')) doc.preamble.append(color_bullet) doc.preamble.append( NoEscape( r'\newcommand{\plogo}{\fbox{$\mathcal{PL}$}} % Generic dummy publisher logo' )) doc.preamble.append(NoEscape('%')) doc.preamble.append(NoEscape('%')) doc.preamble.append(NoEscape(titlePage)) doc.append(NoEscape(r'\maketitle')) entriesList = list(fileData.keys()) entriesList.sort() currentSection = "a" sectionStr = "\chapter*{" + currentSection.upper() + "}" doc.append(NoEscape(sectionStr)) doc.append(NoEscape(r'\begin{multicols}{2}')) for item in entriesList: entryList = fileData[item] for entry in entryList: word = entry.get('word', "") bengali = entry.get('bengali', "") prep = entry.get('prep', "") ownExample = entry.get('example', "") origin = entry.get('origin', "") phonetic = entry.get('phonetic', "") meaning = entry.get('meaning', {}) word = escape_latex(word) bengali = escape_latex(bengali) prep = escape_latex(prep) ownExample = escape_latex(ownExample) origin = escape_latex(origin) phonetic = escape_latex(phonetic) if len(prep): prep = " \\colorBullet{OTHER} " + prep if len(origin): origin = " \\colorBullet{ORIGIN} " + origin partsOfSpeech = list(meaning.keys()) if len(partsOfSpeech) == 1: partStr = "" else: partStr = "\\small{\\textsf{\\textit{" + escape_latex( ", ".join(partsOfSpeech)) + "}}}" for part in partsOfSpeech: escapePart = escape_latex(str(part)) onepart = meaning[part] deffCnt = 0 if len(partsOfSpeech) == 1: strGen = "\\textsf{\\textit{" + escapePart + "}}\\" else: strGen = "\\\\{\\fontspec{DejaVu Sans}▪ }\\textsf{\\textit{" + escapePart + "}}\\\\" for deff in onepart: deffCnt = deffCnt + 1 definition = deff.get('definition', "") example = deff.get('example', "") synonyms = deff.get('synonyms', {}) definition = escape_latex(definition) example = escape_latex(example) synonyms = escape_latex(", ".join(synonyms)) if len(synonyms): synonyms = " \\colorBulletS{SYN} " + synonyms strGen = strGen + " \\textbf{" + str( deffCnt ) + "} " + definition + " {\\fontspec{DejaVu Sans}◇} " + "\\textit{" + example + "}" + synonyms partStr = partStr + " " + strGen # lorem = "\entry{"+word+"}{"+phonetic+"}{"+bengali+"}{"+partStr+"}" + "{" + prep +"}" + "{" + ownExample + "}" + "{" + origin + "}" // With origin lorem = "\entry{" + word + "}{" + phonetic + "}{" + bengali + "}{" + partStr + "}" + "{" + prep + "}" + "{" + ownExample + "}" if item[0] is not currentSection[0]: currentSection = item[0] sectionStr = "\section*{" + currentSection.upper() + "}" doc.append(NoEscape(r'\end{multicols}')) doc.append(NoEscape(r'\pagebreak')) doc.append(NoEscape(sectionStr)) doc.append(NoEscape(r'\begin{multicols}{2}')) doc.append(NoEscape(lorem)) doc.append(NoEscape(r'\par')) doc.append(NoEscape(r'\end{multicols}')) doc.generate_pdf(clean_tex=False, compiler='xelatex') doc.generate_tex() tex = doc.dumps()