Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text/latex output in HTMLWriter #1283

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Writers/HTMLWriter.jl
Expand Up @@ -1646,7 +1646,11 @@ function mdconvert(d::Dict{MIME,Any}, parent; kwargs...)
elseif haskey(d, MIME"image/jpeg"())
out = Documents.RawHTML(string("<img src=\"data:image/jpeg;base64,", d[MIME"image/jpeg"()], "\" />"))
elseif haskey(d, MIME"text/latex"())
out = Utilities.mdparse(d[MIME"text/latex"()]; mode = :single)
mortenpi marked this conversation as resolved.
Show resolved Hide resolved
# If the show(io, ::MIME"text/latex", x) output is already wrapped in \[ ... \], we
# unwrap it first, since when we output Markdown.LaTeX objects we put the correct
# delimiters around it anyway.
m = match(r"\s*\\\[(.*)\\\]\s*", d[MIME"text/latex"()])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stripping of \[ and \] is directly motivated by SymPy. I am wondering if we should also strip $ .. $,$$ .. $$, \( .. \) etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me to be consistent here and just strip them all.

out = Markdown.LaTeX(m === nothing ? d[MIME"text/latex"()] : m[1])
elseif haskey(d, MIME"text/markdown"())
out = Markdown.parse(d[MIME"text/markdown"()])
elseif haskey(d, MIME"text/plain"())
Expand Down
24 changes: 24 additions & 0 deletions test/examples/src/man/tutorial.md
Expand Up @@ -385,3 +385,27 @@ Hello World!
Written in <a href="https://fonts.google.com/specimen/Nanum+Brush+Script">Nanum Brush Script.</a>
</div>
```

## Handling of `text/latex`

You can define a type that has a `Base.show` method for the `text/latex` MIME:

```@example showablelatex
struct LaTeXEquation
code :: String
end
Base.show(io, ::MIME"text/latex", latex::LaTeXEquation) = write(io, latex.code)
nothing # hide
```

In an `@example` or `@eval`block, it renders as an equation:

```@example showablelatex
LaTeXEquation("x^2")
```

Documenter also supports having the LaTeX text being already wrapped in `\[ ... \]`.

```@example showablelatex
LaTeXEquation("\\[\\left[ \\begin{array}{rr}x&2 x\\end{array}\\right]\\]")
```