You can also modify the document using the TexSoup tree, then export the changes back to a \(\LaTeX\) file.


As mentioned in Soup, you can change commands and their arguments.

>>> soup = TexSoup(r'I am \textbf{\large Large and bold}')
>>> cmd = soup.textbf
>>> = 'textit'
>>> cmd
\textit{\large Large and bold}

You can set .string for any single-argument command (e.g., \section).

>>> cmd.string = 'corgis are the best'
>>> cmd
\textit{corgis are the best}

You can do the same for any command in math mode.

>>> soup2 = TexSoup(r'$$\textrm{math}\sum$$')
>>> soup2.textrm.string = 'not math'
>>> soup2
$$\textrm{not math}\sum$$

You can also remove any command in-place, by calling .delete on it.

>>> soup2.textrm.delete()
>>> soup2


You can modify arguments just as you would a list.

>>> cmd.args.append('{moar}')
>>> cmd
\textit{corgis are the best}{moar}
>>> cmd.args.remove('{moar}')
>>> cmd
\textit{corgis are the best}
>>> cmd.args.extend(['[moar]', '{crazy}'])
\textit{corgis are the best}[moar]{crazy}
>>> cmd.args = cmd.args[:2]
>>> cmd
\textit{corgis are the best}[moar]

Use the argument’s .string attribute to modify the argument’s contents.

>>> cmd.args[0].string = 'no'
>>> cmd


Use the .string attribute to modify any environment with only text content (i.e., a verbatim or math environment).

>>> soup = TexSoup(r'\begin{verbatim}Huehue\end{verbatim}')
>>> soup.verbatim.string = 'HUEHUE'
>>> soup
>>> soup = TexSoup(r'$$\text{math}$$')
>>> soup.text.string = ''

You can add to an environment’s contents using list-like operations, like .append, .remove, .insert, and .extend.

>>> from TexSoup import TexSoup
>>> soup = TexSoup(r'''
... \begin{itemize}
...     \item Hello
...     \item Bye
... \end{itemize}''')
>>> tmp = soup.item
>>> soup.itemize.remove(soup.item)
>>> soup.itemize
\item Bye
>>> soup.insert(1, tmp)
>>> soup
\item Hello
\item Bye

See for more utilities.