def build(self): tex = [build(self.head) + '{' + ''.join(self.alignment) + '}'] if self.top_rule: tex.append(r'\toprule') tex_array = np.full_like(self.data, '', dtype=object) for i, row in enumerate(self.data): for j, content in enumerate(row): content = self._format_number(i, j, content) content = build(content) content = self._apply_commands(i, j, content) tex_array[i, j] = content tex_array_format = np.array([[' & '] * (self.shape[1] - 1) + [r'\\']] * self.shape[0]) self._apply_multicells(tex_array, tex_array_format) for i, (row, row_format) in enumerate(zip(tex_array, tex_array_format)): tex.append(''.join( build(item, self) for pair in zip(row, row_format) for item in pair)) if i in self.rules: for rule in self.rules[i]: tex.append(build(rule)) if self.bottom_rule: tex.append(r'\bottomrule') tex += [self.tail] return self._build_list(tex)
def build(self, save_to_disk=True, compile_to_pdf=True, show_pdf=True): """ Builds the document to a tex file and optionally compiles it into tex and show the output pdf in the default pdf reader of the system. Args: save_to_disk (bool): If True, the built tex will be save to disk automatically. Else, one can recover the tex string from the return of the current method. compile_to_pdf (bool): If True, automatically call pdflatex to compile the generated tex file to pdf. show_pdf (bool): If True, the default pdf reader will be called to show the compiled pdf. This may not work well with non-read-only pdf viewer such as Acrobat Reader or Foxit Reader. Returns: The tex string of the file. """ tex = super().build() tex = build(self.doc_class) + '\n' + self.build_preamble() + '\n' + tex if save_to_disk: self.file.save(tex) if compile_to_pdf: self.file.save(tex) self.file.compile_to_pdf() if show_pdf: open_file_with_default_program(self.filename, self.filepath) return tex
def render(self, compile_to_pdf=True, show_pdf=True, build_from_dir='source'): """ Loads the input files, parses the tex to find the anchors, inserts the code generated by python2latex then saves it to disk. Args: compile_to_pdf (bool): If True, automatically call pdflatex to compile the generated tex file to pdf. show_pdf (bool): If True, the default pdf reader will be called to show the compiled pdf. This may not work well with non-read-only pdf viewer such as Acrobat Reader or Foxit Reader. Suggested readers are SumatraPDF on Windows and Okular or Evince on Linux. build_from_dir (str, either 'source' or 'cwd'): Directory to build from. With the 'source' option, pdflatex will be called from the directory containing the TeX file, like this: ~/some/path/to/tex_file> pdflatex './filename.tex' With the 'cwd' option, pdflatex will be called from the current working directory, like this: ~/some/path/to/cwd> pdflatex 'filepath/filename.tex' This can be important if you include content in the TeX file, such as with the command \input{<path_to_some_file>}, where 'path_to_some_file' should be relative to the directory where pdflatex is called. """ tex = self._load_tex_file() preamble, doc = self._split_preamble(tex) self._insert_tex_at_anchors(doc) self._update_preamble(preamble) tex = '\n'.join(build(line) for line in preamble + doc) self.output_file.save(tex) if compile_to_pdf: self.output_file.compile_to_pdf(build_from_dir=build_from_dir) if show_pdf: open_file_with_default_program(self.output_file.filename, self.output_file.filepath)
def _build_list(self, list_to_build): """ Builds a list of objects to build for a TeX string representation. Return a TeX string representation of the list. """ tex = [build(part, self) for part in list_to_build] return '\n'.join([part for part in tex if part])
def build(self): self.tabular.head.parameters += (''.join(self.alignment), ) if self.top_rule: self.tabular.body.append(r"\toprule") self._format_cells() self._apply_highlights() table_format = self._generate_table_format() for i, (row, row_format) in enumerate(zip(self.data, table_format)): self.tabular.body.append(''.join( str(build(item, self)) for pair in zip(row, row_format) for item in pair)) if i in self.rules: for rule in self.rules[i]: self.tabular.body.append(build(rule)) if self.bottom_rule: self.tabular.append(r'\bottomrule') return super().build()
def build(self, save_to_disk=True, compile_to_pdf=True, show_pdf=True, delete_files=list(), build_from_dir='cwd'): r""" Builds the document to a tex file and optionally compiles it into tex and show the output pdf in the default pdf reader of the system. Args: save_to_disk (bool): If True, the built tex will be save to disk automatically. Else, one can recover the tex string from the return of the current method. compile_to_pdf (bool): If True, automatically call pdflatex to compile the generated tex file to pdf. Only used if 'save_to_disk' is True. show_pdf (bool): If True, the default pdf reader will be called to show the compiled pdf. This may not work well with non-read-only pdf viewer such as Acrobat Reader or Foxit Reader. Only used if 'save_to_disk' and 'compile_to_pdf' are True. delete_files (Union[str, Iterable[str]]): Extensions of the files to delete after compilation. By default no files saved on disk are deleted. Valid extensions are 'tex', 'aux', 'log' and 'pdf'. 'all' is also accepted and will delete everything except the pdf. build_from_dir (str, either 'source' or 'cwd'): Directory to build from. With the 'source' option, pdflatex will be called from the directory containing the TeX file, like this: ~/some/path/to/tex_file> pdflatex './filename.tex' With the 'cwd' option, pdflatex will be called from the current working directory, like this: ~/some/path/to/cwd> pdflatex 'filepath/filename.tex' This can be important if you include content in the TeX file, such as with the command \input{<path_to_some_file>}, where 'path_to_some_file' should be relative to the directory where pdflatex is called. Returns: The tex string of the file. """ tex = super().build() tex = build(self.doc_class) + '\n' + self.build_preamble() + '\n' + tex if save_to_disk: self.file.save(tex) if compile_to_pdf: self.file.compile_to_pdf(build_from_dir=build_from_dir) if show_pdf: open_file_with_default_program(self.filename, self.filepath) if isinstance(delete_files, str): if delete_files == 'all': delete_files = ['tex', 'aux', 'log'] else: delete_files = [delete_files] for ext in delete_files: if ext in ['tex', 'aux', 'log', 'pdf']: os.remove(f'{self.filepath}/{self.filename}.{ext}') return tex
def build(self): """ Builds recursively the environments of the body and converts it to .tex. Returns the .tex string of the file. """ tex = [self.head] if self.label_pos == 'top': tex.append(self._label) tex.extend(self.body) if self.label_pos == 'bottom': tex.append(self._label) tex.append(self.tail) tex = [build(part, self) for part in tex] return '\n'.join([part for part in tex if part])
def build(self, save_to_disk=True, compile_to_pdf=True, show_pdf=True, delete_files=list()): """ Builds the document to a tex file and optionally compiles it into tex and show the output pdf in the default pdf reader of the system. Args: save_to_disk (bool): If True, the built tex will be save to disk automatically. Else, one can recover the tex string from the return of the current method. compile_to_pdf (bool): If True, automatically call pdflatex to compile the generated tex file to pdf. Only used if 'save_to_disk' is True. show_pdf (bool): If True, the default pdf reader will be called to show the compiled pdf. This may not work well with non-read-only pdf viewer such as Acrobat Reader or Foxit Reader. Only used if 'save_to_disk' and 'compile_to_pdf' are True. delete_files (Union[str, Iterable[str]]): Extensions of the files to delete after compilation. By default no files saved on disk are deleted. Valid extensions are 'tex', 'aux', 'log' and 'pdf'. 'all' is also accepted and will delete everything except the pdf. Returns: The tex string of the file. """ tex = super().build() tex = build(self.doc_class) + '\n' + self.build_preamble() + '\n' + tex if save_to_disk: self.file.save(tex) if compile_to_pdf: self.file.compile_to_pdf() if show_pdf: open_file_with_default_program(self.filename, self.filepath) if isinstance(delete_files, str): if delete_files == 'all': delete_files = ['tex', 'aux', 'log'] else: delete_files = [delete_files] for ext in delete_files: if ext in ['tex', 'aux', 'log', 'pdf']: os.remove(f'{self.filepath}/{self.filename}.{ext}') return tex
def render(self, compile_to_pdf=True, show_pdf=True): """ Loads the input files, parses the tex to find the anchors, inserts the code generated by python2latex then saves it to disk. Args: compile_to_pdf (bool): If True, automatically call pdflatex to compile the generated tex file to pdf. show_pdf (bool): If True, the default pdf reader will be called to show the compiled pdf. This may not work well with non-read-only pdf viewer such as Acrobat Reader or Foxit Reader. Suggested readers are SumatraPDF on Windows and Okular or Evince on Linux. """ tex = self._load_tex_file() preamble, doc = self._split_preamble(tex) self._insert_tex_at_anchors(doc) self._update_preamble(preamble) tex = '\n'.join(build(line) for line in preamble + doc) self.output_file.save(tex) if compile_to_pdf: self.output_file.compile_to_pdf() if show_pdf: open_file_with_default_program(self.output_file.filename, self.output_file.filepath)
def _apply_commands(self, i, j, content): command_list = self.commands[i, j] for command in command_list: content = build(command(content), self) return content