michael-brade / LaTeX.js

JavaScript LaTeX to HTML5 translator
https://latex.js.org
MIT License
764 stars 58 forks source link

latex-parser.pegjs.js:825 "Two \\documentclass commands. The document may only declare one class" #155

Open artistl opened 1 month ago

artistl commented 1 month ago

Is there any obvious reason to be getting this error in the js console? I think I'm doing everything right...? My first time calling doPandoc(), everything works. But the second+ times I get this error. What is received back from the HTTP response is not a standalone latex doc... it has nothing like \documentclass.

<head>
  <script src="https://cdn.jsdelivr.net/npm/latex.js/dist/latex.js"></script>
</head>

...

<script>

    var isFirst = true

    let generator = new latexjs.HtmlGenerator({hyphenate: false})

    function doPandoc() {
        var contents = document.getElementById("in").value;

        $.post(
            "/kip/editor/pandoc",
            {markdown: contents},
            function(data) {
                generator = latexjs.parse(data, {generator: generator})
                if (isFirst) {
                    document.head.appendChild(generator.stylesAndScripts("https://cdn.jsdelivr.net/npm/latex.js@0.12.4/dist/"))
                }
                isFirst = false
                document.body.appendChild(generator.domFragment())
            }
        )
    }

</script>
michael-brade commented 2 weeks ago

Ah. Well, calling parse replaces the documentclass macro with that error. So that means you cannot use it twice on different documents. Therefore, you have to call generator.reset() first thing in your function, before latexjs.parse().

Let me know if that solves your issue.