(self, s: str, loc: int,
toks: ParseResults | dict[str, str])
| 2392 | return [self._make_space(toks["space"])] |
| 2393 | |
| 2394 | def symbol(self, s: str, loc: int, |
| 2395 | toks: ParseResults | dict[str, str]) -> T.Any: |
| 2396 | c = toks["sym"] |
| 2397 | if c == "-": |
| 2398 | # "U+2212 minus sign is the preferred representation of the unary |
| 2399 | # and binary minus sign rather than the ASCII-derived U+002D |
| 2400 | # hyphen-minus, because minus sign is unambiguous and because it |
| 2401 | # is rendered with a more desirable length, usually longer than a |
| 2402 | # hyphen." (https://www.unicode.org/reports/tr25/) |
| 2403 | c = "\N{MINUS SIGN}" |
| 2404 | try: |
| 2405 | char = Char(c, self.get_state()) |
| 2406 | except ValueError as err: |
| 2407 | raise ParseFatalException(s, loc, |
| 2408 | "Unknown symbol: %s" % c) from err |
| 2409 | |
| 2410 | if c in self._spaced_symbols: |
| 2411 | # iterate until we find previous character, needed for cases |
| 2412 | # such as $=-2$, ${ -2}$, $ -2$, or $ -2$. |
| 2413 | prev_char = next((c for c in s[:loc][::-1] if c != ' '), '') |
| 2414 | # Binary operators at start of string should not be spaced |
| 2415 | # Also, operators in sub- or superscripts should not be spaced |
| 2416 | if (self._needs_space_after_subsuper or ( |
| 2417 | c in self._binary_operators and ( |
| 2418 | len(s[:loc].split()) == 0 or prev_char in { |
| 2419 | '{', *self._left_delims, *self._relation_symbols}))): |
| 2420 | return [char] |
| 2421 | else: |
| 2422 | return [Hlist([self._make_space(0.2), |
| 2423 | char, |
| 2424 | self._make_space(0.2)], |
| 2425 | do_kern=True)] |
| 2426 | elif c in self._punctuation_symbols: |
| 2427 | prev_char = next((c for c in s[:loc][::-1] if c != ' '), '') |
| 2428 | next_char = next((c for c in s[loc + 1:] if c != ' '), '') |
| 2429 | |
| 2430 | # Do not space commas between brackets |
| 2431 | if c == ',': |
| 2432 | if prev_char == '{' and next_char == '}': |
| 2433 | return [char] |
| 2434 | |
| 2435 | # Do not space dots as decimal separators |
| 2436 | if c == '.' and prev_char.isdigit() and next_char.isdigit(): |
| 2437 | return [char] |
| 2438 | else: |
| 2439 | return [Hlist([char, self._make_space(0.2)], do_kern=True)] |
| 2440 | return [char] |
| 2441 | |
| 2442 | def unknown_symbol(self, s: str, loc: int, toks: ParseResults) -> T.Any: |
| 2443 | raise ParseFatalException(s, loc, f"Unknown symbol: {toks['name']}") |
no test coverage detected