Skip to content
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: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jobs:
fail-fast: false
matrix:
version:
- '0.7'
- '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia.
- 'nightly'
os:
Expand Down Expand Up @@ -66,4 +65,4 @@ jobs:
- run: julia --project=docs docs/make.jl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }}
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.15.14"
version = "0.15.15"

[deps]
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
Expand Down
12 changes: 12 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ x = 0.5
"
```

A special keyword `post` can be supplied to `@latexdefine`, which is a
function that will be called on the final right hand sign before
latexification. This is merely formatting and will not affect any assignments.

```julia
julia> @latexdefine x=π post=round
L"$x = \pi = 3.0$"

julia> @latexdefine x
L"$x = \pi$"
```

## 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.

Expand Down
19 changes: 18 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ end

Latexify `expression`, followed by an equals sign and the return value of its evaluation.
Any side effects of the expression, like assignments, are evaluated as well.
The RHS can be formatted or otherwise transformed by supplying a function as kwarg `post`.

# Examples
```julia-repl
Expand All @@ -73,16 +74,32 @@ y = \\frac{3}{2} + 1.5 = 3.0

julia> y
3.0

julia> @latexdefine y=π post=round
L"\$x = \\pi = 3.0\$"
```
See also [`@latexify`](@ref), [`@latexrun`](@ref).
"""
macro latexdefine(expr, kwargs...)
params = _extractparam.(kwargs)
post = :identity
for param in params
if param === :post
post = :post
break
end
if param isa Expr && param.args[1] === :post
post = param.args[2]
break
end
end

return esc(
Expr(
:call,
:latexify,
Expr(:parameters, _extractparam.(kwargs)...),
Expr(:call, :Expr, QuoteNode(:(=)), Meta.quot(expr), _executable(expr)),
Expr(:call, :Expr, QuoteNode(:(=)), Meta.quot(expr), Expr(:call, post, _executable(expr))),
),
)
end
Expand Down
11 changes: 9 additions & 2 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ l12 = @latexrun x = 1 env=:raw
l13 = @latexdefine y = x env=:raw
@test l13 == raw"y = x = 1"

#= # Loading a file with this in it doesn't work on VERSION < v"1.5.0"
env = :raw
l14 = @latexdefine y env
@test l14 == raw"y = 1"
=#

l15 = @latexdefine y = x post=float
@test l15 == raw"$y = x = 1.0$"
@test y isa Integer

post = x->round(x; sigdigits=3)
l16 = @latexdefine y = π post
@test l16 == raw"$y = \pi = 3.14$"
@test y == π

@test latexify(:(@hi(x / y))) == replace(
raw"$\mathrm{@hi}\left( \frac{x}{y} \right)$", "\r\n"=>"\n")