Skip to content

Commit

Permalink
Add other per-modes
Browse files Browse the repository at this point in the history
  • Loading branch information
gustaphe committed Apr 10, 2021
1 parent 93f5847 commit 98ab4ca
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UnitfulLatexify"
uuid = "45397f5d-5981-4c77-b2b3-fc36d6e9b728"
authors = ["David Gustavsson <[email protected]> and contributors"]
version = "1.4.1"
version = "1.5.0"

[deps]
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Expand Down
8 changes: 8 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ plot(
)
```

## Per-modes
In mathrm-mode, one might prefer ``\mathrm{J}\,/\,\mathrm{kg}`` or
``\frac{\mathrm{J}}{\mathrm{kg}}`` over ``\mathrm{J}\,\mathrm{kg}^{-1}``. This can be achieved by
supplying `permode=:slash` or `permode=:frac` respectively.

These will have no effect in `siunitx` mode, because the latex package handles
this for you, and you can set it in your document.

## A more complete list of defined units
Below is a poorly scraped list of units defined in `siunitx` and what comes out
if you run it through `latexify`. Feel free to create an issue if there's a
Expand Down
30 changes: 24 additions & 6 deletions src/default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
if pow == 1//1
expo = ""
else
expo = "^{$(latexify(pow;kwargs...,fmt="%g",env=:raw))}"
expo = "^{$(latexify(pow; kwargs..., fmt="%g", env=:raw))}"
end
return LaTeXString("\\mathrm{$prefix$unitname}$expo")
end
env --> :raw
if unitformat === :siunitx
per = pow < 0 ? "\\per" : ""
pow = abs(pow)
expo = pow == 1//1 ? "" : "\\tothe{$(latexify(pow;kwargs...,fmt="%g",env=:raw))}"
expo = pow == 1//1 ? "" : "\\tothe{$(latexify(pow; kwargs..., fmt="%g", env=:raw))}"
else
per = ""
expo = pow == 1//1 ? "" : "^{$(latexify(pow;kwargs...,fmt="%g",env=:raw))}"
expo = pow == 1//1 ? "" : "^{$(latexify(pow; kwargs..., fmt="%g", env=:raw))}"
end
return LaTeXString("$per$prefix$unitname$expo")
end

@latexrecipe function f(u::T; unitformat=:mathrm) where {T<:Units}
@latexrecipe function f(u::T; unitformat=:mathrm, permode=:power) where {T<:Units}
if unitformat === :mathrm
env --> :inline
return Expr(:latexifymerge, NakedUnits(u))
Expand All @@ -50,8 +50,26 @@ end
struct NakedUnits
u::Units
end
@latexrecipe function f(u::T; unitformat=:mathrm) where {T<:NakedUnits}
return Expr(:latexifymerge, intersperse(listunits(u.u), delimiters[unitformat])...)

@latexrecipe function f(u::T; unitformat=:mathrm, permode=:power) where {T<:NakedUnits}
unitlist = listunits(u.u)
if unitformat in (:siunitx, :siunitxsimple) || permode === :power
return Expr(:latexifymerge, intersperse(unitlist, delimiters[unitformat])...)
end

numunits = [x for x in unitlist if power(x) >= 0]
denunits = [typeof(x)(tens(x), -power(x)) for x in unitlist if power(x) < 0]

numerator = intersperse(numunits, delimiters[unitformat])
denominator = intersperse(denunits, delimiters[unitformat])

if permode === :slash
return Expr(:latexifymerge, numerator..., "\\,/\\,", denominator...)
end
if permode === :frac
return Expr(:latexifymerge, "\\frac{", numerator..., "}{", denominator..., "}")
end
return error("permode $permode undefined.")
end

const delimiters = Dict{Symbol,String}(
Expand Down
36 changes: 28 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function unitfullatexifytest(val, mathrmexpected, siunitxexpected, siunitxsimple
@test latexify(val; unitformat=:siunitxsimple) == LaTeXString(siunitxsimpleexpected)
end

@testset "UnitfulLatexify.jl" begin
@testset "Latexify units" begin
unitfullatexifytest(
u"H*J/kg",
raw"$\mathrm{H}\,\mathrm{J}\,\mathrm{kg}^{-1}$",
Expand Down Expand Up @@ -75,13 +75,33 @@ end
)
@test latexify(24.7e9u"Gm/s^2"; fmt="%.1e") ==
L"$2.5e+10\;\mathrm{Gm}\,\mathrm{s}^{-2}$"
@testset "Labels" begin
@test latexslashunitlabel("x", u"m") == "\$x\\;/\\;\\mathrm{m}\$"
@test latexroundunitlabel("x", u"m") == "\$x\\;\\left(\\mathrm{m}\\right)\$"
@test latexsquareunitlabel("x", u"m") == "\$x\\;\\left[\\mathrm{m}\\right]\$"
@test latexfracunitlabel("x", u"m") == "\$\\frac{x}{\\mathrm{m}}\$"
@test latexify("x", u"m") == "\$x\\;/\\;\\mathrm{m}\$"
end
end

@testset "permode" begin
p = 5u"m^3*s^2/H/kg^4"
@test latexify(p) == LaTeXString(
raw"$5\;\mathrm{m}^{3}\,\mathrm{s}^{2}\,\mathrm{kg}^{-4}\,\mathrm{H}^{-1}$"
)
@test latexify(p; permode=:power) == LaTeXString(
raw"$5\;\mathrm{m}^{3}\,\mathrm{s}^{2}\,\mathrm{kg}^{-4}\,\mathrm{H}^{-1}$"
)
@test latexify(p; permode=:slash) == LaTeXString(
raw"$5\;\mathrm{m}^{3}\,\mathrm{s}^{2}\,/\,\mathrm{kg}^{4}\,\mathrm{H}$"
)
@test latexify(p; permode=:frac) == LaTeXString(
raw"$5\;\frac{\mathrm{m}^{3}\,\mathrm{s}^{2}}{\mathrm{kg}^{4}\,\mathrm{H}}$"
)
@test latexify(p; unitformat=:siunitx, permode=:frac) ==
latexify(p; unitformat=:siunitx)
@test_throws ErrorException latexify(p; permode=:wrogn)
end

@testset "Labels" begin
@test latexslashunitlabel("x", u"m") == "\$x\\;/\\;\\mathrm{m}\$"
@test latexroundunitlabel("x", u"m") == "\$x\\;\\left(\\mathrm{m}\\right)\$"
@test latexsquareunitlabel("x", u"m") == "\$x\\;\\left[\\mathrm{m}\\right]\$"
@test latexfracunitlabel("x", u"m") == "\$\\frac{x}{\\mathrm{m}}\$"
@test latexify("x", u"m") == "\$x\\;/\\;\\mathrm{m}\$"
end

format(UnitfulLatexify; overwrite=false) || @warn """
Expand Down

2 comments on commit 98ab4ca

@gustaphe
Copy link
Owner 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/33985

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 v1.5.0 -m "<description of version>" 98ab4ca410586fd4852520a535fb28a2562bf13e
git push origin v1.5.0

Please sign in to comment.