Skip to content

Commit

Permalink
Fix potential bugs by switching from Formatting.jl to Format.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones authored and korsbo committed Mar 3, 2024
1 parent 6b869c3 commit a7ee6f5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 40 deletions.
7 changes: 3 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ 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.16.1"
version = "0.16.2"

[deps]
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[weakdeps]
Expand All @@ -23,7 +22,7 @@ DataFramesExt = "DataFrames"
SymEngineExt = "SymEngine"

[compat]
Formatting = "0.4"
Format = "1.3"
LaTeXStrings = "0.3, 1"
MacroTools = "0.4 - 0.5"
OrderedCollections = "1"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ latexify([12893.1 1.328e2; "x/y" 7832//2378]; fmt=FancyNumberFormatter(3))
```

```@example main
using Formatting
using Format
latexify([12893.1 1.328e2]; fmt=x->format(round(x, sigdigits=2), autoscale=:metric))
```

Expand Down
3 changes: 1 addition & 2 deletions src/Latexify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ using InteractiveUtils
using Markdown
using MacroTools: postwalk
import MacroTools
using Printf
using Formatting
using Format

export latexify, md, copy_to_clipboard, auto_display, set_default, get_default,
reset_default, @latexrecipe, render, @latexify, @latexrun, @latexdefine
Expand Down
50 changes: 18 additions & 32 deletions src/numberformatters.jl
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
abstract type AbstractNumberFormatter end

(::AbstractNumberFormatter)(x) = string(x)

const float_regex = r"(?'mantissa'(?'before_dp'(?'sign'-?)(?'before_dp_nosign'\d+))(\.(?'after_dp'\d+))?)(?'e_or_E'e)(?'raw_exp'(?'sign_exp'-?)\+?0*(?'mag_exp'\d+))"i

struct PlainNumberFormatter <: AbstractNumberFormatter end

(::PlainNumberFormatter)(x) = string(x)


struct PrintfNumberFormatter <: AbstractNumberFormatter
fmt::String
f::Function

PrintfNumberFormatter(fmt::String) = new(fmt, Format.generate_formatter(fmt))
end
PrintfNumberFormatter(fmt::String) = PrintfNumberFormatter(fmt, Formatting.generate_formatter(fmt))
(f::PrintfNumberFormatter)(x) = f.f(x)

(f::PrintfNumberFormatter)(x) = f.f(x)

struct StyledNumberFormatter <: AbstractNumberFormatter
fmt::String
f::Function

StyledNumberFormatter(fmt::String="%.4g") = new(fmt)

end

function StyledNumberFormatter(significant_digits::Int)
return StyledNumberFormatter("%.$(significant_digits)g")
StyledNumberFormatter(fmt::String="%.4g") = new(fmt, Format.generate_formatter(fmt))
end

(f::StyledNumberFormatter)(x) = string(x)
function (f::StyledNumberFormatter)(x::AbstractFloat)
s = @eval @sprintf $(f.fmt) $x
return replace(s, float_regex => s"\g<mantissa> \\mathrm{\g<e_or_E>}{\g<sign_exp>\g<mag_exp>}")
end
StyledNumberFormatter(significant_digits::Int) = StyledNumberFormatter("%.$(significant_digits)g")

(f::StyledNumberFormatter)(x::AbstractFloat) =
replace(f.f(x), float_regex => s"\g<mantissa> \\mathrm{\g<e_or_E>}{\g<sign_exp>\g<mag_exp>}")
(f::StyledNumberFormatter)(x::Unsigned) = "\\mathtt{0x$(string(x; base=16, pad=2sizeof(x)))}"


struct FancyNumberFormatter <: AbstractNumberFormatter
fmt::String
f::Function
exponent_format::SubstitutionString

function FancyNumberFormatter(fmt::String="%.4g",
exponent_format::SubstitutionString=s"\g<mantissa> \\cdot 10^{\g<sign_exp>\g<mag_exp>}")
return new(fmt, exponent_format)
end
FancyNumberFormatter(fmt::String="%.4g",
exponent_format::SubstitutionString=s"\g<mantissa> \\cdot 10^{\g<sign_exp>\g<mag_exp>}") = new(fmt, Format.generate_formatter(fmt), exponent_format)
end

function FancyNumberFormatter(fmt::String, mult_symbol)
return FancyNumberFormatter(fmt, SubstitutionString("\\g<mantissa> $(escape_string(mult_symbol)) 10^{\\g<sign_exp>\\g<mag_exp>}"))
end
function FancyNumberFormatter(significant_digits, mult_symbol="\\cdot")
return FancyNumberFormatter("%.$(significant_digits)g", mult_symbol)
end
FancyNumberFormatter(fmt::String, mult_symbol) =
FancyNumberFormatter(fmt, SubstitutionString("\\g<mantissa> $(escape_string(mult_symbol)) 10^{\\g<sign_exp>\\g<mag_exp>}"))
FancyNumberFormatter(significant_digits, mult_symbol="\\cdot") =
FancyNumberFormatter("%.$(significant_digits)g", mult_symbol)

(f::FancyNumberFormatter)(x) = string(x)

function (f::FancyNumberFormatter)(x::AbstractFloat)
s = @eval @sprintf $(f.fmt) $x
return replace(s, float_regex => f.exponent_format)
end

(f::FancyNumberFormatter)(x::AbstractFloat) = replace(f.f(x), float_regex => f.exponent_format)
(f::FancyNumberFormatter)(x::Unsigned) = "\\mathtt{0x$(string(x; base=16, pad=2sizeof(x)))}"
1 change: 1 addition & 0 deletions test/numberformatters_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ x = -23.4728979e7
@test FancyNumberFormatter()(x) == "-2.347 \\cdot 10^{8}"
@test FancyNumberFormatter("%.5E", s"\g<mantissa>,\g<before_dp>,\g<sign>,\g<before_dp_nosign>,\g<after_dp>,\g<e_or_E>,\g<raw_exp>,\g<sign_exp>,\g<mag_exp>")(x) == "-2.34729,-2,-,2,34729,E,+08,,8"

@test StyledNumberFormatter(4) == StyledNumberFormatter()

xne = -23.4728979e-7

Expand Down

2 comments on commit a7ee6f5

@korsbo
Copy link
Owner

@korsbo korsbo commented on a7ee6f5 Mar 3, 2024

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/102151

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.16.2 -m "<description of version>" a7ee6f5ca912a1e82f3c0679ac22fd4d2a782d71
git push origin v0.16.2

Please sign in to comment.