Repository: texworld/blacktex (language: python)

Issue:
Spaces sometimes inserted two characters after underscore in label 
A small working example:

```tex
\documentclass{standalone}
\begin{document}
\section{One} \label{one_one}
\section{Two} \label{_two}
\section{Three} \label{three_t}
\subsection{Four} \label{__}
\end{document}
```
blacktex produces the following:

```tex
\documentclass{standalone}
\begin{document}
\section{One}\label{one_o ne}
\section{Two}\label{_t wo}
\section{Three}\label{three_t}
\subsection{Four}\label{__}
\end{document}
```

Notice that the first two labels are changed while the last two are not.

The output of `blacktex -v` is `blacktex 0.2.1, Python 3.8.2`.


Discussion / hints:
Not sure if blacktex will ever deal with this. Underscores are assumed to occur in math mode, and there, `one_o ne`  is actually better (because only the first `o` is put in the subindex). Of course, this should really only happen in _math mode_, but for this, blacktex would have to include an actual parser, which it probably never will.
Underscores are useful in labels and bibliography entry names. As one example, the wonderful package cleveref doesn't accept spaces in references (without significant hacking). This modification of labels makes blacktex unusable for me and many LaTeX users whose code I've seen.

You are correct that it is impossible to detect math mode without parsing. It should be much easier to detect an enclosing command like \label, \cite, \cref, \ref, or \eqref.
PRs welcome!

Fix patch:
diff --git a/blacktex/main.py b/blacktex/main.py
index 8074412..762c326 100644
--- a/blacktex/main.py
+++ b/blacktex/main.py
@@ -79,7 +79,7 @@ def _replace_obsolete_text_mods(string):
 
 
 def _add_space_after_single_subsuperscript(string):
-    string = re.sub("([_\\^])([^{\\\\])([^_\\^\\s\\$})])", r"\1\2 \3", string)
+    string = re.sub("([\\^])([^{\\\\])([^_\\^\\s\\$})])", r"\1\2 \3", string)
     return string
 
 
diff --git a/setup.cfg b/setup.cfg
index 7cb870c..8c36de4 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
 name = blacktex
-version = 0.3.5
+version = 0.3.6
 author = Nico Schlömer
 author_email = nico.schloemer@gmail.com
 description = Cleans up your LaTeX files


Test patch:
diff --git a/test/test_blacktex.py b/test/test_blacktex.py
index 4d36095..a9b1f20 100644
--- a/test/test_blacktex.py
+++ b/test/test_blacktex.py
@@ -114,10 +114,6 @@ def test_subsuperscript_space():
     out = blacktex.clean(input_string)
     assert out == "2^n g"
 
-    input_string = "2_ng"
-    out = blacktex.clean(input_string)
-    assert out == "2_n g"
-
     input_string = "$1/n^3$."
     out = blacktex.clean(input_string)
     assert out == "$1/n^3$."
@@ -136,11 +132,12 @@ def test_subsuperscript_space():
 
     input_string = "a^2_PP^2"
     out = blacktex.clean(input_string)
-    assert out == "a^2_P P^2"
+    assert out == "a^2_PP^2"
 
-    input_string = "a_2^PP_2"
+    # Underscore separation just produces too many false positives. Leave as is.
+    input_string = "2_ng"
     out = blacktex.clean(input_string)
-    assert out == "a_2^P P_2"
+    assert out == "2_ng"
 
 
 def test_triple_dots():