示例#1
0
def make_document(tex, folder):
    # Document with `\maketitle` command activated
    doc = Document()
    doc.append(NoEscape(tex))

    doc.generate_pdf(folder + 'tex_doc', clean_tex=False)
    tex_pdf = readPDF(folder)
    tex = doc.dumps()
    return tex, tex_pdf
示例#2
0
    def dump(self, ass, fh=None):
        fh = super().get_fh(fh)

        doc = Document()

        self.build_preamble(doc, ass)
        self.build_questions(doc, ass)
        self.build_figures(doc, ass)

        make_key = self.make_key
        # if assignment has a make_key entry, use it instead
        if ass.meta.has('make_key'):
            make_key = ass.meta.make_key
        if make_key:
            self.build_key(doc, ass)

        fh.write(doc.dumps())

        return
示例#3
0
def createDocument(filename, documentResults):
    doc = Document('full')
    imageFilename = os.path.join(os.path.dirname(__file__), 'plots/Beni_Anliker.png')
    with doc.create(Section('Leistungstest Feuerwehr Derendingen 2017', numbering=False)):
        with doc.create(Subsection('Persönliche Daten', numbering=False)):
            doc.append('Name ')
            doc.append('\nAlter ')
            doc.append('\nGrösse ')

            doc.append(italic('\nitalic text. '))

        with doc.create(Subsection('Aktuelle Testresultate', numbering=False)):
            doc.append('Standweitsprung: ' + documentResults['slj'])
            doc.append('\nGesamtpunktzahl Leistungstest: ' + documentResults['totalScore']  + ' von 125 Punkten = ' + documentResults['numberToLabel'])

        with doc.create(Subsection('Längsschnittvergleich', numbering=False)):
            with doc.create(Figure(position = 'h!')) as diagram:
                diagram.add_image(imageFilename, width = '350px')
                diagram.add_caption('Beurteilung: 1 = Ungenügend, 2 = Genügend, 3 = Gut, 4 = Sehr gut, 5 = Hervorragend')

    #doc.generate_pdf(clean_tex=False)
    #doc.generate_tex()
    print(doc.dumps())
示例#4
0
    def generate_latex(self):
        doc = Document('ImageLatexResult')
        doc.content_separator = '\n'

        for i in range(len(self.lines)):

            if self.lines[i].type == "large":
                doc.append("\Huge")
            elif self.lines[i].type == "medium":
                doc.append("\LARGE")
            elif self.lines[i].type == "small":
                doc.append("\large")

            if self.lines[i].is_centered:
                doc.append(NoEscape(r'\centerline'))
            elif self.lines[i].is_indented:
                doc.append("\t")

            doc.append(NoEscape(r'{'))
            doc.content_separator = ''
            for k in xrange(0, len(self.lines[i].words)):
                doc.append(self.lines[i].words[k].word)
            doc.content_separator = '\n'
            doc.append(NoEscape(r'}'))
            doc.append(NoEscape("\n"))

            if self.lines[i].is_end_paragraph:
                doc.append(NoEscape(r'\n'))
                doc.append(NoEscape(r'\n'))
                doc.append(NoEscape(r'\n'))

            #if self.lines[i].is_bold:
            #	for k in xrange(0, len(self.lines[i].words)):
            #		doc.append(bold(self.lines[i].words[k].word))
            #	doc.append("\n")

        return doc.dumps()
示例#5
0
def wrap_table_in_latex_document(table: str,
                                 landscape: bool = False,
                                 margin: float = 1.5) -> str:
    """
    Wraps a table inside a proper latex document.

    Uses ``\\longtable`` instead of ``\\tabular`` to fit data on multiple pages.

    Args:
        table: table string to wrap the document around
        landscape: whether to layout the table in landscape mode
        margin: margin around the table in cm

    Returns:
        the resulting latex document as a string
    """
    doc = Document(documentclass="scrbook",
                   document_options="paper=a4",
                   geometry_options={
                       "margin": f"{margin}cm",
                       "landscape": "true" if landscape else "false"
                   })
    # set monospace font
    monospace_comm = UnsafeCommand('renewcommand',
                                   r'\familydefault',
                                   extra_arguments=r'\ttdefault')
    doc.preamble.append(monospace_comm)

    doc.packages.append(Package('longtable'))
    doc.packages.append(Package('booktabs'))

    doc.change_document_style("empty")

    # embed latex table inside document
    doc.append(NoEscape(table))

    return tp.cast(str, doc.dumps())
