Skip to content

Commit

Permalink
Merge pull request #100 from korsbo/render
Browse files Browse the repository at this point in the history
  • Loading branch information
korsbo authored Jun 5, 2020
2 parents fb34189 + af5cf56 commit 8205f13
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 4 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 <[email protected]>"]
repo = "https://github.com/korsbo/Latexify.jl.git"
version = "0.13.2"
version = "0.13.3"

[deps]
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
Expand Down
17 changes: 17 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,24 @@ and to reset your changes, use
```julia
reset_default()
```
## Macros
Two macros are exported.

- `@latexify` simply latexifies the expression that you provide to it, similar to `latexify(:(...))`.
- `@latexrun` both executes and latexifies the given expression.

They can for example be useful for latexifying simple mathsy functions like
```julia
lstr = @latexrun f(x; y=2) = x/y
```

## External rendering
While LaTeXStrings already render nicely in many IDEs or in Jupyter, they do not render in the REPL. Therefore, we provide a function `render(str)` which generates a standalone PDF using LuaLaTeX and opens that file in your default PDF viewer.

I have found the following syntax pretty useful:
```julia
latexify(:(x/y)) |> render
```

## Legacy support

Expand Down
3 changes: 2 additions & 1 deletion src/Latexify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Printf
using Formatting

export latexify, md, copy_to_clipboard, auto_display, set_default, get_default,
reset_default, @latexrecipe
reset_default, @latexrecipe, render, @latexify, @latexrun

## Allow some backwards compatibility until its time to deprecate.
export latexarray, latexalign, latexraw, latexinline, latextabular, mdtable
Expand Down Expand Up @@ -37,6 +37,7 @@ include("latexequation.jl")
include("latextabular.jl")
include("default_kwargs.jl")
include("recipes.jl")
include("macros.jl")

include("md.jl")
include("mdtable.jl")
Expand Down
17 changes: 15 additions & 2 deletions src/latexoperation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ function latexoperation(ex::Expr, prevOp::AbstractArray; cdot=true, kwargs...)
end

if ex.head == :call
return "\\mathrm{$op}\\left( $(join(args[2:end], ", ")) \\right)"
if args[2] isa String && occursin("=", args[2])
return "\\mathrm{$op}\\left( $(join(args[3:end], ", ")); $(args[2]) \\right)"
else
return "\\mathrm{$op}\\left( $(join(args[2:end], ", ")) \\right)"
end
end

if ex.head == :tuple
Expand All @@ -123,9 +127,18 @@ function latexoperation(ex::Expr, prevOp::AbstractArray; cdot=true, kwargs...)
end

ex.head == Symbol("'") && return "$(args[1])'"

## Enable the parsing of kwargs in a function definition
ex.head == :kw && return "$(args[1]) = $(args[2])"
ex.head == :parameters && return join(args, ", ")

## Use the last expression in a block.
## This is somewhat shady but it helps with latexifying functions.
ex.head == :block && return args[end]

## if we have reached this far without a return, then error.
error("Latexify.jl's latexoperation does not know what to do with one of the
operators in your expression ($op).")
expressions provides ($ex).")
return ""
end

Expand Down
13 changes: 13 additions & 0 deletions src/macros.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
macro latexrun(expr)
return quote
$(esc(expr))
latexify($(string(expr)))
end
end


macro latexify(expr)
return quote
latexify($(string(expr)))
end
end
40 changes: 40 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,43 @@ function add_brackets(ex::Expr, vars)
ex = postwalk(x -> x in vars ? "\\left[ $(convertSubscript(x)) \\right]" : x, ex)
return ex
end


"""
render(::LaTeXString; debug=false, name=tempname(), command="\\Large")
Display a standalone PDF with the given input.
"""
function render(s::LaTeXString; debug=false, name=tempname(), command="\\Large")
doc = """
\\documentclass[varwidth]{standalone}
\\usepackage{amssymb}
\\usepackage{amsmath}
\\begin{document}
{
$command
$s
}
\\end{document}
"""
doc = replace(doc, "\\begin{align}"=>"\\[\n\\begin{aligned}")
doc = replace(doc, "\\end{align}"=>"\\end{aligned}\n\\]")
open("$(name).tex", "w") do f
write(f, doc)
end
cd(dirname(name)) do
cmd = `lualatex $(name).tex`
debug || (cmd = pipeline(cmd, devnull))
run(cmd)
end
if Sys.iswindows()
run(`cmd /c "start $(name).pdf"`, wait=false)
elseif Sys.islinux()
run(`xdg-open $(name).pdf`, wait=false)
elseif Sys.isapple()
run(`open $(name).pdf`, wait=false)
elseif Sys.isbsd()
run(`xdg-open $(name).pdf`, wait=false)
end
return nothing
end
10 changes: 10 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
l = @latexify dummyfunc(x; y=1, z=3) = x^2/y + z
@test l == raw"$\mathrm{dummyfunc}\left( x; y = 1, z = 3 \right) = \frac{x^{2}}{y} + z$"

@test_throws UndefVarError dummyfunc(1.)

l2 = @latexrun dummyfunc2(x; y=1, z=3) = x^2/y + z
@test l2 == raw"$\mathrm{dummyfunc2}\left( x; y = 1, z = 3 \right) = \frac{x^{2}}{y} + z$"

@test dummyfunc2(1.) == 4

1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using Test

# Run tests

@testset "macro test" begin include("macros.jl") end
@testset "recipe test" begin include("recipe_test.jl") end
@testset "latexify tests" begin include("latexify_test.jl") end
@testset "latexraw tests" begin include("latexraw_test.jl") end
Expand Down

2 comments on commit 8205f13

@korsbo
Copy link
Owner Author

@korsbo korsbo commented on 8205f13 Jun 5, 2020

Choose a reason for hiding this comment

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

@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/15899

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.13.3 -m "<description of version>" 8205f1393a69f8d13a401b863bafaf3a3319e60b
git push origin v0.13.3

Please sign in to comment.