pylatexenc icon indicating copy to clipboard operation
pylatexenc copied to clipboard

how to instantiate LatexMacroNode?

Open nschloe opened this issue 3 years ago • 2 comments

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 avatar Feb 15 '22 17:02 nschloe

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 avatar Feb 15 '22 18:02 nschloe

I think I got it:

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

nschloe avatar Feb 15 '22 18:02 nschloe

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.

phfaist avatar Sep 17 '22 09:09 phfaist