Skip to content

Why do latex expressions need to have \\ instead of \ but not always #599

@Panuf

Description

@Panuf

Hi,

I do like library that You created. It is not issue as far as I know, I just need to know why this is happening, and if something can be setup in library to not manually adjust incomming markdown.

I was trying to use katex plugin due to LLM outputing some latex equasions for be but I got confuesed about the way \ character is used. I have compare how different equations work with markdown and katex directly.

Why for text, frac or escaping % inside text there need to be \ instead of ?
Why it is working fine with things like int, hat, pi etc?
Is the only solution just to modify incomming markdown or some option can be setup to handle this differently?

I am using Angular 18 with following versions of libraries:

    "katex": "^0.16.22",
    "ngx-markdown": "^18.1.0",

import { Component } from '@angular/core';
import { renderToString } from 'katex';
import { MarkdownComponent } from 'ngx-markdown';

@Component({
    selector: 'cpt-katex-test',
    standalone: true,
    imports: [MarkdownComponent],
    template: `
        @for (equation of equations; track equation) {
            <div>Equation: {{ equation }}</div>
            Using katex: <br /><br /><br />
            <div [innerHtml]="renderToString(equation)"></div>
            <br /><br /><br />
            Using markdown
            <markdown katex [data]="'$$ ' + equation + ' $$'"></markdown>
        }
    `,
})
export class KatexTestComponent {
    renderToString(equation: string): string {
        try {
            const result = renderToString(equation);
            return result;
        } catch {
            return 'Failed to render equation';
        }
    }

    equations = [
        'f(x) = \\int_{-\\infty}^\\infty \\hat f(\\xi) e^{2 \\pi i \\xi x} d\\xi',
        '\\\\text{Something\\\\%} = \\int_{-\\infty}^\\infty \\hat f(\\xi) e^{2 \\pi i \\xi x} d\\xi',
        '\\text{Something\\%} = \\frac{\\text{quotient}}{\\text{dividend}}',
        '\\\\text{Something\\\\%} = \\\\frac{\\\\text{quotient}}{\\\\text{dividend}}',
    ];
}


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions