Katex is not working in Angular 18 but works in console

Discussão em 'Angular' iniciado por thecodingsage, Outubro 30, 2024.

  1. I have imported katex with script tag in index.html of my angular 18 project like this:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css" integrity="sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+" crossorigin="anonymous">

    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js" integrity="sha384-7zkQWkzuo3B5mTepMUcHkMB5jZaolc2xDwL6VFqjFALcbeS9Ggm/Yr2r3Dy4lfFg" crossorigin="anonymous"></script>

    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js" integrity="sha384-43gviWU0YVjaDtb/GhzOouOXtZMP/7XUzwPTstBeZFe/+rCMvRwr4yROQP43s0Xk" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>


    There are no errors in the console, but it is not showing the updated version: [​IMG]

    Though after I run this command in the developer console it works:

    renderMathInElement(document.body)


    [​IMG]

    [​IMG]

    Here is the html code:

    <p>\(x^2 + y^2 = z^2\)</p>


    What else I tried:


    • Using katex from npm:

      import renderMathInElement from 'katex/contrib/auto-render'
      // ...
      ngAfterViewInit(){
      this.start()

      }

      start(){
      renderMathInElement(document.body)
      }



      SCSS:

      @import "../node_modules/katex/dist/katex.min.css";


      With no errors this didn't work either


    • Using MathJax: I used the script tag to load mathjax and used window.MathJax.typsetPromise()

      ngAfterViewInit(){
      this.start()

      }

      start(){
      window.MathJax.typsetPromise()
      }

    Additional Details:

    • I have tried using different lifecycle hooks (constructor, ngOnInit, and even triggering on a button click).
    • The console.log (Not shown above) in the start function prints but it does not change the appearance work.

    Any advice on what might be causing this issue or how to resolve it?

    Thanks in Advance!

    Continue reading...

Compartilhe esta Página