Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nschloe opened this issue Feb 18, 2022 · 1 comment
Open

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

nschloe opened this issue Feb 18, 2022 · 1 comment

Comments

@nschloe
Copy link

nschloe commented Feb 18, 2022

\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
Copy link
Owner

phfaist commented Sep 17, 2022

\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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants