Git Product home page Git Product logo

Comments (3)

bloc97 avatar bloc97 commented on August 24, 2024

If someone else also has this problem, this quick hack can work, although I don't know if it will break some other expressions.
It detects the ^ symbol, and adds brackets between the ^ and the next dissimilar character.
eg. x^34+y -> x^{34}+y
eg. 45^x+3 -> 45^{x}+3
eg. 45^xy+3 -> 45^{xy}+3
eg. 45^{x+y+3} -> 45^{{x+y+3}}

Use the function fixExpLatex() on the TeX part of the Algebrite output, make sure the TeX string does not contain any spaces.

const isLetter = function(char) {
    return char.toLowerCase() !== char.toUpperCase();
};

const isNumber = function(char) {
    return +char === +char;
};

const findNextDifferentChar = function(str, index) { //find the next dissimilar character
    if (isNumber(str[index])) { //if character is a number, return next character that isn't a number
        for (let i=index; i<str.length; i++) {
            if (!isNumber(str[i])) {
                return i;
            }
        }
        return str.length;
    } else if (isLetter(str[index])) { //if character is a letter, return next character that isn't a letter
        for (let i=index; i<str.length; i++) {
            if (!isLetter(str[i])) {
                return i;
            }
        }
        return str.length;
        
    } else if (str[index] === "{") { //if character is a bracket, return next character that is the inverted bracket, so as to not modify existing expressions
        for (let i=index; i<str.length; i++) {
            if (str[i] === "}") {
                return i;
            }
        }
        return str.length;
        
    } else { //if character is a parenthesis or a symbol, return the end index
        return str.length;
    }
};

const fixExpLatex = function(str) {
    
    let textPart = [];
    let lastindex = 0;
    
    for (let i=0; i<str.length-1; i++) {
        if (str[i] === "^") {
            const nextindex = findNextDifferentChar(str, i+1); //find next character that isn't similar to the one after the "^"
            textPart.push(str.slice(lastindex, i+1)); //push the unchanged part
            textPart.push("{" + str.slice(i+1, nextindex) + "}"); //push the exponent part in brackets
            lastindex = nextindex;
            i = nextindex-1; //skip the part already saved
        }
    }
    textPart.push(str.slice(lastindex, str.length)); //push in the last part that was not detected
    return textPart.join("");
};

from algebrite.

davidedc avatar davidedc commented on August 24, 2024

acknowledged. Will examine the suggested fix. Should be more compact to fix the buggy code in the print routine inside but I'll check.

from algebrite.

davidedc avatar davidedc commented on August 24, 2024

fixed in 0.4.2 release notes here: http://algebrite.org/docs/index.html

from algebrite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.