phfaist / pylatexenc

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

how to instantiate LatexMacroNode? #79

Closed nschloe closed 1 year ago

nschloe commented 2 years ago

I'm trying to change

{\it text}

to

\textit{text}

with pylatexenc. To this end, I need create a LatexMacroNode from a LatexGroupNode. I tried

new_node = LatexMacroNode("textit", nodeargd=groupnode.nodelist[1:])
print()
print("xx", new_node)
print(nodelist_to_latex([new_node]))

but this fails with

AttributeError: 'list' object has no attribute 'argspec'

I'n not sure how to properly instantiate LatexMacroNode

nschloe commented 2 years ago

Okay, I've gotten to

new_node = LatexMacroNode(
    macroname="textit",
    nodeargd=ParsedMacroArgs(
        argspec="{",
        argnlist=node.nodelist[1:],
    ),
)

nodelist_to_latex will print

\textitipsum dolor

so there are {} missing. Not sure if I need to add delimiters or instead of node.nodelist[1:] somehow provide a LatexGroupNode.

nschloe commented 2 years ago

I think I got it:

new_node = LatexMacroNode(
    macroname="textit",
    nodeargd=ParsedMacroArgs(
        argspec="{",
        argnlist=[LatexGroupNode(nodelist=node.nodelist[1:])],
    ),
)
phfaist commented 1 year ago

The function nodelist_to_latex() should not be relied upon. See my comment in #89. I'm hoping to add the ability to regenerate latex code from a node's properties in the future. In the meantime, my sister project latexpp might provide an alternative.