How to use the pylatexenc.latexwalker.LatexCharsNode function in pylatexenc

To help you get started, we’ve selected a few pylatexenc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github phfaist / pylatexenc / test / test_latexwalker.py View on Github external
chars='\n\nAlso a ',
                               pos=27+1+26+19, len=2+7),
                LatexMacroNode(parsing_state=parsing_state,
                               macroname=r'itshape',
                               macro_post_space='',
                               nodeargd=macrospec.ParsedMacroArgs(argspec='',argnlist=[]),
                               pos=27+1+26+20+1+7, len=15-7),
                LatexCommentNode(parsing_state=parsing_state,
                                 comment=' comment after a macro',
                                 comment_post_space='\n',
                                 pos=27+1+26+20+1+15, len=39-15),
                LatexCommentNode(parsing_state=parsing_state,
                                 comment=' and also a second line',
                                 comment_post_space='\n',
                                 pos=27+1+26+20+1+39, len=25),
                LatexCharsNode(parsing_state=parsing_state,
                               chars='some italic text.',
                               pos=27+1+26+20+1+39+25, len=17),

             ],
             0,len(latextext))
        )
github phfaist / pylatexenc / test / test_latexwalker.py View on Github external
\end{enumerate}
Indeed thanks to \cite[Lemma 3]{Author}, we know that...
'''
        lw = LatexWalker(latextext, tolerant_parsing=False)

        parsing_state = lw.make_parsing_state()

        p = latextext.find(r'\textbf')+len(r'\textbf')
        self.assertEqual(lw.get_latex_maybe_optional_arg(pos=p, parsing_state=parsing_state), None)
        p = latextext.find(r'\cite')+len(r'\cite')
        self.assertEqual(lw.get_latex_maybe_optional_arg(pos=p, parsing_state=parsing_state),
                         (LatexGroupNode(
                             parsing_state=parsing_state,
                             delimiters=('[', ']'),
                             nodelist=[
                                 LatexCharsNode(
                                     parsing_state=parsing_state,
                                     chars='Lemma 3',
                                     pos=p+1,
                                     len=len('Lemma 3'),
                                 )
                             ],
                             pos=p,
                             len=9
                         ),p,9,))
github phfaist / pylatexenc / test / test_latexwalker.py View on Github external
\item Hi there!  % here goes a comment
 \item[a] Hello!  @@@
     \end{enumerate}
Indeed thanks to \cite[Lemma 3]{Author}, we know that...
Also: {\itshape some italic text}.
'''
        lw = LatexWalker(latextext, tolerant_parsing=False)
        parsing_state = lw.make_parsing_state()

        p = latextext.find(r'\begin{enumerate}')
        good_parsed_structure = \
            (LatexEnvironmentNode(
                parsing_state=parsing_state,
                environmentname='enumerate',
                nodelist=[
                    LatexCharsNode(parsing_state=parsing_state,
                                   chars='\n',
                                   pos=p+22, len=1),
                    LatexMacroNode(parsing_state=parsing_state,
                                   macroname='item',
                                   nodeargd=macrospec.ParsedMacroArgs(argspec='[', argnlist=[None]),
                                   macro_post_space=' ',
                                   pos=p+23, len=6),
                    LatexCharsNode(parsing_state=parsing_state,
                                   chars='Hi there!  ',
                                   pos=p+23+6, len=17-6),
                    LatexCommentNode(parsing_state=parsing_state,
                                     comment=' here goes a comment',
                                     comment_post_space='\n ',
                                     pos=p+23+17, len=38-17+2),
                    LatexMacroNode(
                        parsing_state=parsing_state,
github phfaist / pylatexenc / test / test_latexwalker.py View on Github external
LatexMacroNode(parsing_state=parsing_state, pos=20, len=40,
                               macroname='verb',
                               macro_post_space='',
                               nodeargd=macrospec.ParsedVerbatimArgs(
                                   verbatim_chars_node=
                                   LatexCharsNode(parsing_state=parsing_state, pos=26, len=33,
                                                  chars='\\begin{verbatim}...\\end{verbatim}'),
                                   verbatim_delimiters=('+', '+'),
                               )),
                LatexCharsNode(parsing_state=parsing_state, pos=60, len=4, chars=' to\n'),
                LatexEnvironmentNode(
                    parsing_state=parsing_state, pos=64, len=91,
                    environmentname='verbatim', nodelist=[],
                    nodeargd=macrospec.ParsedVerbatimArgs(
                        verbatim_chars_node=
                        LatexCharsNode(
                            parsing_state=parsing_state, pos=80, len=61,
                            chars='\ntypeset \\verbatim text with \\LaTeX $ escapes \\(like this\\).\n'
                        ),
                        verbatim_delimiters=None,
                    )
                ),
                LatexCharsNode(parsing_state=parsing_state, pos=155, len=12,
                               chars='\nThis is it.')
            ],
            0,
            167)
        )
github phfaist / pylatexenc / test / test_latexwalker.py View on Github external
chars='and ',
                               pos=p, len=4),
                LatexMacroNode(parsing_state=parsing_state,
                               macroname='textbf',
                               nodeargd=macrospec.ParsedMacroArgs(argspec='{', argnlist=[
                                   LatexGroupNode(parsing_state=parsing_state,
                                                  delimiters=('{', '}'),
                                                  nodelist=[
                                                      LatexCharsNode(parsing_state=parsing_state,
                                                                     chars='bold text',
                                                                     pos=p+54-42, len=9)
                                                  ],
                                                  pos=p+53-42, len=11)
                               ]),
                               pos=p+46-42, len=64-46),
                LatexCharsNode(parsing_state=parsing_state,
                               chars=' and ',
                               pos=p+64-42, len=69-64),
                LatexMathNode(parsing_state=parsing_state,
                              displaytype='inline',
                              delimiters=('$', '$'),
                              nodelist=[
                                  LatexMacroNode(parsing_state=parsing_state_inner,
                                                 macroname='vec',
                                                 macro_post_space=' ',
                                                 nodeargd=macrospec.ParsedMacroArgs(argspec='{', argnlist=[
                                                     LatexCharsNode(parsing_state=parsing_state_inner,
                                                                    chars='b',
                                                                    pos=p+75-42, len=1)
                                                 ]),
                                                 pos=p+70-42, len=76-70),
                              ],
github phfaist / pylatexenc / test / test_latexwalker.py View on Github external
chars='Indeed thanks to ',
                               pos=0,len=17),
                LatexMacroNode(
                    parsing_state=parsing_state2,
                    macroname='cite',
                    nodeargd=macrospec.ParsedMacroArgs(argspec='{{{{', argnlist=[
                        LatexCharsNode(parsing_state=parsing_state2,
                                       chars='[',
                                       pos=22,len=1),
                        LatexCharsNode(parsing_state=parsing_state2,
                                       chars='L',
                                       pos=23,len=1),
                        LatexCharsNode(parsing_state=parsing_state2,
                                       chars='e',
                                       pos=24,len=1),
                        LatexCharsNode(parsing_state=parsing_state2,
                                       chars='m',
                                       pos=25,len=1),
                    ]),
                    pos=17,len=26-17),
            LatexCharsNode(parsing_state=parsing_state2,
                           chars='ma 3]',
                           pos=26,len=31-26),
            LatexGroupNode(parsing_state=parsing_state2,
                           delimiters=('{', '}'),
                           nodelist=[
                               LatexCharsNode(parsing_state=parsing_state2,
                                              chars='Author',
                                              pos=32,len=38-32),
                           ],
                           pos=31,len=39-31),
            LatexCharsNode(parsing_state=parsing_state2,
github phfaist / pylatexenc / test / test_latexwalker.py View on Github external
p = latextext.find('$$')
        self.assertEqual(
            lw.get_latex_nodes(pos=p+2, stop_upon_closing_mathmode='$$', parsing_state=parsing_state),
            ( [ LatexCharsNode(parsing_state=parsing_state,
                               chars='\n   ax + b = y\n',
                               pos=p+2,len=len('\n   ax + b = y\n')), ] ,
              p+2, len('\n   ax + b = y\n$$') ) # len includes closing token
        )
        # check that first node is math mode node when parsing including the math mode start
        nodes = lw.get_latex_nodes(pos=p, parsing_state=parsing_state)[0]
        parsing_state_inner = nodes[0].nodelist[0].parsing_state # inner "math mode" state
        self.assertEqual(
            nodes[0],
            LatexMathNode(parsing_state=parsing_state,
                          displaytype='display',
                          nodelist=[ LatexCharsNode(parsing_state=parsing_state_inner,
                                                    chars='\n   ax + b = y\n',
                                                    pos=p+2,len=len('\n   ax + b = y\n')), ],
                          delimiters=('$$', '$$'),
                          pos=p,len=len('$$\n   ax + b = y\n$$'))
        )


        p = latextext.find(r'\[')
        good_parsed_structure = lambda parsing_state, ps2, ps3: [
            LatexCharsNode(parsing_state=parsing_state,
                           chars=' cx^2+z=-d',
                           pos=p+2,len=12-2),
            LatexMacroNode(parsing_state=parsing_state,
                           macroname='quad',
                           pos=p+12,len=17-12),
            LatexMacroNode(parsing_state=parsing_state,
github phfaist / bibolamazi / bibolamazi / filters / fixes.py View on Github external
def iterate_over_words_in_nodelist(nodelist):
        # first, split the root-level char nodes at spaces and keep the rest.
        split_nodelist = []
        for n in nodelist:
            if n.isNodeType(latexwalker.LatexCharsNode):
                # split at whitespace
                chunks = re.compile(r'(\s+)').split(n.chars)
                plen = 0
                for chunk, sep in zip(chunks[0::2], chunks[1::2]+['']):
                    split_nodelist += [
                        (latexwalker.LatexCharsNode(parsing_state=n.parsing_state,
                                                    chars=chunk,
                                                    pos=n.pos+plen, len=len(chunk)), sep)
                    ]
                    plen += len(chunk)+len(sep)
            else:
                split_nodelist += [(n, '')]

        # now, combine them smarly into words.
        cur_nodelist = []
        need_protection_hint = False
        for n, sep in split_nodelist:
            logger.longdebug("node to consider for chunk: %r, sep=%r", n, sep)
            if sep: # has separator
                cur_nodelist += [n]
                # flush what we've accumulated so far
                yield cur_nodelist, sep, need_protection_hint
github phfaist / pylatexenc / pylatexenc / macrospec / _argparsers.py View on Github external
if self.verbatim_arg_type == 'verbatim-environment':
            # simply scan the string until we find '\end{verbatim}'.  That's
            # exactly how LaTeX processes it.
            endverbpos = w.s.find(r'\end{verbatim}', pos)
            if endverbpos == -1:
                raise latexwalker.LatexWalkerParseError(
                    s=w.s,
                    pos=pos,
                    msg=r"Cannot find matching \end{verbatim}"
                )
            # do NOT include the "\end{verbatim}", latexwalker will expect to
            # see it:
            len_ = endverbpos-pos

            argd = ParsedVerbatimArgs(
                verbatim_chars_node=w.make_node(latexwalker.LatexCharsNode,
                                                parsing_state=parsing_state,
                                                chars=w.s[pos:pos+len_],
                                                pos=pos,
                                                len=len_)
            )
            return (argd, pos, len_)

        if self.verbatim_arg_type == 'verb-macro':
            # read the next nonwhitespace char. This is the delimiter of the
            # argument
            while w.s[pos].isspace():
                pos += 1
                if pos >= len(w.s):
                    raise latexwalker.LatexWalkerParseError(
                        s=w.s,
                        pos=pos,