@@ -362,7 +362,7 @@ def _break_up_by_substrings(self) -> Self:
362362 new_submobjects .append (sub_tex_mob )
363363 curr_index = new_index
364364 i += 1
365- elif tex_string . strip (). startswith (( "^" , "_" ) ):
365+ elif self . _is_pure_script ( tex_string ):
366366 # Handle consecutive scripts as a group, matching by Y-position
367367 script_group , j = self ._group_consecutive_scripts (i )
368368 total_script_submobs = self ._total_submobs_for_scripts (script_group )
@@ -376,11 +376,18 @@ def _break_up_by_substrings(self) -> Self:
376376 i = j
377377 else :
378378 # Base element processing: check if followed by scripts
379+ < << << << Updated upstream
379380 next_is_script = i + 1 < len (self .tex_strings ) and self .tex_strings [
380381 i + 1
381382 ].strip ().startswith (("^" , "_" ))
382-
383- if next_is_script and num_submobs > 0 :
383+ == == == =
384+ # But skip if this element already has scripts attached (e.g., \int^b)
385+ has_scripts_already = '^' in tex_string or '_' in tex_string
386+ next_is_script = (i + 1 < len (self .tex_strings ) and
387+ self ._is_pure_script (self .tex_strings [i + 1 ]))
388+ >> >> >> > Stashed changes
389+
390+ if next_is_script and num_submobs > 0 and not has_scripts_already :
384391 script_group , j = self ._group_consecutive_scripts (i + 1 )
385392 total_script_submobs = self ._total_submobs_for_scripts (script_group )
386393 total_needed = num_submobs + total_script_submobs
@@ -422,6 +429,20 @@ def _break_up_by_substrings(self) -> Self:
422429 self .submobjects = new_submobjects
423430 return self
424431
432+ def _is_pure_script (self , tex_string : str ) -> bool :
433+ """Check if a tex_string is a pure script (only ^ or _ with its content).
434+
435+ A pure script should not contain spaces or other content beyond the script itself.
436+ For example: '^n', '_1', '^{abc}' are pure scripts.
437+ But '^b dx' is not a pure script (has additional content).
438+ """
439+ stripped = tex_string .strip ()
440+ if not stripped .startswith (("^" , "_" )):
441+ return False
442+ # Pure scripts shouldn't have spaces (which indicate additional content)
443+ # They should be compact like '^n', '_1', '^{...}', etc.
444+ return ' ' not in stripped
445+
425446 def _group_consecutive_scripts (self , start_index : int ) -> tuple [list [str ], int ]:
426447 """Collect consecutive script tex_strings starting at ``start_index``.
427448
@@ -430,9 +451,13 @@ def _group_consecutive_scripts(self, start_index: int) -> tuple[list[str], int]:
430451 """
431452 script_group = [self .tex_strings [start_index ]]
432453 j = start_index + 1
454+ < << << << Updated upstream
433455 while j < len (self .tex_strings ) and self .tex_strings [j ].strip ().startswith (
434456 ("^" , "_" )
435457 ):
458+ == == == =
459+ while j < len (self .tex_strings ) and self ._is_pure_script (self .tex_strings [j ]):
460+ > >> >> >> Stashed changes
436461 script_group .append (self .tex_strings [j ])
437462 j += 1
438463 return script_group , j
0 commit comments