addFrac method

void addFrac(
  1. TeXFunction frac

Adds a fraction to the current node.

There are two options: Either we divide the previous term, or we add an empty frac.


void addFrac(TeXFunction frac) {
  // We first want to divide the list with children at the current courser
  // position. This way, we can always look at the last element in the list,
  // when taking the numerator, and don't need to keep track of the index.
  final tail = currentNode.children.sublist(currentNode.courserPosition);
      .removeRange(currentNode.courserPosition, currentNode.children.length);
  // Expressions that indicate operators.
  final operators = ['+', '-', r'\cdot', r'\div'];
  // We need to determine whether we want to append an empty fraction or
  // divide the last expression, therefore keep it as the numerator.
  var keepNumerator = true;
  // There are 3 cases where we want to append a clean fraction, and therefore
  // don't keep a numerator.
  // CASE 1: The current node is empty.
  if (currentNode.children.isEmpty) {
    keepNumerator = false;
  // CASE 2: The tex symbol before is a opening parenthesis.
  else if (currentNode.children.last.expression.endsWith('(')) {
    keepNumerator = false;
  // CASE 3: The tex symbol before is an operator.
  else if (operators.contains(currentNode.children.last.expression)) {
    keepNumerator = false;
  // If the goal is to divide the previous expression, we want to remove this
  // part from the current node and use it as the first argument of the frac.
  if (keepNumerator) {
    // We then need to set the courserPosition to the new length of the
    // current node's children list.
    currentNode.courserPosition = currentNode.children.length;
  // We know want to add all elements that we saved earlier to the end of
  // the list.
  // If we took the numerator, we want to jump straight into the second
  // argument.
  currentNode = frac.argNodes[keepNumerator ? 1 : 0];