Skip to content

Commit

Permalink
Fix multiline display equations in LaTeX
Browse files Browse the repository at this point in the history
This is an identical bugfix fo #1518, which was only applied to the
HTML writer.
  • Loading branch information
odow committed Oct 13, 2021
1 parent 4ad72d8 commit 28c8b34
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
22 changes: 22 additions & 0 deletions docs/src/man/latex.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,25 @@ To write a system of equations, use the `aligned` environment:
```

These are Maxwell's equations.

## Printing LaTeX from Julia

To pretty-print LaTeX from Julia, overload `Base.show` for the
`MIME"text/latex"` type. For example:
```@example
struct LaTeXEquation
content::String
end
function Base.show(io::IO, ::MIME"text/latex", x::LaTeXEquation)
# Wrap in $$ for display math printing
return print(io, "\$\$ " * x.content * " \$\$")
end
LaTeXEquation(raw"""
\left[\begin{array}{c}
x \\
y
\end{array}\right]
""")
```
2 changes: 2 additions & 0 deletions src/Writers/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,8 @@ function mdconvert(d::Dict{MIME,Any}, parent; kwargs...)
# If the show(io, ::MIME"text/latex", x) output is already wrapped in \[ ... \] or $$ ... $$, we
# unwrap it first, since when we output Markdown.LaTeX objects we put the correct
# delimiters around it anyway.
#
# This bug also occurs in the LaTeXWriter.
latex = d[MIME"text/latex"()]
# Make sure to match multiline strings!
m_bracket = match(r"\s*\\\[(.*)\\\]\s*"s, latex)
Expand Down
16 changes: 15 additions & 1 deletion src/Writers/LaTeXWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,21 @@ function latex(io::IO, d::Dict{MIME,Any})
\\end{figure}
""")
elseif haskey(d, MIME"text/latex"())
latex(io, Utilities.mdparse(d[MIME"text/latex"()]; mode = :single))
# If the show(io, ::MIME"text/latex", x) output is already wrapped in
# \[ ... \] or $$ ... $$, we unwrap it first, since when we output
# Markdown.LaTeX objects we put the correct delimiters around it anyway.
#
# This bug also occurs in the HTMLWriter.
latex_str = d[MIME"text/latex"()]
# Make sure to match multiline strings!
m_bracket = match(r"\s*\\\[(.*)\\\]\s*"s, latex_str)
m_dollars = match(r"\s*\$\$(.*)\$\$\s*"s, latex_str)
if m_bracket === nothing && m_dollars === nothing
latex(io, Utilities.mdparse(latex_str; mode = :single))
else
out = m_bracket !== nothing ? m_bracket[1] : m_dollars[1]
latex(io, Markdown.LaTeX(out))
end
elseif haskey(d, MIME"text/markdown"())
latex(io, Markdown.parse(d[MIME"text/markdown"()]))
elseif haskey(d, MIME"text/plain"())
Expand Down

0 comments on commit 28c8b34

Please sign in to comment.