示例#6
0
                        doc.append(json_load['creationDate'])
                except KeyError:
                    result = 'pas de création date'
                try:
                    with doc.create(Subsection('Date de publication:')):
                        doc.append(json_load['publicationDate'])
                except KeyError:
                    result = 'pas de publication date'
                try:
                    with doc.create(Subsection('keywords:')):
                        doc.append(json_load['keyword'])
                except KeyError:
                    result = 'pas de keywords'

    doc.generate_pdf('/home/francois/basic_maketitle2', clean_tex=False)
tex = doc.dumps()  # The document as string in LaTeX syntax
'''
  print(json_load['defaultTitle'])
    print(json_load['keyword'])

    print(json_load['creationDate'])

    print(json_load['abstract'])

    print(json_load['publicationDate'])
defaultTitle
keyword
creationDate
abstract
publicationDate
'''
示例#7
0
        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")):
        doc.append("Some text.")

    doc.generate_pdf("basic_maketitle2")
    tex = doc.dumps()  # The document as string in LaTeX syntax
示例#8
0
docauthor = portfolio["name"]
metadata = NoEscape(
    Options(
        pdftitle=doctitle,
        pdfauthor=docauthor,
        pdfcreator="PyResume with XeLaTeX",
    ).dumps()[1:-1])

doc.preamble.append(Command('title', doctitle))
doc.preamble.append(Command('author', docauthor))
doc.preamble.append(Command('date', Command('today')))
doc.preamble.append(Command('hypersetup', metadata))
doc.packages.clear()

if prog_args.l:
    print(doc.dumps())

out_dir = path.realpath(prog_args.outdir)
prog_path = path.dirname(path.realpath(__file__))
latex_path = path.join(prog_path, "latex")

# Determine final location of output file
file_name = path.join(
    out_dir, "{} ({})".format(doctitle,
                              datetime.today().strftime("%b, %Y")))
if not path.exists(out_dir):
    os.mkdir(out_dir)
count = 0
next_name = "{} R{}".format(file_name, count)
out_name = next_name
while path.exists(next_name + ".pdf"):
示例#9
0
#TODO: need amsmath package

with doc.create(Section('Questions')):
    #i=0
    for q in data["questions"]:

        qTitle = q['title']
        with doc.create(Subsection(qTitle)):
            content = q['content']

            content = strip(content)

            doc.append(NoEscape(content))
            if q['content_type'] == 'question':
                if q['mode'] == 'multiple' or q['mode'] == 'true':
                    with doc.create(
                            Enumerate(enumeration_symbol=r"\alph*)")) as enum:

                        for a in q['choices']:
                            ans = a['answer']
                            #change mathjax to standard latex
                            #TODO: fix this (pylatex likes to make them "textblackslash" in latex)
                            ans = strip(ans)
                            enum.add_item(ans)
    #hack to make sure amsmath package is declared
    with doc.create(amsmath()):
        doc.append('')

tex = doc.dumps()
doc.generate_tex('./test2Tex_1')
示例#10
0
    def __init__(self):
        doc = Document()
        doc.Command(Package('fullpage', options=Options('cm')))

        print(doc.dumps())
示例#11
0
class PDFRenderer(object):
    def __init__(self, statistics: List, histograms: List, figure_width=8):
        """
        :param statistics: List of tuples (name, value)
        :param histograms: List of tuples (name, list[int])
        """
        self.doc = Document('output')
        self.statistics = statistics
        self.histograms = histograms
        self.figure_size = (figure_width, 3 * len(histograms))
        self.fill_document()

    def set_title(self, title):
        self.doc.preamble.append(Command('title', title))
        self.doc.append(NoEscape(r'\maketitle'))

    def add_author(self, author):
        self.doc.preamble.append(Command('author', author))

    def set_date(self):
        self.doc.preamble.append(Command('date', NoEscape(r'\today')))

    def build_statistics_table(self):
        if not self.statistics:
            return
        self.doc.append(Command('centering'))
        with self.doc.create(Section('DP Statistics')):
            table1 = Tabular('|c|c|')
            table1.add_hline()
            table1.add_row((MultiColumn(2, align='|c|', data='Summary'), ))
            table1.add_hline()
            for stat in self.statistics:
                table1.add_row((stat['statistic'], stat['result']['value']))
                table1.add_hline()
            self.doc.append(table1)

    def build_histograms(self):
        if not self.histograms:
            return
        fig = plt.figure(tight_layout=True, figsize=self.figure_size)
        gs = gridspec.GridSpec(len(self.histograms), 2)
        for i, hist_dict in enumerate(self.histograms):
            ax = fig.add_subplot(gs[i, :])
            ax.bar(x=hist_dict['data'], height=hist_dict['height'])
            ax.set_xlabel(hist_dict['name'])
        with self.doc.create(Section('DP Histograms')):
            with self.doc.create(Figure(position='htbp')) as plot:
                plot.add_plot()
                plot.add_caption('DP Histograms')

    def fill_document(self):
        self.build_statistics_table()
        self.build_histograms()

    def save_pdf(self, filepath):
        self.doc.generate_pdf(filepath=filepath, clean_tex=False)

    def save_latex(self, filepath):
        self.doc.generate_tex(filepath=filepath)

    def get_latex(self):
        return self.doc.dumps()
