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 committed Feb 16, 2024
1 parent 6b869c3 commit c77f093
Show file tree
Hide file tree
Showing 5 changed files with 24 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(sig_digits::Int) = StyledNumberFormatter("%.$(significant_digits)g")

Check warning on line 25 in src/numberformatters.jl

View check run for this annotation

Codecov / codecov/patch

src/numberformatters.jl#L25

Added line #L25 was not covered by tests

(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)))}"

0 comments on commit c77f093

Please sign in to comment.