docs: def latex_to_unicode(text, capitalize=False):
      """Replace LaTeX entities with the equivalent unicode and optionally capitalize.
  
      :param text: The LaTeX string to be converted
      :param capitalize: Can be 'sentence', 'name', 'title', 'upper', 'lower'
      """
      if capitalize:
          res = []
          brac_count = 0
          for i, c in enumerate(text):
              if c == '{':
                  brac_count += 1
              if c == '}':
                  brac_count -= 1
              if brac_count > 0:
                  res.append(c)
              elif capitalize == 'upper' or (i == 0 and not capitalize == 'lower'):
                  res.append(c.upper())
              elif capitalize == 'sentence' and (i > 2 and text[i - 1] == ' ' and text[i - 2] == '.'):
                  res.append(c.upper())
              elif (capitalize == 'name' and text[i - 1] in [' ', '-']) or (capitalize == 'title' and text[i - 1] == ' '):
                  nextword = text[i:].split(' ', 1)[0].rstrip(string.punctuation)
                  nextword = nextword[:1].lower() + nextword[1:] if text else ''
                  if capitalize == 'name' and nextword in NAME_SMALL:
                      res.append(c.lower())
                  elif capitalize == 'title' and nextword in SMALL:
                      res.append(c.lower())
                  else:
                      res.append(c.upper())
              elif capitalize == 'name' and c == c.upper():
                  n1 = text[i - 1] if i > 0 else None
                  n2 = text[i - 2] if i > 1 else None
                  n3 = text[i - 3] if i > 2 else None
                  n4 = text[i - 4] if i > 3 else None
                  if n2 == 'M' and n1 == 'c' and (not n3 or n3 == ' '):
                      res.append(c)  # McCartney
                  elif n2 == 'O' and n1 == '\'' and (not n3 or n3 == ' '):
                      res.append(c)  # O'Boyle
                  elif n3 == 'M' and n2 == 'a' and n1 == 'c' and (not n4 or n4 == ' '):
                      res.append(c)  # MacGarry
                  else:
                      res.append(c.lower())
              else:
                  res.append(c.lower())
          text = ''.join(res)
      if any(i in text for i in ['\\', '{', '}', '$', '&', '%', '#', '_']):
          for k, v in six.iteritems(LATEX_MAPPINGS):
              text = text.replace(k, v)
          for k, v in six.iteritems(LATEX_SUB_MAPPINGS):
              text = text.replace(k, v)
          for mod in ['mathbb', 'mathbf', 'mathbit', 'mathfrak', 'mathrm', 'mathscr', 'mathsf', 'mathsfbf', 'mathsfbfsl', 
                      'mathsfsl', 'mathsl', 'mathslbb', 'mathtt']:
              text = re.sub(r'\\%s\{([\\\w]+)\}' % mod, r'\1', text)
          for k, v in six.iteritems(LATEX_SUB_SUB_MAPPINGS):
              text = text.replace(k, v)
          for k, v in six.iteritems(LATEX_COMBINING_CHARS):
              text = re.sub(r'%s\{?(\w)\}?' % k, r'\1%s' % v, text)
          text = re.sub(r'\\noopsort\{.*?\}', r'', text)
          text = re.sub(r'\\path\|(.*?)\|', r'\1', text)
          text = re.sub(r'(?<!\\)[{}$]', r'', text)
          text = re.sub(r'\\([{}$&_])', r'\1', text)
      return text