phfaist / pylatexenc

Simple LaTeX parser providing latex-to-unicode and unicode-to-latex conversion
https://pylatexenc.readthedocs.io
MIT License
301 stars 37 forks source link

how to parse `\def\enorm#1{\|#1\|_2}` #83

Open nschloe opened 2 years ago

nschloe commented 2 years ago
\def\enorm#1{\|#1\|_2}

is another one of those weird TeX commands. The above corresponds with

\newcommand\enorm[1]{\|#1\|_2}

Any hint on how to add a parse instruction for it? This

from pylatexenc.latexwalker import LatexWalker, get_default_latex_context_db
from pylatexenc.macrospec import std_macro
from rich import print

string = "\def\enorm#1{\|#1\|_2}"

latex_context = get_default_latex_context_db()
latex_context.add_context_category(
    "myextensions",
    macros=[
        # std_macro("def", "{[{")
        std_macro("def", False, 3),
    ],
)

w = LatexWalker(string, latex_context=latex_context)

nodelist, _, _ = w.get_latex_nodes(pos=0)

print(nodelist)
[
    LatexMacroNode(parsing_state=<parsing state 140069788183760>, pos=0, len=12, 
macroname='def', nodeargd=ParsedMacroArgs(argspec='{{{', 
argnlist=[LatexMacroNode(parsing_state=<parsing state 140069788183760>, pos=4, len=6, 
macroname='enorm', nodeargd=None, macro_post_space=''), LatexCharsNode(parsing_state=<parsing 
state 140069788183760>, pos=10, len=1, chars='#'), LatexCharsNode(parsing_state=<parsing state 
140069788183760>, pos=11, len=1, chars='1')]), macro_post_space=''),
    LatexGroupNode(parsing_state=<parsing state 140069788183760>, pos=12, len=10, 
nodelist=[LatexMacroNode(parsing_state=<parsing state 140069788183760>, pos=13, len=2, 
macroname='|', nodeargd=ParsedMacroArgs(argspec='', argnlist=[]), macro_post_space=''), 
LatexCharsNode(parsing_state=<parsing state 140069788183760>, pos=15, len=2, chars='#1'), 
LatexMacroNode(parsing_state=<parsing state 140069788183760>, pos=17, len=2, macroname='|', 
nodeargd=ParsedMacroArgs(argspec='', argnlist=[]), macro_post_space=''), 
LatexCharsNode(parsing_state=<parsing state 140069788183760>, pos=19, len=2, chars='_2')], 
delimiters=('{', '}'))
]

is not it.

phfaist commented 2 years ago

\def is a command with a complex argument structure. It is not yet supported. You need to write your own argument parser for it. Note that I am reworking how arguments are parsed in the upcoming version pylatexenc 3, and I am hoping to also provide a arguments parser for commands such as \def.