示例#12
0
def s2string(sbmlArgument, file_path=None):
    ''' Convert sbml to a latex string
    
    Args: 
        param1 (string): file name to sbml OR sbml string
        
        file_path (string, optional): path for creation of a pdf file, only works with latexmk or pdflatex installed
    Returns:
        LaTeX string
    '''
    try:
        import tesbml as libsbml
    except:
        import libsbml

    try:
        from libsbml import formulaToL3String, writeMathMLToString, parseFormula, readMathMLFromString
    except:
        from tesbml import formulaToL3String, writeMathMLToString, parseFormula, readMathMLFromString

    import math
    import pathlib  # For extracting file extensions
    import os

    def getLaTeXFromAST(tree):
        #        xmlstr = writeMathMLToString(tree)
        #        # Strip out the header
        #        xmlstr = xmlstr.replace ('<?xml version="1.0" encoding="UTF-8"?>', '')
        #
        #        return  mathml2latex_yarosh(xmlstr).strip ('$')
        from MATH import convertToInfix

        return convertToInfix(tree)

    #The zeroes are out of nessessity, I don't know why, but just having a single obj variable does not work
    #So, predefined all classes that are used later
    def listfiller(Commands,
                   obj=0,
                   R=0,
                   Sp=0,
                   ass=0,
                   Par=0,
                   tr=0,
                   libsbml=libsbml,
                   tofill=[],
                   twoD=1):
        '''
        Uses a dismal method of evaluating a piece of code 
        from 'Commands' to fit a specific string into 'tofill' 
        takes in a libsbml object as obj
        
        if twoD = 0, then does not fill 'tofill' with the templin as one element
        but returns the compiled templin as 1-D list
        '''
        l = len(Commands)
        templin = [None] * l
        for i in range(l):
            templin[i] = eval(Commands[i])
        if twoD == 1:
            tofill.append(templin)
            return tofill
        elif twoD == 0:
            return templin

    def round_half_up(n, decimals=0):
        '''
        use this to round numbers that are way to big to put in a table
        '''
        multiplier = 10**decimals
        return math.floor(n * multiplier + 0.5) / multiplier

    def lawcutter(prefix):
        '''
        cuts up the string version of the KineticLaw object into something the 
        mathml converter can read
        '''
        lis = prefix.split('\n')
        i = len(lis) - 1
        if ('  <listOfParameters>' in lis):
            i = lis.index('  <listOfParameters>')
        lis = lis[1:i]
        for n in range(0, len(lis)):
            lis[n] = lis[n][
                2:]  #so, here we are messing with indentation, not sure if it will be consistent
            #for all models or even if it is nessessary, but it's here
        newstr = '\n'.join(lis)
        return newstr

    def notecutter(prefix):
        '''
        same as lawcutter but for notes
        
        '''
        prefix = prefix.replace("\n", "")
        lis = prefix.split('>')
        i = len(lis) - 2
        lis = lis[1:i]
        #for n in range(0, len(lis)):
        #   lis[n] =lis[n][1:]
        newstr = '>'.join(lis)
        newstr = newstr + '>'
        return newstr

    # ----------------------------------------------
    # Start of sb2l
    # ----------------------------------------------
    reader = libsbml.SBMLReader()
    # Check if its a file name
    if os.path.isfile(sbmlArgument):
        suff = pathlib.Path(sbmlArgument).suffix
        if suff == '.xml' or suff == '.sbml':
            sbmldoc = reader.readSBMLFromFile(sbmlArgument)
    else:
        # If it's not a file, assume it's an sbml string
        sbmldoc = reader.readSBMLFromString(
            sbmlArgument)  # Reading in the model

    errors = sbmldoc.getNumErrors()
    numReadErr = 0
    numReadWarn = 0
    for i in range(errors):
        severity = sbmldoc.getError(i).getSeverity()
        if (severity == libsbml.LIBSBML_SEV_ERROR) or (
                severity == libsbml.LIBSBML_SEV_FATAL):
            seriousErrors = True
            numReadErr += 1
        else:
            numReadWarn += 1

        oss = libsbml.ostringstream()
        sbmldoc.printErrors(oss)
        errMsgRead = oss.str()
        raise RuntimeError(errMsgRead)

    Model_id = sbmldoc.model.getName(
    )  # This is essentially how each list is filled, using commands from LibSBML
    if len(Model_id) < 1:
        Model_id = sbmldoc.model.getId()
    Model_id = Model_id.replace(r'_', r'\_')

    Compartments = []
    Species = []
    Parameters = []
    Reactions = []
    Events = []
    Rules = []
    FunctionDefinitions = []
    FunctionArgList = []

    # making a notes list
    lis = None
    notes = sbmldoc.model.getNotesString()
    if len(notes) != 0:
        lis = notecutter(notes).split('<')
        lis = lis[2:len(lis)]
    del notes

    l = sbmldoc.model.getNumCompartments()
    if l != 0:
        ComList = [
            'obj.getId()', 'obj.getSBOTerm()', 'obj.getSpatialDimensions()',
            'obj.getSize()', 'obj.getConstant()'
        ]
        for x in range(0, l):
            obj = sbmldoc.model.getCompartment(x)
            Compartments = listfiller(
                ComList, obj=obj,
                tofill=Compartments)  # see the function above
        del (ComList)

    l = sbmldoc.model.getNumSpecies()
    if l != 0:
        SpecList = [
            'obj.getId()', 'obj.getInitialConcentration()',
            'obj.getHasOnlySubstanceUnits()', ' obj.getBoundaryCondition()',
            'obj.getConstant()'
        ]
        for x in range(0, l):
            obj = sbmldoc.model.getSpecies(x)
            Species = listfiller(SpecList, obj=obj, tofill=Species)
            if not math.isnan(Species[x][1]):
                if (Species[x][1] * 1000 <
                        1):  # need this to round things to fit in the table
                    Species[x][1] = round_half_up(Species[x][1], decimals=6)
                else:
                    Species[x][1] = round_half_up(Species[x][1], decimals=4)

        del (SpecList)

    l = sbmldoc.model.getNumParameters()
    if l != 0:
        ParList = ['obj.getId()', 'obj.getValue()', 'obj.getConstant()']
        for x in range(0, l):
            obj = sbmldoc.model.getParameter(x)
            Parameters = listfiller(ParList, obj=obj, tofill=Parameters)
        del (ParList)

    l = sbmldoc.model.getNumReactions()
    if l != 0:
        Rlist = ['R.getId()', 'R.getReversible()', 'R.getFast()']
        ReProlist = [
            'Sp.getSpecies()', 'Sp.getStoichiometry()', 'Sp.getConstant()'
        ]
        Modlist = ['obj.getSpecies()']
        for x in range(0, l):
            R = sbmldoc.model.getReaction(x)
            RL = listfiller(
                Rlist, R=R, twoD=0
            )  #starting the element of common matrix/list to append at the end

            #making the list for reactants
            lRe = R.getNumReactants()
            ReL = []
            for y in range(0, lRe):
                Sp = R.getReactant(y)
                ReL = listfiller(ReProlist, Sp=Sp, tofill=ReL)
            RL.append(ReL)
            del (lRe, ReL)  #Adding reactants list to RL

            #making the list for products
            lPro = R.getNumProducts()
            ProL = []
            for y in range(0, lPro):
                Sp = R.getProduct(y)
                ProL = listfiller(ReProlist, Sp=Sp, tofill=ProL)
            RL.append(ProL)
            del (Sp, ProL, y, lPro)  #Adiing products list to RL

            #making the law thing
            law = R.getKineticLaw()
            prefix = law.toSBML()
            Formula = lawcutter(prefix)
            # repeating the deleted list for now, so that code works consitstnently
            ParList = [
                'Par.getId()', 'Par.getValue()',
                'Par.getDerivedUnitDefinition()', 'Par.getConstant()'
            ]
            lPar = law.getNumParameters()
            ParL = []
            for y in range(0, lPar):
                Par = law.getParameter(y)
                ParL = listfiller(ParList, Par=Par, tofill=ParL)
            KinLaw = [Formula, ParL]
            RL.append(KinLaw)
            del (Formula, law)

            lMod = R.getNumModifiers()
            if lMod > 0:
                ModL = []
                for y in range(0, lMod):
                    obj = R.getModifier(y)
                    ModL = listfiller(Modlist, obj=obj, tofill=ModL)
                RL.append(ModL)

            Reactions.append(
                RL
            )  #Appending all info about a given reaction to the common list
        del (RL, R, Rlist, ReProlist, ParList, lPar, ParL, KinLaw, prefix)

    l = sbmldoc.model.getNumEvents()
    if l != 0:
        TrList = ['tr.getInitialValue()', 'tr.getPersistent()', 'tr.getMath()']
        AsList = ['ass.getId()', 'ass.getMath()']
        for x in range(0, l):
            eve = sbmldoc.model.getEvent(x)  #get the event
            tr = eve.getTrigger()
            TrigL = [0, 0, 0]
            TrigL = listfiller(TrList, tr=tr, tofill=TrigL,
                               twoD=0)  #define trigger things
            m = eve.getNumEventAssignments()
            AssL = []
            for i in range(0, m):
                ass = eve.getEventAssignment(i)
                AssL = listfiller(
                    AsList, ass=ass, tofill=AssL
                )  #add up all of the ID = Formula in a single list
            del (i, m)

            Events.append([eve.getId(), eve.getName(), TrigL, AssL])
        del (TrList, AsList, eve, tr, TrigL, ass, AssL)

    l = sbmldoc.model.getNumRules()
    if l != 0:
        RuList = ['obj.getVariable()', 'obj.getFormula()']
        for x in range(0, l):
            obj = sbmldoc.model.getRule(x)
            Rules = listfiller(RuList, obj=obj, tofill=Rules)
        del (RuList)
        del (obj)

    l1 = sbmldoc.model.getNumFunctionDefinitions()
    if l1 != 0:
        FuncList = ['obj.getId()', 'obj.getBody()']
        for x in range(0, l1):
            obj = sbmldoc.model.getFunctionDefinition(x)
            FunctionDefinitions = listfiller(FuncList,
                                             obj=obj,
                                             tofill=FunctionDefinitions)
            l2 = obj.getNumArguments()
            if l2 != 0:
                for k in range(0, l2):
                    FunctionArgList.append(obj.getArgument(k))

    del (libsbml, lawcutter, l, notecutter, listfiller)

    # The part where everything is compiled into the TeX file

    from pylatex import Document, Section, Subsection, Subsubsection, Command, Math, Tabular, LongTable
    from pylatex import Table, LineBreak
    from pylatex.utils import italic, NoEscape, bold

    doc = Document()  # start a doc

    doc.packages.append(NoEscape(r'\usepackage{xcolor}'))
    doc.packages.append(NoEscape(r'\usepackage{titlesec}'))
    doc.packages.append(NoEscape(r"\usepackage{hyperref}"))
    doc.packages.append(
        NoEscape(r"\hypersetup{colorlinks=true,linkcolor=blue,urlcolor=blue}"))
    doc.packages.append(NoEscape(r"\usepackage{amsmath}"))
    doc.packages.append(NoEscape(r"\usepackage{breqn}"))

    doc.preamble.append(NoEscape(r'\definecolor{blue}{cmyk}{.93, .59, 0, 0}'))
    doc.preamble.append('')
    doc.preamble.append(NoEscape(r'\titleformat{\chapter}[display]'))
    doc.preamble.append(
        NoEscape(r'  {\normalfont\sffamily\huge\bfseries\color{blue}}'))
    doc.preamble.append(
        NoEscape(r'  {\chaptertitlename\ \thechapter}{20pt}{\Huge}'))
    doc.preamble.append(NoEscape(r'\titleformat{\section}'))
    doc.preamble.append(
        NoEscape(r'  {\normalfont\sffamily\Large\bfseries\color{blue}}'))
    doc.preamble.append(NoEscape(r'  {\thesection}{1em}{}'))
    doc.preamble.append(NoEscape(r'\titleformat{\subsection}'))
    doc.preamble.append(
        NoEscape(r'  {\normalfont\sffamily\large\bfseries\color{blue}}'))
    doc.preamble.append(NoEscape(r'  {\thesubsection}{1em}{}'))
    doc.preamble.append(NoEscape(r'\titleformat{\subsubsection}'))
    doc.preamble.append(
        NoEscape(r'  {\normalfont\sffamily\normalsize\bfseries\color{blue}}'))
    doc.preamble.append(NoEscape(r'  {\thesubsubsection}{1em}{}'))

    doc.append(NoEscape(r'\begin{center}'))
    doc.append(
        NoEscape(r'{\normalfont\sffamily\huge\bfseries SBML Model Report}\\'))

    doc.append(NoEscape(r'\vspace{5mm}'))
    doc.append(
        NoEscape(
            r'{\normalfont\sffamily\LARGE\bfseries\color{blue} Model name: ' +
            Model_id + r'}\\'))

    doc.append(NoEscape(r'\vspace{5mm}'))
    doc.append(NoEscape(r'\large\today'))
    doc.append(NoEscape(r'\end{center}'))

    def rxn_eq(Reaction, Command=Command):
        '''
        Stitches up a list to plug into Math function for reaction equations
        
        '''
        numRe = len(
            Reaction[3])  # the products info is stored as a list in position 3
        numPr = len(Reaction[4])
        try:
            numMod = len(Reaction[6])
        except:
            numMod = 0
        arrow = []
        plus = ['+']
        Re = []
        Pr = []

        if numRe > 0:
            for i in range(0, numRe):
                if (i > 0):
                    Re = Re + plus
                Re.append(Command(
                    command='text', arguments=Reaction[3][i]
                    [0]))  #Appends with IDs of species that are reactannts
        else:
            Re.append(Command(command='text', arguments=['None']))

        if numPr > 0:
            for i in range(0,
                           numPr):  # Put in the form Math class can interpret
                if (i > 0):
                    Pr = Pr + plus
                Pr.append(Command(command='text', arguments=Reaction[4][i][0]))
        else:
            Pr.append(Command(command='text', arguments=['None']))

        if numMod > 0:
            arg = []
            for i in range(0, numMod):
                arg.append(Reaction[6][i][0])
            arg = ", ".join(arg)
            arrow = [
                Command(command='xrightarrow',
                        arguments=Command(command='text', arguments=arg))
            ]
        else:
            arrow = [Command('longrightarrow')]

        DaList = Re + arrow + Pr
        return DaList

    if lis != None:
        # NOTES -- made from html string, can recognize:
        # <a href...>, <b>, <i>,<br/> and treats emphasis as italic or bold
        # there is a known issue with special characters such as # not being interpreted right
        # to fix that, follow the structure below
        leng = len(lis)
        with doc.create(Section('Notes')):

            def findOccurrences(s, ch):
                return [i for i, letter in enumerate(s) if letter == ch]

            doc.append(Command('raggedright'))
            doc.append(Command('frenchspacing'))
            for i in range(0, leng):
                if (leng < 2):
                    doc.append(lis[i])
                    continue
                if (
                        '&apos;' in lis[i]
                ):  #THIS if statement is being referenced above, &apos; is the HTML code for
                    #the apostrophe
                    lis[i] = lis[i].replace("&apos;", "'")
                if ('&amp;' in lis[i]):
                    lis[i] = lis[i].replace("&amp;", "&")
                if ('&dollar;' in lis[i]):
                    lis[i] = lis[i].replace("&dollar;", "$")
                if ('&num;' in lis[i]):
                    lis[i] = lis[i].replace("&num;", "#")
                if ('&plus;' in lis[i]):
                    lis[i] = lis[i].replace("&plus;", "+")
                if ('&excl;' in lis[i]):
                    lis[i] = lis[i].replace("&excl;", "!")
                if ('&quest;' in lis[i]):
                    lis[i] = lis[i].replace("&quest;", "?")
                if ('/' in lis[i] and 'br/>' not in lis[i]
                        and '//' not in lis[i]
                        and len(lis[i].replace(" ", "")) < 4
                        and 'strong>' not in lis[i]):
                    continue  #! trying to skip every instance of </something> assuming the 4 length as cutoff

                elif ('br/>' in lis[i] and len(lis[i].replace(" ", "")) < 4):
                    doc.append(LineBreak())
                elif ('br/>' in lis[i]):
                    doc.append(LineBreak())
                    doc.append(lis[i].replace("br/>", ""))
                elif ('p>' in lis[i]):
                    doc.append(Command('par'))
                    doc.append(lis[i][2:len(lis[i])])
                elif ('sub>' in lis[i] and '/sub>' not in lis[i]):
                    temp = lis[i].replace("sub>", "")
                    doc.append(NoEscape("$_{\\text{" + temp + "}}$"))

                elif (('b>' in lis[i] or 'strong>' in lis[i])
                      and ('/b>' not in lis[i]) and ('/strong>' not in lis[i])
                      and ('/sub>' not in lis[i])):
                    temp = lis[i].replace("b>", "")
                    temp = temp.replace("strong>", "")
                    doc.append(bold(temp))

                elif (('i>' in lis[i] or 'em>' in lis[i])
                      and ('/i>' not in lis[i]) and ('/em>' not in lis[i])):
                    temp = lis[i].replace("i>", "")
                    temp = temp.replace("em>", "")
                    doc.append(italic(temp))
                elif (('/b>' in lis[i]) or ('/strong>' in lis[i])
                      or ('/i>' in lis[i]) or ('/em>' in lis[i])
                      or ('/sub>' in lis[i])):
                    temp = lis[i].replace("/i>", "")
                    temp = temp.replace("/em>", "")
                    temp = temp.replace("/b>", "")
                    temp = temp.replace("/strong>", "")
                    temp = temp.replace("/sub>", "")

                    doc.append(temp)
                elif ('a href=' in lis[i]):
                    t_list = lis[i].split('>')
                    pos = findOccurrences(t_list[0], '\"')
                    link = t_list[0][pos[0] + 1:pos[
                        1]]  #! Assuming that the first to places with " \" "
                    #will surround the link
                    doc.append(
                        NoEscape("\href{" + link + "}" + "{" + t_list[1] +
                                 "}"))
                    #! Assuming that in a hyperlink notation:
                    # i. e <a href="http://link.com">text that the author wants to be seen</a>
                else:
                    pos = findOccurrences(lis[i], '>')
                    doc.append(lis[i][pos[0] + 1:])

            del (leng)

    with doc.create(Section('Contents')):
        # Summary of contents of sbml model
        doc.append('The number of components in this model:')
        doc.append(NoEscape(r'\\[2mm]'))

        with doc.create(Table(position='htpb')) as table1:
            doc.append(NoEscape(r'\centering'))
            tbl_cmnd = ''
            tbl_cmnd = 'l|c|l|c'
            with doc.create(Tabular(tbl_cmnd, booktabs=True)) as table:
                table.add_row('Element', 'Quantity', 'Element', 'Quantity')
                table.add_hline()
                table.add_row('Compartment', str(len(Compartments)), 'Species',
                              str(len(Species)))
                table.add_row('Reactions', str(len(Reactions)), 'Events',
                              str(len(Events)))
                table.add_row('Global Parameters', str(len(Parameters)),
                              'Function Definitions',
                              str(len(FunctionDefinitions)))
            table1.add_caption('Components in this model.')

    # COMPARTMENTS TABLE
    listlen = len(Compartments)  #number of rows
    sublistlen = len(Compartments[0])  #number of columns
    tbl_cmnd = ''
    tbl_cmnd = tbl_cmnd.join('c|' for i in range(0, sublistlen))
    tbl_cmnd = tbl_cmnd[:-1]

    with doc.create(Section('Compartments')):
        doc.append('Table of comparments in the model:')
        with doc.create(LongTable(tbl_cmnd, booktabs=True)) as table:
            table.add_row(('ID', 'SBO ', 'Spatial ', 'Size', 'Constant'))
            table.add_row(('', 'Term', 'Dimensions', '', ''))
            table.add_hline()
            for i in range(0, listlen):
                if math.isnan(Compartments[i][1]):
                    Species[i][1] = 'undefined'
                table.add_row(tuple(Compartments[i]))

    # SPECIES TABLE
    # getting info from the list
    listlen = len(Species)  #number of rows
    sublistlen = len(Species[0])  #number of columns
    tbl_cmnd = ''
    #tbl_cmnd.join('X|' for i in range(0, sublistlen))
    tbl_cmnd = tbl_cmnd.join('c|' for i in range(0, sublistlen))
    tbl_cmnd = tbl_cmnd[:-1]  # Remove last character, dont want verical line

    # making a tble for latex
    # As the most simple way of doing this, we can convert the lists into tuples and just paste into
    # the add_row command. For something more complicated: some if statements would be useful
    with doc.create(Section('Species')):
        doc.append('Table of species in the model:')
        with doc.create(LongTable(tbl_cmnd, booktabs=True)) as table:
            table.add_row(('ID', 'Initial ', 'Only ', 'Boundary', 'Constant'))
            table.add_row(
                ('', 'Concentration', 'Substance Units', ' Conditions', ''))
            table.add_hline()
            for i in range(0, listlen):
                if math.isnan(Species[i][1]):
                    Species[i][1] = 'undefined'
                table.add_row(tuple(Species[i]))

    # GLOBAL PARAMETER TABLE
    listlen = len(Parameters)  #number of rows
    if (listlen < 1):
        with doc.create(Section('Parameters')):
            doc.append(
                'The function could not identify any global Parameters in the model'
            )
    else:
        sublistlen = len(Parameters[0])  #number of columns
        tbl_cmnd = ''
        #tbl_cmnd.join('X|' for i in range(0, sublistlen))
        tbl_cmnd = tbl_cmnd.join('c|' for i in range(0, sublistlen))
        tbl_cmnd = tbl_cmnd[:
                            -1]  # Remove last character, dont want verical line

        with doc.create(Section('Parameters')):
            doc.append(
                'The following table is the list of Parameters in the model.')
            with doc.create(LongTable(tbl_cmnd, booktabs=True)) as table:
                table.add_row(('ID', 'Value', 'Constant'))
                table.add_hline()
                for i in range(0, listlen):
                    table.add_row(tuple(Parameters[i]))

    # PROCESS RULES
    listlen = len(Rules)
    if (listlen >= 1):
        with doc.create(Section('Rules')):
            doc.append('Number of rules in the model: ' + str(listlen))
            for i in range(0, listlen):
                with doc.create(
                        Subsection('Rule ' + str(i + 1) + ': ' + Rules[i][0])):
                    doc.append(Math(data=[Rules[i][0] + '=' + Rules[i][1]]))

    # PROCESS FUNCTION DEDFINITIONS
    listlen = len(FunctionDefinitions)
    if (listlen >= 1):
        with doc.create(Section('Function Definitions')):
            doc.append('Number of usr defined functions in the model: ' +
                       str(listlen))
            for i in range(0, listlen):
                latexstr = getLaTeXFromAST(FunctionDefinitions[i][1])

                with doc.create(Subsection('Function ' + str(i + 1))):
                    doc.append(Command("begin", "dmath*"))
                    doc.append(
                        NoEscape(
                            '$$' + '\\text{' +
                            FunctionDefinitions[i][0].replace('_', '\\_') +
                            '}\ ('))
                    for j in range(0, len(FunctionArgList)):

                        latexarg = getLaTeXFromAST(FunctionArgList[j])
                        if j == len(FunctionArgList) - 1:
                            doc.append(
                                NoEscape(str(latexarg.replace('_', '\\_'))))
                        else:
                            doc.append(
                                NoEscape(latexarg.replace('_', '\\_') + ','))
                    doc.append(
                        NoEscape('): ' + latexstr.replace('_', '\\_') + '$$'))
                    doc.append(Command("end", "dmath*"))

    # PROCESS EVENTS
    listlen = len(Events)
    if (listlen >= 1):
        with doc.create(Section('Events')):
            doc.append('Number of events defined in the model: ' +
                       str(listlen))
            for i in range(0, listlen):
                with doc.create(
                        Subsection('Event ' + str(i + 1) + ': ' +
                                   Events[i][0])):
                    if (len(Events[i][1]) > 0):
                        with doc.create(Subsubsection('Name',
                                                      numbering=False)):
                            doc.append(Events[i][1])
                    with doc.create(Subsubsection('Trigger', numbering=False)):
                        doc.append(
                            NoEscape('$$' + getLaTeXFromAST(Events[i][2][2]) +
                                     '$$'))
                    with doc.create(
                            Subsubsection('Assignments', numbering=False)):
                        for j in range(0, len(Events[i][3])):
                            assTree = parseFormula(Events[i][3][j][0])
                            ass = '$$' + getLaTeXFromAST(
                                assTree) + '=' + getLaTeXFromAST(
                                    Events[i][3][j][1]) + '$$'
                            doc.append(NoEscape(ass))

    # PROCESS REACTIONS
    listlen = len(Reactions)  # number of rows

    with doc.create(Section('Reactions')):
        doc.append('Number of reactions in the model: ' + str(listlen))
        for i in range(0, listlen):
            with doc.create(
                    Subsection('Reaction ' + str(i + 1) + ': ' +
                               Reactions[i][0])):

                with doc.create(
                        Subsubsection('Reaction equation', numbering=False)):
                    doc.append(Math(data=rxn_eq(Reaction=Reactions[i])))
                with doc.create(Subsubsection('Kinetic Law', numbering=False)):
                    m = readMathMLFromString(Reactions[i][5][0])
                    formula = getLaTeXFromAST(m)
                    formula = formula.replace('\mathrm', '\ \mathrm')
                    doc.append(Command("begin", "dmath*"))
                    doc.append(
                        NoEscape('$$v =' + formula.replace('_', '\\_') + '$$'))
                    doc.append(Command("end", "dmath*"))
                with doc.create(Subsubsection('Local Parameters')):
                    if len(Reactions[i][5][1]) != 0:
                        sublistlen = len(Reactions[i][5][1][0])
                        tbl_cmnd = ''
                        tbl_cmnd = '||' + tbl_cmnd.join(
                            'c|' for n in range(0, sublistlen)) + '|'
                        with doc.create(LongTable(tbl_cmnd,
                                                  booktabs=False)) as table:
                            table.add_hline()
                            table.add_row(('ID', 'Value', 'Units', 'Constant'))
                            table.add_hline()
                            table.add_hline()
                            listleng = len(Reactions[i][5][1])
                            for j in range(0, listleng):
                                table.add_row(tuple(Reactions[i][5][1][j]))
                                table.add_hline()
                    else:
                        doc.append('No LOCAL Parameters found')

    del (Command, Document, NoEscape, Section, Subsection, italic)
    return doc.dumps()
示例#13
0
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()