Skip to content

LaTeX parsing: Invalid subscript with complex number i (e.g. i_A) #256

@tejashah88

Description

@tejashah88

Description

It seems that i with any subscript is not being processed properly, very similar to #255. The compute engine still tries to treat i as a complex number, instead of treating i_A as its own variable.

Steps to Reproduce

import { ComputeEngine } from "@cortex-js/compute-engine";

const ce = new ComputeEngine();

const BATCH_1 = [
    'i',
    'i+1',
    'i_A',
    'i_A+1',
    '\frac{i_{A}}{i}',
];

function convertLatexToMathJson(latex) {
    const parsedExpr = ce.parse(latex);
    const boxedExpr = ce.box(parsedExpr, { canonical: true });
    return boxedExpr.json;
}

// Run testing
console.log('Batch 1 Testing');
for (const expr of BATCH_1) {
    const mjson = convertLatexToMathJson(expr);
    console.log(expr, ' => ', JSON.stringify(mjson, null, 2));
}

Actual Result

Batch 1 Testing
i  =>  [
  "Complex",
  0,
  1
]
i+1  =>  [
  "Add",
  1,
  [
    "Complex",
    0,
    1
  ]
]
i_A  =>  [
  "Subscript",
  [
    "Complex",
    0,
    1
  ],
  "A"
]
i_A+1  =>  [
  "Add",
  [
    "Error",
    [
      "ErrorCode",
      "'incompatible-type'",
      "number",
      "expression"
    ]
  ],
  1
]
\frac{i_{A}}{i}  =>  [
  "Divide",
  [
    "Error",
    [
      "ErrorCode",
      "'incompatible-type'",
      "number",
      "expression"
    ]
  ],
  [
    "Complex",
    0,
    1
  ]
]

Environment

Testing code is ran against Node.js v22.20.0 and NPM v10.9.3 using compute engine v0.30.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions