TexSoup
TexSoup copied to clipboard
Control spaces "\ " in math mode don't make the roundtrip
People often use \ (called control space) to insert a little extra space in math mode—to avoid the default behaviour of ignoring math spaces.
In TexSoup these get eaten up somehow, so when serialize back out, the tex is different:
Failing test case:
def test_contol_space_command():
"""A control space "\ " can be used to insert extra spacing in math mode."""
math_with_spaces = r"""$a \ = \ \sin\theta$"""
soup = TexSoup(math_with_spaces)
assert str(soup) == math_with_spaces, 'Control spaces not preserved in math'
expected: $a \ = \ \sin\theta$
observed: $a \=\\sin\theta$
Here are the tokens in case that's helpful:
'$'_MathSwitch
'a '_Text
'\'_Escape
' = '_Text
'\'_Escape
' '_MergedSpacer
'\'_Escape
'sin'_CommandName
'\'_Escape
'theta'_CommandName
'$'_MathSwitch
See https://tex.stackexchange.com/a/74354 for a complete list of all special LaTeX spacing commands inside and outide of math mode.
No urgency to fix this --- perhaps it is even a feature, since prevents users from trying to override default LaTeX spacing behaviour :)
This is done outside of math mode as well, generally to get a space after a macro. This is causing the same problems there.
Here's another example of where space gets munged: Notice the space after \toprule
TexSoup(r'''
\begin{document}
\begin{tabular}{ll}
\toprule
{\(a\)} & \\
\bottomrule
\end{tabular}%
\end{document}
''')
\begin{document}
\begin{tabular}{ll}
\toprule{\(a\)} & \\
\bottomrule \bot
\end{tabular}%
\end{document}