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

replace_with does not work for arguments of a node #119

Closed
WilcoKruijer opened this issue Nov 3, 2020 · 1 comment · Fixed by #141
Closed

replace_with does not work for arguments of a node #119

WilcoKruijer opened this issue Nov 3, 2020 · 1 comment · Fixed by #141

Comments

@WilcoKruijer
Copy link

WilcoKruijer commented Nov 3, 2020

soup = TexSoup(r"\somecommand{\anothercommand}")
some_obj = TexNode(TexText("new text"))
soup.somecommand.anothercommand.replace_with(some_obj)

Gives

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wilco/.cache/pypoetry/virtualenvs/viaduct-CDUJVlHg-py3.8/lib/python3.8/site-packages/TexSoup/data.py", line 573, in replace_with
    self.parent.replace(self, *nodes)
  File "/home/wilco/.cache/pypoetry/virtualenvs/viaduct-CDUJVlHg-py3.8/lib/python3.8/site-packages/TexSoup/data.py", line 597, in replace
    self.expr.remove(child.expr),
  File "/home/wilco/.cache/pypoetry/virtualenvs/viaduct-CDUJVlHg-py3.8/lib/python3.8/site-packages/TexSoup/data.py", line 838, in remove
    self._assert_supports_contents()
  File "/home/wilco/.cache/pypoetry/virtualenvs/viaduct-CDUJVlHg-py3.8/lib/python3.8/site-packages/TexSoup/data.py", line 1084, in _assert_supports_contents
    raise TypeError(
TypeError: Command "somecommand" has no children. `add_contents` is only validfor: 1. environments like `itemize` and 2. `\item`. Alternatively, you can add, edit, or delete arguments by modifying `.args`, which behaves like a list.

The replace_with method tries to replace a child of 'somecommand' but we are dealing with an argument instead. The documentation does not mention this limitation.

In my use case I am parsing all newcommands, then replacing these commands with their values. I.e.

\newcommand{\test}{Just a test}
\stuff{\test}

Should be converted to

\newcommand{\test}{Just a test}
\stuff{Just a test}

In this case I do not know I am operating on an argument of a node instead of a child of this node. Is there a workaround?

Edit: my workaround is the following, but I am not sure what further implications this has (if any).

            try:
                cmd.replace_with(replace)
            except TypeError:
                cmd.name = replace.name
                cmd.args = replace.args
@peterzjx
Copy link

peterzjx commented Nov 9, 2020

I had the same need of replacing inside the argument, and I ended up writing a loop to check for all args.contents and set the values manually. However please be aware of the subtle usage of setter/getter as described in #113

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

Successfully merging a pull request may close this issue.

2 participants