import re from typing import Iterator from latex2mathml import commands from latex2mathml.symbols_parser import convert_symbol UNITS = ("in", "mm", "cm", "pt", "em", "ex", "pc", "bp", "dd", "cc", "sp", "mu") PATTERN = re.compile( rf""" (?P%[^\n]+) | (?P[a-zA-Z]) | (?P[_^])(?P\d) | (?P-?\d+(?:\.\d+)?\s*(?:{"|".join(UNITS)})) | (?P\d+(?:\.\d+)?) | (?P\.\d*) | (?P\\[\\\[\]{{}}\s!,:>;|_%#$&]) | (?P\\(?:begin|end)\s*{{[a-zA-Z]+\*?}}) | (?P\\operatorname(?:withlimits|\*)?\s*{{[a-zA-Z\s*]+\*?\s*}}) | (?P\\(?:cla(?:p|ss)|color(?!box)|emph|fbox|hbox|href|llap|mbox|rlap|style |tag\*?|text(?:bf|color|it|md|normal|rm|sf|tt|up)?|underbar))\s*{{(?P[^}}]*)}} | (?P\\[cdt]?frac)\s*(?P[.\d])\s*(?P[.\d])? | (?P\\math(?!ring|bin|close|inner|op|open|ord|punct|rel|strut) [a-z]+)(?P{{)(?P[a-zA-Z])(?P}}) | (?P\\verb(?P.)(?P.*?)(?P=verb_delim)) | (?P\\[a-zA-Z]+) | (?P\S) """, re.VERBOSE, ) def tokenize(latex_string: str, skip_comments: bool = True) -> Iterator[str]: """ Converts Latex string into tokens. :param latex_string: Latex string. :param skip_comments: Flag to skip comments (default=True). """ for match in PATTERN.finditer(latex_string): tokens = tuple(filter(lambda x: x is not None, match.groups())) if tokens[0].startswith(commands.VERB): yield commands.VERB yield tokens[2] # verb_content continue if tokens[0].startswith(commands.MATH) and tokens[0] not in commands.MATH_NON_FONT_COMMANDS: full_math = "".join(tokens) symbol = convert_symbol(full_math) if symbol: yield f"&#x{symbol};" continue for captured in tokens: if skip_comments and captured.startswith("%"): break if captured.endswith(UNITS) and captured[0:1].isdigit(): yield captured.replace(" ", "") continue if captured.startswith((commands.BEGIN, commands.END, commands.OPERATORNAME)): yield "".join(captured.split(" ")) continue yield captured