alvinwan / TexSoup

fault-tolerant Python3 package for searching, navigating, and modifying LaTeX documents
https://texsoup.alvinwan.com
BSD 2-Clause "Simplified" License
290 stars 43 forks source link

Control spaces "\ " in math mode don't make the roundtrip #117

Closed ivanistheone closed 1 year ago

ivanistheone commented 4 years ago

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'
Screen Shot 2020-10-18 at 1 10 39 AM

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 :)

masonproffitt commented 3 years ago

This is done outside of math mode as well, generally to get a space after a macro. This is causing the same problems there.

astromancer commented 2 years ago

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}