How can I create a line break in Katex?

9.4k views Asked by At

I am trying to test rendering of katex on my browser. It renders correctly, but when I try using \newline to signal a line break, it throws me an error:

ParseError: KaTeX parse error: Expected 'EOF', got '\newline' at position 45: …)-\tfrac{1}{2}x\̲n̲e̲w̲l̲i̲n̲e̲ \Righta…

<body>
   <div class="maths">
       \Rightarrow\quad F(x)-\tfrac{1}{2}x
       \Rightarrow\quad  F(x)-F(a)\leq\tfrac{1}{2}(x-a)[2ex]
</div> 
<script>
// Get all <div class ="maths"> elements in the document
    var x = document.getElementsByClassName('maths');  
    for (var i = 0; i < x.length; i++) {
        try {
            if (x[i].tagName == "DIV") {
                t = katex.render(x[i].textContent, x[i], { displayMode: true });
            } else {
                t = katex.render(x[i].textContent, x[i]);
            }
        }
        catch (err) {
            x[i].style.color = 'red';
            x[i].textContent = err;
        }
}
</script>

How can I call for a newline in Katex since "\newline" doesn't work?

1

There are 1 answers

2
Jeremy Feng On

Use three backslashs instead of two.

I'm using markdown to publish blog with KaTeX. It works for me.

$$
\begin{aligned}
&(n-1)(n-1)\\\
&=n^2-2n+1
\end{aligned}
$$

It renders:

KaTeX Output