code: def format_element(
      element: Element, keep_refs: bool = False, latex_env: bool = False
  ) -> List[str]:
      """
      Formats a given Element into a list of formatted strings.
  
      Args:
          element (Element): The element to be formatted.
          keep_refs (bool, optional): Whether to keep references in the formatting. Default is False.
          latex_env (bool, optional): Whether to use LaTeX environment formatting. Default is False.
  
      Returns:
          List[str]: A list of formatted strings representing the formatted element.
      """
      if isinstance(element, TextElement):
          if latex_env:
              return [latex_escape(element.content)]
          else:
              return [element.content]
      if isinstance(element, Bold):
          parts = format_children(element, keep_refs, latex_env)
          if element.find_parent(Algorithm) is not None:
              return parts
          lead, text, tail = leading_trailing_whitespace("".join(parts))
          return [*lead, "**", *remove_line_breaks(text), "**", *tail]
      if isinstance(element, Italic):
          parts = format_children(element, keep_refs, latex_env)
          if element.find_parent(Algorithm) is not None:
              return parts
          lead, text, tail = leading_trailing_whitespace("".join(parts))
          return [*lead, "_", *remove_line_breaks(text), "_", *tail]
      if isinstance(element, PlaintextMath):
          return format_children(element, keep_refs) + ["\n"]
      if isinstance(element, Paragraph):
          return format_children(element, keep_refs, latex_env) + [
              "\n\n"
          ]
      if isinstance(element, TableCell):
          parts = format_children(element, keep_refs, latex_env)
          remove_trailing_whitespace(parts)
          if element.multirow is not None:
              parts.insert(0, "\\multirow{%i}{*}{" % (element.multirow))
              parts.append("}")
          if element.multicolumn is not None:
              parts.insert(
                  0,
                  "\\multicolumn{%i}{%s}{"
                  % (element.multicolumn, element.spec),
              )
              parts.append("}")
          return parts
      if isinstance(element, TableRow):
          parts = []
          if element.hline_above:
              parts.append(element.hline_above + "\n")
          parts.extend(
              remove_line_breaks(
                  format_iterator(
                      element.cells, keep_refs, latex_env, join=" & "
                  )
              )
          )
          parts.append(r" \\")
          parts.append((" " + element.hline_below).rstrip())
          return parts
      if isinstance(element, Tabular):
          parts = [
              "\\begin{tabular}",
              "{%s}\n" % element.get_table_spec(),
          ]
          parts.extend(
              format_iterator(element.rows, keep_refs, True, join="\n")
          )
          parts.append("\n\\end{tabular}\n")
          return parts
      if isinstance(element, Table):
          parts = [
              "[TABLE%s]\n\\begin{table}\n"
              % (
                  str(uuid4())[:5]
                  if element.id is None
                  else ":" + str(element.id)
              )
          ]
          parts.extend(format_children(element, keep_refs, latex_env))
          caption_parts = format_element(
              element.caption, keep_refs, latex_env
          )
          remove_trailing_whitespace(caption_parts)
          parts.append("\\end{table}\n")
          if len(caption_parts) > 0:
              parts.extend(caption_parts + ["\n"])
          parts.append("[ENDTABLE]\n\n")
          return parts
      if isinstance(element, Figure):
          parts = format_element(element.caption, keep_refs)
          remove_trailing_whitespace(parts)
          return (
              [
                  "[FIGURE%s]\n"
                  % (
                      str(uuid4())[:5]
                      if element.id is None
                      else ":" + str(element.id)
                  )
              ]
              + parts
              + ["\n[ENDFIGURE]\n\n"]
          )
      if isinstance(element, SectionHeader):
          parts = ["# "]
          if element.id:
              parts.append(f"{element.id.upper()} ")
          if element.header:
              header = format_element(element.header, keep_refs)
          else:
              header = format_iterator(element.children, keep_refs)
          _, title, _ = leading_trailing_whitespace("".join(header))
          parts.append(title)
          parts.append("\n\n")
          return parts
      if isinstance(element, Section):
          children_parts = format_children(element, keep_refs)
          if is_empty(children_parts):
              return []
          if element.header:
              parts = [f"\n\n{'#'*element.hnum} "]
              _, title, _ = leading_trailing_whitespace(
                  "".join(format_element(element.header, keep_refs))
              )
              parts.append(title)
              parts.append("\n\n")
          else:
              parts = []
          return parts + children_parts
      if isinstance(element, Footnote):
          if element.id is not None:
              foot = f"\n[FOOTNOTE:{element.id}]Footnote {element.id}: "
          else:
              foot = "\n[FOOTNOTE:%s]Footnote: " % (str(uuid4())[:5])
          return (
              [foot]
              + format_children(element, keep_refs)
              + ["[ENDFOOTNOTE]\n\n"]
          )
      if isinstance(element, ListContainer):
          items = [
              (
                  item.label,
                  "".join(format_element(item, keep_refs))
                  .strip()
                  .replace("\n", " "),
              )
              for item in element.items
          ]
          parts = ["\n"]
          indent = "  " * max(element.level - 1, 0)
          for i, (label, item) in enumerate(items, 1):
              if label:
                  bullet = label
              else:
                  bullet = f"{i}." if element.ordered else "*"
              parts.append(f"{indent}{bullet} {item}\n")
          parts.append("\n")
          return parts
      if isinstance(element, Equation):
          # equation comprises of multiple displaystyle TeX formulas and optional equation label
          parts = []
          for child in element.children:
              if isinstance(child, LatexMath):
                  tex = normalize_tex(
                      "".join(format_element(child, keep_refs)).strip(
                          " \n"
                      ),
                      inline=False,
                  )
                  parts.append(tex)
              else:
                  text = "".join(format_element(child, keep_refs))
                  if text:
                      parts.append(text)
          lead, eqs, tail = leading_trailing_whitespace(parts)
          s = " ".join(eqs).replace(r"\] \[", " ")
          return [*lead, s, *tail]
      if isinstance(element, EquationList):
          parts = ["\n"]
          items = element.equations
          items = [
              "".join(format_element(item, keep_refs)).rstrip()
              for item in items
          ]
          items = [item + "\n" for item in items if item]
          if items:
              parts.extend(items)
              parts.append("\n")
          return parts
      if isinstance(element, Algorithm):
          parts = []
          items = element.lines
          items = [
              "".join(format_element(item, keep_refs)).rstrip()
              for item in items
          ]
          if element.inline:
              items = [item for item in items if item]
          else:
              items = [item + "\n" for item in items if item]
          if items:
              prepend = "`" if element.inline else "\n```\n"
              parts.append(prepend)
              parts.extend(items)
              append = "`" if element.inline else "```\n\n"
              parts.append(append)
          return parts
      if isinstance(element, DefinitionList):
          parts = ["\n"]
          if element.header is not None:
              parts.extend(format_element(element.header, keep_refs))
              parts.append("\n")
          items = [
              "".join(format_element(item, keep_refs)).rstrip()
              for item in element.items
          ]
          items = [item + "\n" for item in items if item]
          if items:
              parts.extend(items)
              parts.append("\n")
          return parts
      if isinstance(element, Definition):
          parts = []
          if element.term is not None:
              term = (
                  "".join(
                      format_element(element.term, keep_refs)
                  ).rstrip(" \n\t:")
                  + ": "
              )
              # maths in wiki might be inside a definition without a term
              if term.strip() != ":":
                  parts.append(term)
          if element.definition is not None:
              definition = "".join(
                  format_element(element.definition, keep_refs)
              ).rstrip()
              parts.append(definition)
          if parts:
              parts.append("\n")
          return parts
      if isinstance(element, LatexMath):
          parts = []
          if not element.inline:
              parts.append("\n\n")
          parts.append(
              normalize_tex(element.code, element.inline).strip()
          )
          if not element.inline:
              parts.append("\n\n")
          return parts
      if isinstance(element, (Superscript, Subscript)):
          content = element.plaintext
          if content.strip().isdigit():
              script_map = (
                  SUBSCRIPT_MAP
                  if isinstance(element, Subscript)
                  else SUPERSCRIPT_MAP
              )
              return [content.translate(script_map)]
          else:
              return format_children(element, keep_refs)
      if isinstance(element, InlineRef):
          parts = format_children(element, keep_refs)
          return parts
      return format_children(element, keep_refs, latex_env)
  
file_path: nougat/nougat/dataset/parser/markdown.py
imports: from typing import Iterable, List, Optional, Tuple
  import re
  from uuid import uuid4
  from nougat.dataset.utils import normalize_tex
  from nougat.dataset.parser.document import *
  from nougat.dataset.parser.latexml_parser import _clean_html_whitespace
  from unidecode import unidecode
label_desc: Use image node_id 6 for calling a global function with example usage: format_element(element, keep_refs, latex_env) and returns: format_children, list, list, unknown, unknown, parts, parts, parts, parts, unknown, parts, unknown, unknown, parts, list, parts, parts, parts, parts, parts, parts, list, list, parts, parts, list, list, format_children