Skip to content

Commit

Permalink
Merge pull request #213 from gustaphe/usepackage
Browse files Browse the repository at this point in the history
Provide packages to render
  • Loading branch information
gustaphe committed Mar 7, 2022
2 parents 4350135 + 60fedb5 commit 59ad11c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Latexify"
uuid = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
authors = ["Niklas Korsbo <niklas.korsbo@gmail.com>"]
repo = "https://github.com/korsbo/Latexify.jl.git"
version = "0.15.12"
version = "0.15.13"

[deps]
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
Expand Down
11 changes: 11 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ latexify(:(x/y)) |> s -> render(s, MIME("image/png"))
PNG and SVG outputs rely on [dvipng](http://www.nongnu.org/dvipng/) and [dvisvgm](https://dvisvgm.de/), respectively.
If your code requires specific packages or document classes to render
correctly, you can supply those as keyword arguments:
```julia
L"\qty{1.25}{nm}" |> render(s, MIME("image/png"); documentclass="article", packages=("microtype", ("siunitx", exponent-product="\cdot"))
```
The arguments to these are either strings, or tuples of strings where the first
one is the name of the package or class, and any further are optional
arguments.
## Legacy support
Latexify.jl has stopped supporting Julia versions older than 0.7. This does not mean that you cannot use Latexify with earlier versions, just that these will not get new features. Latexify.jl's release v0.4.1 was the last which supported Julia 0.6. Choose that release in the dropdown menu if you want to see that documentation.
92 changes: 78 additions & 14 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ function add_brackets(ex::Expr, vars)
end


function _writetex(s::LaTeXString; name=tempname(), command="\\Large")
function _writetex(s::LaTeXString;
name=tempname(),
command="\\Large",
documentclass=("standalone", "varwidth=100cm"),
packages=occursin("\\ce{", s) ? (
"amssymb","amsmath","mhchem"
) : (
"amssymb","amsmath"
),
)
doc = """
\\documentclass[varwidth=100cm]{standalone}
\\usepackage{amssymb}
\\usepackage{amsmath}
$(occursin("\\ce{", s) ? "\\usepackage{mhchem}" : "")
\\documentclass$(_packagename(documentclass))
"""*prod(
"\\usepackage$(_packagename(k))\n" for k in packages
)*"""
\\begin{document}
{
$command
Expand Down Expand Up @@ -103,8 +112,19 @@ function render(s::LaTeXString, mime::MIME"juliavscode/html"; kwargs...)
end


function render(s::LaTeXString, ::MIME"application/pdf"; debug=false, name=tempname(), command="\\Large", open=true)
_writetex(s; name=name, command=command)
function render(s::LaTeXString, ::MIME"application/pdf";
debug=false,
name=tempname(),
command="\\Large",
open=true,
documentclass=("standalone", "varwidth=100cm"),
packages=occursin("\\ce{", s) ? (
"amssymb","amsmath","mhchem"
) : (
"amssymb","amsmath"
),
)
_writetex(s; name=name, command=command, documentclass=documentclass, packages=packages)

cd(dirname(name)) do
cmd = `lualatex --interaction=batchmode $(name).tex`
Expand All @@ -127,8 +147,19 @@ function render(s::LaTeXString, ::MIME"application/pdf"; debug=false, name=tempn
end


function render(s::LaTeXString, ::MIME"application/x-dvi"; debug=false, name=tempname(), command="\\Large", open=true)
_writetex(s; name=name, command=command)
function render(s::LaTeXString, ::MIME"application/x-dvi";
debug=false,
name=tempname(),
command="\\Large",
open=true,
documentclass=("standalone", "varwidth=100cm"),
packages=occursin("\\ce{", s) ? (
"amssymb","amsmath","mhchem"
) : (
"amssymb","amsmath"
),
)
_writetex(s; name=name, command=command, documentclass=documentclass, packages=packages)

cd(dirname(name)) do
cmd = `dvilualatex --interaction=batchmode $(name).tex`
Expand All @@ -151,8 +182,21 @@ function render(s::LaTeXString, ::MIME"application/x-dvi"; debug=false, name=tem
end


function render(s::LaTeXString, ::MIME"image/png"; debug=false, name=tempname(), command="\\Large", callshow=true, open=true, dpi=300)
render(s, MIME("application/x-dvi"); debug=debug, name=name, command=command, open=false)
function render(s::LaTeXString, ::MIME"image/png";
debug=false,
name=tempname(),
command="\\Large",
callshow=true,
open=true,
dpi=300,
documentclass=("standalone", "varwidth=100cm"),
packages=occursin("\\ce{", s) ? (
"amssymb","amsmath","mhchem"
) : (
"amssymb","amsmath"
),
)
render(s, MIME("application/x-dvi"); debug=debug, name=name, command=command, open=false, documentclass=documentclass, packages=packages)

cmd = `dvipng -bg Transparent -D $(dpi) -T tight -o $(name).png $(name).dvi`
debug || (cmd = pipeline(cmd, devnull))
Expand All @@ -170,8 +214,20 @@ function render(s::LaTeXString, ::MIME"image/png"; debug=false, name=tempname(),
end


function render(s::LaTeXString, ::MIME"image/svg"; debug=false, name=tempname(), command="\\Large", callshow=true, open=true)
render(s, MIME("application/x-dvi"); debug=debug, name=name, command=command, open=false)
function render(s::LaTeXString, ::MIME"image/svg";
debug=false,
name=tempname(),
command="\\Large",
callshow=true,
open=true,
documentclass=("standalone", "varwidth=100cm"),
packages=occursin("\\ce{", s) ? (
"amssymb","amsmath","mhchem"
) : (
"amssymb","amsmath"
),
)
render(s, MIME("application/x-dvi"); debug=debug, name=name, command=command, open=false, documentclass=documentclass, packages=packages)

cmd = `dvisvgm -n -v 0 -o $(name).svg $(name).dvi`
debug || (cmd = pipeline(cmd, devnull))
Expand Down Expand Up @@ -200,11 +256,19 @@ function expr_to_array(ex)
## If it is a matrix
if ex.args[1] isa Expr && ex.args[1].head == :row
return eval(ex.head)(map(y -> permutedims(y.args), ex.args)...)
else
else
if ex.head == :hcat
return safereduce(hcat, ex.args)
elseif ex.head in [:vcat, :vect]
return safereduce(vcat, ex.args)
end
end
end

function _packagename(x::AbstractString)
return "{$x}"
end

function _packagename(x::Tuple)
return "[$(join(x[2:end], ", "))]{$(first(x))}"
end
20 changes: 19 additions & 1 deletion test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end
\documentclass[varwidth=100cm]{standalone}
\usepackage{amssymb}
\usepackage{amsmath}
\begin{document}
{
\Large
Expand All @@ -58,3 +57,22 @@ end
}
\end{document}
""", "\r\n"=>"\n")

Latexify._writetex(L"\qty{135}{nm}"; name=name, documentclass=("article", "a4paper"), packages=("siunitx",))
tex = open("$(name).tex") do f
read(f, String)
end
@test tex == replace(raw"""
\documentclass[a4paper]{article}
\usepackage{siunitx}
\begin{document}
{
\Large
$\qty{135}{nm}$
}
\end{document}
""", "\r\n"=>"\n")

@test Latexify._packagename("hi") == "{hi}"
@test Latexify._packagename(("hi", "x=5")) == "[x=5]{hi}"
@test Latexify._packagename(("hi", "x=5", "y")) == "[x=5, y]{hi}"

2 comments on commit 59ad11c

@gustaphe
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/56148

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.15.13 -m "<description of version>" 59ad11c3293688a1ddd06e0bced9dfcdb94980fb
git push origin v0.15.13

Please sign in to comment.