Documenter.jl icon indicating copy to clipboard operation
Documenter.jl copied to clipboard

MathJax Macros with arguments

Open haakon-e opened this issue 7 months ago • 6 comments

I'm trying to follow the docs to create my own macros, but am struggling with certain ones. For example, how would I create either of these macros in a way that works?

mathengine = Documenter.MathJax3(Dict(:TeX => Dict(
    :equationNumbers => Dict(:autoNumber => "AMS"),
    :Macros => Dict(
        :t => ["\\text#1", 1],
        :dx => ["\\frac{d#2}{d#1}", 2, "x"],
    ),
)))

(the latter is from mathjax docs).

haakon-e avatar Jun 13 '25 00:06 haakon-e

It's not clear to me what you are asking. I assume you tried out the example from the manual you are quoting, and something didn't work. But what exactly didn't work? What's the concrete issue?

fingolfin avatar Oct 02 '25 07:10 fingolfin

@haakon-e ping

fingolfin avatar Nov 04 '25 11:11 fingolfin

Thanks for the critical questions. In hindsight I should have provided a more comprehensive reproducer. It seems like the \text macro works in MathJax3 now, but not \frac. Both work with MathJax2.

I created an empty package TestPackage with pkg> generate TestPackage then added

docs/make.jl
docs/Project.toml  # with Documenter, TestPackage as dependencies
docs/src/index.md

index.md has the following contents:

hello world

```math
\ket{\phi}, \bra{\phi}
```

```math
\dyx{y}
```

```math
\dx{x}{y}
```

make.jl has the following contents (illustrating two MathJax options)

using Documenter, TestPackage

macros = Dict(
    :ket => ["|#1\\rangle", 1],
    :bra => ["\\langle#1|", 1],
    :dyx => ["\\frac{d#1}{dx}", 1],
    :dx => ["\\frac{d#2}{d#1}", 2, "x"],
    :t => ["\\text#1", 1],
)

v = 2
if v == 2
    ## With MathJax2
    mathengine = Documenter.MathJax(Dict(:TeX => Dict(
        :equationNumbers => Dict(:autoNumber => "AMS"),
        :Macros => macros,
    )))
elseif v == 3
    ## With MathJax3
    mathengine = MathJax3(Dict(
        :loader => Dict("load" => ["[tex]/physics"]),
        :tex => Dict(
            "inlineMath" => [["\$","\$"], ["\\(","\\)"]],
            "tags" => "ams",
            "packages" => ["base", "ams", "autoload", "physics"],
        ),
        :Macros => macros,
    ))
end

makedocs(sitename="My docs", remotes=nothing,
    format = Documenter.HTML(;mathengine)
)

With Documenter.MathJax, i.e. v=2, the docs compile as expected:

Image

With Documenter.MathJax3, i.e. v=3, the equations do not compile

Image

Hope this is more helpful!

haakon-e avatar Nov 04 '25 21:11 haakon-e

Shouldn't the .macros section also go into the .tex section for MathJax v3? X-ref: https://docs.mathjax.org/en/v3.0/options/index.html#configuring-mathjax

So maybe something like this?

mathengine = MathJax3(Dict(
    :loader => Dict("load" => ["[tex]/physics"]),
    :tex => Dict(
        "inlineMath" => [["\$","\$"], ["\\(","\\)"]],
        "tags" => "ams",
        "packages" => ["base", "ams", "autoload", "physics"],
        "macros" => macros,
    ),
))

mortenpi avatar Nov 05 '25 08:11 mortenpi

I tried both. It makes no difference:

mathengine = MathJax3(Dict(
    :loader => Dict("load" => ["[tex]/physics"]),
    :tex => Dict(
        "inlineMath" => [["\$","\$"], ["\\(","\\)"]],
        "tags" => "ams",
        "packages" => ["base", "ams", "autoload", "physics"],
        "macros" => macros,
    ),
    :macros => macros,
))
Image

haakon-e avatar Nov 07 '25 21:11 haakon-e

My suggestion here would be to try to work backwards then. Take the generated HTML/JS site, look at the generated JS for this in assets/documenter.js, and try to figure out how to fix that. Once the JS works, then figure out if Documenter can generate the necessary JS already or we need to change something.

mortenpi avatar Nov 11 '25 09:11 mortenpi