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 MIME type in LaTeXWriter #1709

Merged
merged 5 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Documenter.jl changelog

## Unreleased

* ![Bugfix][badge-bugfix] Fix text/latex MIME type in LaTeXWriter. ([#1709][github-1709])

## Version `v0.27.8`

Expand Down
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]
""")
```
3 changes: 2 additions & 1 deletion src/Writers/LaTeXWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ 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 it has a latex MIME, just write it out directly.
_print(io, d[MIME"text/latex"()])
elseif haskey(d, MIME"text/markdown"())
latex(io, Markdown.parse(d[MIME"text/markdown"()]))
elseif haskey(d, MIME"text/plain"())
Expand Down
93 changes: 93 additions & 0 deletions test/examples/src.latex_simple/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,96 @@ julia> 127 % Int8
```julia-repl sayhello2
julia> function foo end;
```

## Issue 1401: rendering custom LaTeX

### @example block / show with `text/latex` MIME

```@example
struct Table end

Base.show(io, ::MIME"text/latex", ::Table) = write(io, raw"""
\begin{tabular}{r|ccc}
& i & y & z\\
\hline
& Int64 & Char & Int64\\
\hline
1 & 1 & 'A' & 5 \\
2 & 2 & 'B' & 6 \\
3 & 3 & 'C' & 7 \\
4 & 4 & 'D' & 8 \\
\end{tabular}
""")

Table()
```

### @raw block

```@raw latex
\begin{tabular}{r|ccc}
& i & y & z\\
\hline
& Int64 & Char & Int64\\
\hline
1 & 1 & 'A' & 5 \\
2 & 2 & 'B' & 6 \\
3 & 3 & 'C' & 7 \\
4 & 4 & 'D' & 8 \\
\end{tabular}
```

### Inline LaTeX

_Note: this should render as just text, not as a table._

\begin{tabular}{r|ccc}
& i & y & z\\
\hline
& Int64 & Char & Int64\\
\hline
1 & 1 & 'A' & 5 \\
2 & 2 & 'B' & 6 \\
3 & 3 & 'C' & 7 \\
4 & 4 & 'D' & 8 \\
\end{tabular}

## 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]
""")
```

```@example
struct LaTeXEquation2
content::String
end

function Base.show(io::IO, ::MIME"text/latex", x::LaTeXEquation2)
# Wrap in \[...\] for display math printing
return print(io, "\\[ " * x.content * " \\]")
end

LaTeXEquation2(raw"""
\left[\begin{array}{c}
x \\
y
\end{array}\right]
""")
```