doks icon indicating copy to clipboard operation
doks copied to clipboard

KaTex cannot render some equations

Open preminstrel opened this issue 4 years ago • 14 comments

The problem is that when I use KaTex to render my equations, some eqautions can be rendered while others cannot be. Such equation like below cannot be rendered: (use env aligned)

$$\left\{
\begin{aligned}
PDE \quad &u_{tt}-a^2u_{xx}=0 \\
B.C.\quad &u(0,t)=u(l,t)=0\quad t\ge 0 \\
I.C.\quad & u(x,0)=\varphi(x),u_t(x,0)=\phi(x)\quad 0\le x\le l
\end{aligned}
\right.$$

And the result is below: Snipaste_2021-09-11_12-46-29

Thanks for your time !

preminstrel avatar Sep 11 '21 04:09 preminstrel

I find that codes below can work.

  • no line feed
  • use \\ not \
$$\\left\\{\\begin{aligned}PDE \\quad &u_{tt}-a^2u_{xx}=0 \\\B.C.\\quad &u(0,t)=u(l,t)=0\\quad t\\ge 0 \\\I.C.\\quad & u(x,0)=\\varphi(x),u_t(x,0)=\\phi(x)\\quad 0\\le x\\le l \\\\ \end{aligned}\\right.$$

So how I fix it? Is that an error that maybe happened in keTax.auto-render?

preminstrel avatar Sep 11 '21 13:09 preminstrel

@preminstrel, I think it's a configuration setting in ./assets/js/katex.js — see also "newLineInDisplayMode" in options.

Unfortunately, I'm not a KaTeX /LaTeX expert. @emptymalei, could you be of any help? Thanks!

For now, using \newline in stead of \\ does work:

$$
\begin{aligned}
PDE \quad &u_{tt}-a^2u_{xx}=0 \newline
B.C.\quad &u(0,t)=u(l,t)=0\quad t\ge 0 \newline
I.C.\quad & u(x,0)=\varphi(x),u_t(x,0)=\phi(x)\quad 0\le x\le l
\end{aligned}
$$

Snag_5bcbdcd

h-enk avatar Sep 14 '21 17:09 h-enk

@h-enk I'm not familiar with KaTeX. I've been using Mathjax for math. The double-slash works fine in mathjax.

I'll have a look at this. I'm also interested in switching to KaTeX as it is faster than mathjax.

emptymalei avatar Sep 15 '21 07:09 emptymalei

Thank you for your time!
It exactly works. @h-enk

preminstrel avatar Sep 15 '21 09:09 preminstrel

It seems to be a Goldmark issue. A better (portable) trick for now, use <div></div> like so:

<div>
$$\left\{
\begin{aligned}
PDE \quad &u_{tt}-a^2u_{xx}=0 \\
B.C.\quad &u(0,t)=u(l,t)=0\quad t\ge 0 \\
I.C.\quad & u(x,0)=\varphi(x),u_t(x,0)=\phi(x)\quad 0\le x\le l
\end{aligned}
\right.$$
</div>

h-enk avatar Sep 15 '21 09:09 h-enk

This method seems extremely good and portable. I will use this one!😀 Thanks for your time and nice theme.

preminstrel avatar Sep 15 '21 09:09 preminstrel

Katex seems to be broken.

I checked #181, but the math equation on the site is not working anymore.

https://math-typesetting--doks.netlify.app/docs/examples/math/

image

tuyenld avatar Aug 10 '22 10:08 tuyenld

I tried

$$
\begin{aligned }
J_n = nqv_d \newline
J_n = nq\dfrac{1}{2} \dfrac{q\tau}{m^{2}}E
\end{aligned }
$$

$$
\begin{aligned}
  a   &=b+c \cr
  d+e &=f
\end{aligned}
$$

$$
\begin{aligned}
  a & b \\\\ c & d
\end{aligned}
$$

but none of them works.

tuyenld avatar Aug 10 '22 13:08 tuyenld

@tuyenld Hmm, I can't confirm your issue — following (the example in) the Doks docs (https://getdoks.org/docs/how-to/extra-features/katex/), it works for me.

Also, this works for me:

$$
J_n = nqv_d \newline
J_n = nq\dfrac{1}{2} \dfrac{q\tau}{m^{2}}E
$$

Snag_a9718a6

h-enk avatar Aug 12 '22 15:08 h-enk

Do you have a repo you could share?

h-enk avatar Aug 12 '22 15:08 h-enk

Do you have a repo you could share?

Yes. Here is my post, and this is my md file. I changed script-footer.html to adapt my front matter need, but I don't think it is a problem.

Turn out that if I removed minify from $katex | minify on script-footer.html, it displays correctly. Don't know what causes the problem.

tuyenld avatar Aug 12 '22 18:08 tuyenld

Could you try with removing the space in \begin{aligned } and \end{aligned }, like so:

$$
\begin{aligned}
J_n = nqv_d \newline
J_n = nq\dfrac{1}{2} \dfrac{q\tau}{m^{2}}E
\end{aligned}
$$

h-enk avatar Aug 15 '22 08:08 h-enk

Turn out that if I removed minify from $katex | minify on script-footer.html, it displays correctly. Don't know what causes the problem.

Yes, confirmed. We could configure the Hugo minify command to make it work. But, I think it's best to use the minified Katex script itself in the first place.

So,

  1. Update https://github.com/h-enk/doks/blob/master/layouts/partials/footer/script-footer.html#L10:

    {{ $katex := resources.Get "js/vendor/katex/dist/katex.min.js" -}}
    
  2. Update https://github.com/h-enk/doks/blob/master/layouts/partials/footer/script-footer.html#L99:

      {{ $katex := $katex | fingerprint "sha512" -}}
    

h-enk avatar Aug 15 '22 09:08 h-enk

Turn out that if I removed minify from $katex | minify on script-footer.html, it displays correctly. Don't know what causes the problem.

Yes, confirmed. We could configure the Hugo minify command to make it work. But, I think it's best to use the minified Katex script itself in the first place.

So,

  1. Update https://github.com/h-enk/doks/blob/master/layouts/partials/footer/script-footer.html#L10:
    {{ $katex := resources.Get "js/vendor/katex/dist/katex.min.js" -}}
    
  2. Update https://github.com/h-enk/doks/blob/master/layouts/partials/footer/script-footer.html#L99:
      {{ $katex := $katex | fingerprint "sha512" -}}
    

It is not enough for this case.

$$
\begin{align*}
    u(t)={
        \begin{cases}
        1&{\text{if }}t \ge 0 \newline  
        0&{\text{if }}t \lt 0 \newline
        \end{cases}}
    \qquad
    u[n]={
        \begin{cases}
        1&{\text{if }}n \ge 0 \newline
        0&{\text{if }}n \lt 0 \newline
        \end{cases}}
\end{align*}
$$

I need to use {{ .Inner }} shortcode to make it works.

tuyenld avatar Aug 16 '22 07:08 tuyenld

Doks now uses the free Math API service. See also Mathematical Expressions

h-enk avatar Jan 08 '24 14:01 h-enk