def __init__(self, title: Optional[str] = None, authors: Optional[Union[str, Sequence[str]]] = None, date: Optional[str] = None, short_title: Optional[str] = None, subtitle: Optional[str] = None, short_author: Optional[str] = None, institutions: Optional[Sequence[Sequence[str]]] = None, short_institution: Optional[str] = None, **kwargs): pre_env_contents = [ Title(title, short_title=short_title) if title is not None else None, Subtitle(subtitle) if subtitle is not None else None, Author(authors, institutions=institutions, short_institution=short_institution, short_author=short_author) if authors is not None else None, Date(date) if date is not None else Date() ] self.pre_env_content = [ content for content in pre_env_contents if content is not None ] self.add_data_from_content(self.pre_env_content) from pyexlatex.logic.builder import _build pre_env_contents = _build(self.pre_env_content) super().__init__(MakeFrameTitle(), label='title-frame', pre_env_contents=pre_env_contents, **kwargs)
def __init__(self, content, title: Optional[str] = None, text_color: Optional[str] = None, header_color: Optional[str] = None, **kwargs): self.content = content self.title = title title_str = title if title is not None else '' title_modifier_str = f'{{{title_str}}}' pre_env_contents = None post_env_contents = None if text_color is not None or header_color is not None: from pyexlatex.logic.builder import _build color_options = [] if text_color is not None: color_options.append(f'fg={text_color}') if header_color is not None: color_options.append(f'bg={header_color}') set_color = SetBeamerColor('block title', color_options) pre_env_contents = _build(['{', set_color]) post_env_contents = '}' super().__init__(self.name, self.content, env_modifiers=title_modifier_str, pre_env_contents=pre_env_contents, post_env_contents=post_env_contents, **kwargs)
def add_prepend_items_dict_to_latex_str(prepend_items_dict: PrependItemsDict, latex_str: str) -> str: from pyexlatex.logic.builder import _build if not prepend_items_dict: return latex_str prepend_items = [str(item) for item in prepend_items_dict.values()] return _build(prepend_items + [latex_str])
def __str__(self): from pyexlatex.logic.builder import _build if isinstance(self.content, str): return self.content elif isinstance(self.content, (list, tuple)): return _build(self.content) else: return str(self.content)
def _include_references_file(self, references: Optional[ Sequence[BibTexEntryBase]] = None): from pyexlatex.logic.builder import _build all_references = _build(references) references_inline_file = FileContents(all_references, 'refs.bib') self.data.end_document_items.append(references_inline_file) self.data.packages.append(Package('filecontents'))
def _validate_contents(self): contents_exception = ContentParseException( f'not able to parse attribute .contents in {self.__class__.__name__}. ' f'Ensure that .contents is set to a string, a pyexlatex item, or ' f'an iterable. Got {self.contents} of type {type(self.contents)}.') if isinstance(self.contents, str): return elif hasattr(self.contents, 'is_LatexItem') and self.contents.is_LatexItem: return elif isinstance(self.contents, dict): raise contents_exception else: try: from pyexlatex.logic.builder import _build _build(self.contents) except TypeError as e: if 'can only join an iterable' in e: raise contents_exception
def __str__(self): if isinstance(self.contents, str): return self.contents elif hasattr(self.contents, 'is_LatexItem') and self.contents.is_LatexItem: # Another pyexlatex item is the root contents, allow it to determine str output return str(self.contents) else: # An iterable of pyexlatex items from pyexlatex.logic.builder import _build return _build(self.contents)
def __str__(self): from pyexlatex.logic.builder import _build, build contents = deepcopy(self.contents) contents = build(contents) possible_items = [ self.pre_env_contents, self.env.wrap(str(contents)), self.post_env_contents ] items = [item for item in possible_items if item] return _build(items)
def bibtex_str(item_type: str, item_accessor: str, fields: Dict[str, str]) -> str: from pyexlatex.logic.builder import _build begin_str = f'@{item_type}{{{item_accessor},' field_strs = [ ' ' + _bibtex_field_str(key, value) for key, value in fields.items() ] end_str = '}' full_str = _build([begin_str, *field_strs, end_str]) return full_str
def __str__(self): """ \mode must be on its own line with no whitespace in some cases, so must have a custom __str__ method """ from pyexlatex.logic.builder import _build from pyexlatex.logic.format.contents import format_contents mode_definition = no_options_no_contents_str(self.name) # \mode mode_type_str = f'<{self.mode_type}>' contents_str = f'{{{format_contents(self.contents)}}}' mode_contents = f'{mode_type_str}{contents_str}' return _build([ mode_definition, mode_contents ])
def handle_output_loop_cycles_exception(latex_str: str, callback: Callable, cycles_num: Optional[int] = None, **callback_kwargs): from pyexlatex.logic.builder import _build if cycles_num is None: cycles = DEFAULT_EXTRA_CYCLES else: cycles = cycles_num * CYCLES_INCREASE_FACTOR if cycles > EXTRA_CYCLES_LIMIT: raise OutputLoopConsecutiveDeadCycles(f'tried increasing max consecutive dead cycles, ' f'but hit limit of {EXTRA_CYCLES_LIMIT} cycles') max_cycles = MaxDeadCycles(cycles) modified_str = _build([max_cycles, latex_str]) warnings.warn(f'could not create pdf due to too many consecutive dead cycles. trying again with {cycles} cycles') return callback(modified_str, cycles_num=cycles, **callback_kwargs)
self.align = align.lower() self.contents = self._get_positioned_header(contents) super().__init__(self.contents.name, self.contents) def _get_positioned_header(self, contents): if self.align == 'right': return RightHeader(contents) elif self.align == 'left': return LeftHeader(contents) elif self.align == 'center': return CenterHeader(contents) else: raise ValueError( 'must pass one of "right", "left", "center" for align') def __str__(self): return str(self.contents) class FancyHeader(SimpleItem): name = 'fancyhead' def __init__(self, contents): super().__init__(self.name, contents) remove_header_line = r'\renewcommand{\headrulewidth}{0pt}' remove_header = _build([ remove_header_line, FancyHeader('') # remove header default contents ])
def _build(self, contents: PyexlatexItems) -> PyexlatexItem: if isinstance(contents, (list, tuple)): contents = [self._build(c) for c in contents] return _build(contents) contents = cast(PyexlatexItem, contents) return contents
def __str__(self) -> str: from pyexlatex.logic.builder import _build if isinstance(self.contents, str): return self.contents return _build(self.contents)
def __init__( self, content: PyexlatexItems, title: str, author: str, major: str, chair: str, abstract: PyexlatexItems, bibliography: pl.Bibliography, dedication_contents: PyexlatexItems = 'I dedicate this to...', acknowledgements_contents: PyexlatexItems = 'I would like to thank...', biographical_contents: PyexlatexItems = 'Nick made this Pyexlatex template from the LaTeX template, then got a Ph.D.', abbreviation_contents: Optional[PyexlatexItems] = None, appendix_contents: Optional[Sequence[Chapter]] = None, multiple_appendices: bool = False, edit_mode: bool = False, bibliography_style: str = 'amsplain', co_chair: Optional[str] = None, thesis_type: ThesisTypes = ThesisTypes.DISSERTATION, degree_type: str = 'Doctor of Philosophy', degree_year: int = datetime.datetime.today().year, degree_month: str = 'May', has_tables: bool = False, has_figures: bool = False, has_objects: bool = False, tables_relative_font_size: int = 0, figures_relative_font_size: int = 0, natbib_options: Optional[str] = 'numbers', packages: Optional[List[Union[Package, str]]] = None, pre_env_contents: Optional[PyexlatexItems] = None, pre_output_func: Optional[Callable] = None): self.natbib_options = natbib_options self.init_data() # These package imports are already handled in the cls file. To avoid conflicts # when using items which also include these packages, add these to packages first self.data.packages.append( pl.Package('hyperref', modifier_str='linktoc=all')) self.data.packages.append( pl.Package('natbib', modifier_str=natbib_options)) # Necessary to get self.data.references which sets self.has_references in DocumentBase # which enables bibtex self.data.references.extend(bibliography.references) register_doc_type(self.natbib_options) if edit_mode: self.document_class_obj = DocumentClass( document_type='uf-thesis-dissertation', options=['editMode'], ) else: self.document_class_obj = DocumentClass( document_type='uf-thesis-dissertation', ) register_doc_type() from pyexlatex.models.item import ItemBase if pre_env_contents is None: pre_env_contents_list = [] elif isinstance(pre_env_contents, (ItemBase, str)): pre_env_contents_list = [pre_env_contents] else: pre_env_contents_list = pre_env_contents # type: ignore if has_tables: pre_env_contents_list.append(pl.Raw(r'\haveTablestrue')) if has_figures: pre_env_contents_list.append(pl.Raw(r'\haveFigurestrue')) if has_objects: pre_env_contents_list.append(pl.Raw(r'\haveObjectstrue')) self.temp_tex_contents: Dict[str, PyexlatexItems] = dict( dedicationFile=dedication_contents, acknowledgementsFile=acknowledgements_contents, abstractFile=abstract, referenceFile=_build(bibliography.references), biographyFile=biographical_contents, ) set_ref_file = r'\setReferenceFile{referenceFile}{' + bibliography_style + '}%' pre_env_contents_list.extend([ Sloppy(), Title(title), im.DegreeType(degree_type), im.Major(major), Author(author, short_author=None), im.ThesisType(thesis_type), im.DegreeYear(degree_year), im.DegreeMonth(degree_month), im.Chair(chair, co_chair), pl.Raw(r""" \setDedicationFile{dedicationFile}% Dedication Page \setAcknowledgementsFile{acknowledgementsFile}% Acknowledgements Page \setAbstractFile{abstractFile}% Abstract Page (This should only include the abstract itself) \setBiographicalFile{biographyFile}% Biography file of the Author (you). """), pl.Raw(set_ref_file), ]) if multiple_appendices: pre_env_contents_list.append( pl.Raw(r""" \multipleAppendixtrue% Uncomment this if you have more than one appendix, % comment it if you have only one appendix. """)) if abbreviation_contents is not None: pre_env_contents_list.append( pl.Raw(r""" \setAbbreviationsFile{abbreviations}% Abbreviations Page """)) self.temp_tex_contents.update(abbreviations=abbreviation_contents) if appendix_contents is not None: pre_env_contents_list.append( pl.Raw(r""" \setAppendixFile{appendix}% Appendix Content; hyperlinking might be weird. """)) self.temp_tex_contents.update(appendix=appendix_contents) for temp_content in self.temp_tex_contents.values(): self.add_data_from_content(temp_content) floatrow_options = 'capposition=top,captionskip=0pt' pre_env_contents_list.extend( get_table_figure_size_packages( tables_relative_font_size, figures_relative_font_size, floatrow_table_options=floatrow_options, floatrow_figure_options=floatrow_options, )) super().__init__( content, packages=packages, pre_env_contents=pre_env_contents_list, pre_output_func=pre_output_func, )
def wrap(self, other): from pyexlatex.logic.builder import _build return _build([self._begin, other, self._end])
from pyexlatex.models.item import SimpleItem, NoOptionsNoContentsItem from pyexlatex.models.page.footer import RightFooter, CenterFooter from pyexlatex.logic.builder import _build class PageReference(SimpleItem): name = 'pageref' def __init__(self, contents): super().__init__(self.name, contents) class ThisPageNumber(NoOptionsNoContentsItem): """ Gets a reference to the current page number, can be included in headers and more. """ name = 'thepage' def __init__(self): super().__init__(self.name) last_page = PageReference('LastPage') this_page = r'\thepage\ ' # TODO: use ThisPageNumber page_reference_str = f'Page {this_page} of {last_page}' right_aligned_page_numbers = _build([ RightFooter(page_reference_str), # add page number to bottom right CenterFooter('') # cancel original page number at bottom center ])
def __str__(self): from pyexlatex.logic.builder import _build package_import = super().__str__() return _build([package_import, NewColumnTypes()])
def __str__(self): from pyexlatex.logic.builder import _build bibliography_str = _basic_item_str('bibliography', 'refs') output = _build([self.style, bibliography_str]) return output
def __str__(self) -> str: from pyexlatex.logic.builder import _build inst = Inst(self.num) inst_output = TableLineBreak().join(self.institution_lines) return _build([inst, inst_output])