From 222ac88e3d2e0549b159bbe60c2aa46ce3eaac28 Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 18 Dec 2024 21:15:43 +0100 Subject: [PATCH 1/4] Replace cdot with mult_symbol keyword argument --- src/latexoperation.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/latexoperation.jl b/src/latexoperation.jl index 002fc8b2..d00fa219 100644 --- a/src/latexoperation.jl +++ b/src/latexoperation.jl @@ -7,11 +7,11 @@ a parenthesis is needed. """ function latexoperation(ex::Expr, prevOp::AbstractArray; kwargs...)::String - # If we used `cdot` and `index` as keyword arguments before `kwargs...` + # If we used `mult_symbol` and `index` as keyword arguments before `kwargs...` # and they are indeed contained in `kwargs`, they would get lost when # passing `kwargs...` to `latexraw`below. Thus, we need to set default # values as follows. - cdot = get(kwargs, :cdot, true) + mult_symbol = get(kwargs, :mult_symbol, "\\cdot") index = get(kwargs, :index, :bracket) op = ex.args[1] @@ -45,7 +45,7 @@ function latexoperation(ex::Expr, prevOp::AbstractArray; kwargs...)::String arg = args[i] (precedence(prevOp[i]) < precedence(op) || (ex.args[i] isa Complex && !iszero(ex.args[i].re))) && (arg = "\\left( $arg \\right)") str = string(str, arg) - i == length(args) || (str *= cdot ? " \\cdot " : " ") + i == length(args) || (str *= mult_symbol == "" ? " " : " $mult_symbol ") end return str From 50dbca3b891c2369a67a10be51bfd9132702882b Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 18 Dec 2024 21:43:50 +0100 Subject: [PATCH 2/4] Update tests to set mult_symbol instead of cdot --- test/cdot_test.jl | 24 ++++++++++++------------ test/latexify_test.jl | 14 +++++++------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/test/cdot_test.jl b/test/cdot_test.jl index f3a48263..b78f88e8 100644 --- a/test/cdot_test.jl +++ b/test/cdot_test.jl @@ -4,29 +4,29 @@ using Test #inline -@test latexify(:(x * y); env=:inline, cdot=false) == raw"$x y$" +@test latexify(:(x * y); env=:inline, mult_symbol="") == raw"$x y$" -@test latexify(:(x * y); env=:inline, cdot=true) == raw"$x \cdot y$" +@test latexify(:(x * y); env=:inline, mult_symbol="\\cdot") == raw"$x \cdot y$" -@test latexify(:(x*(y+z)*y*(z+a)*(z+b)); env=:inline, cdot=false) == +@test latexify(:(x*(y+z)*y*(z+a)*(z+b)); env=:inline, mult_symbol="") == raw"$x \left( y + z \right) y \left( z + a \right) \left( z + b \right)$" -@test latexify(:(x*(y+z)*y*(z+a)*(z+b)); env=:inline, cdot=true) == +@test latexify(:(x*(y+z)*y*(z+a)*(z+b)); env=:inline, mult_symbol="\\cdot") == raw"$x \cdot \left( y + z \right) \cdot y \cdot \left( z + a \right) \cdot \left( z + b \right)$" # raw -@test latexify(:(x * y); env=:raw, cdot=false) == raw"x y" +@test latexify(:(x * y); env=:raw, mult_symbol="") == raw"x y" -@test latexify(:(x * y); env=:raw, cdot=true) == raw"x \cdot y" +@test latexify(:(x * y); env=:raw, mult_symbol="\\cdot") == raw"x \cdot y" -@test latexify(:(x * (y + z) * y * (z + a) * (z + b)); env=:raw, cdot=false) == +@test latexify(:(x * (y + z) * y * (z + a) * (z + b)); env=:raw, mult_symbol="") == raw"x \left( y + z \right) y \left( z + a \right) \left( z + b \right)" -@test latexify(:(x * (y + z) * y * (z + a) * (z + b)); env=:raw, cdot=true) == +@test latexify(:(x * (y + z) * y * (z + a) * (z + b)); env=:raw, mult_symbol="\\cdot") == raw"x \cdot \left( y + z \right) \cdot y \cdot \left( z + a \right) \cdot \left( z + b \right)" # array -@test latexify( [:(x*y), :(x*(y+z)*y*(z+a)*(z+b))]; env=:equation, transpose=true, cdot=false) == replace( +@test latexify( [:(x*y), :(x*(y+z)*y*(z+a)*(z+b))]; env=:equation, transpose=true, mult_symbol="") == replace( raw"\begin{equation} \left[ \begin{array}{cc} @@ -36,7 +36,7 @@ x y & x \left( y + z \right) y \left( z + a \right) \left( z + b \right) \\ \end{equation} ", "\r\n"=>"\n") -@test latexify( [:(x*y), :(x*(y+z)*y*(z+a)*(z+b))]; env=:equation, transpose=true, cdot=true) == replace( +@test latexify( [:(x*y), :(x*(y+z)*y*(z+a)*(z+b))]; env=:equation, transpose=true, mult_symbol="\\cdot") == replace( raw"\begin{equation} \left[ \begin{array}{cc} @@ -52,7 +52,7 @@ x \cdot y & x \cdot \left( y + z \right) \cdot y \cdot \left( z + a \right) \cdo # mdtable arr = ["x*(y-1)", 1.0, 3*2, :(x-2y), :symb] -@test latexify(arr; env=:mdtable, cdot=false) == +@test latexify(arr; env=:mdtable, mult_symbol="") == Markdown.md"| $x \left( y - 1 \right)$ | | ------------------------:| | $1.0$ | @@ -61,7 +61,7 @@ Markdown.md"| $x \left( y - 1 \right)$ | | $symb$ | " -@test latexify(arr; env=:mdtable, cdot=true) == +@test latexify(arr; env=:mdtable, mult_symbol="\\cdot") == Markdown.md"| $x \cdot \left( y - 1 \right)$ | | ------------------------------:| | $1.0$ | diff --git a/test/latexify_test.jl b/test/latexify_test.jl index 1ee39b9b..74ab7c64 100644 --- a/test/latexify_test.jl +++ b/test/latexify_test.jl @@ -11,19 +11,19 @@ test_array = ["x/y * d" :x ; :( (t_sub_sub - x)^(2*p) ) 3//4 ] @test latexify("x * y") == raw"$x \cdot y$" -set_default(cdot = false) +set_default(mult_symbol = "") @test latexify("x * y") == raw"$x y$" -@test get_default() == Dict{Symbol,Any}(:cdot => false) +@test get_default() == Dict{Symbol,Any}(:mult_symbol => "") -set_default(cdot = true, transpose = true) +set_default(mult_symbol = "\\cdot", transpose = true) -@test get_default() == Dict{Symbol,Any}(:cdot => true,:transpose => true) -@test get_default(:cdot) == true -@test get_default(:cdot, :transpose) == (true, true) -@test get_default([:cdot, :transpose]) == Bool[1, 1] +@test get_default() == Dict{Symbol,Any}(:mult_symbol => "\\cdot",:transpose => true) +@test get_default(:mult_symbol) == "\\cdot" +@test get_default(:mult_symbol, :transpose) == ("\\cdot", true) +@test get_default([:mult_symbol, :transpose]) == ["\\cdot", true] reset_default() @test get_default() == Dict{Symbol,Any}() From 58c4b9527c871da4568f70ba4860b0fdb9ad1c7a Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 18 Dec 2024 21:53:19 +0100 Subject: [PATCH 3/4] Update documentation to use mult_symbol instead of cdot --- docs/src/index.md | 2 +- docs/src/table_generator.jl | 2 +- src/default_kwargs.jl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 30b29559..758600ad 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -156,7 +156,7 @@ Note that this changes Latexify.jl from within and should therefore only be used The calls are additive so that a new call with ```julia -set_default(cdot = false) +set_default(mult_symbol = "") ``` will not cancel out the changes we just made to `fmt` and `convert_unicode`. diff --git a/docs/src/table_generator.jl b/docs/src/table_generator.jl index a4f55ffe..98c28033 100644 --- a/docs/src/table_generator.jl +++ b/docs/src/table_generator.jl @@ -37,7 +37,7 @@ keyword_arguments = [ KeywordArgument(:imaginary_unit, [:mdtable, :tabular, :align, :array, :raw, :inline], "`String`", "`\"\\\\mathit{i}\"`", "The symbol to use to represent the imaginary unit", [:Any]), KeywordArgument(:escape_underscores, [:mdtable, :mdtext], "`Bool`", "`false`", "Prevent underscores from being interpreted as formatting.", [:Any]), KeywordArgument(:convert_unicode, [:mdtable, :tabular, :align, :array, :raw, :inline], "`Bool`", "`true`", "Convert unicode characters to latex commands, for example `α` to `\\alpha`", [:Any]), - KeywordArgument(:cdot, [:mdtable, :tabular, :align, :array, :raw, :inline], "`Bool`", "`true`", "Toggle between using `\\cdot` or just a space to represent multiplication.", [:Any]), + KeywordArgument(:mult_symbol, [:mdtable, :tabular, :align, :array, :raw, :inline], "`String`", "`\"\\\\cdot\"`", "Specify the symbol to use for the multiplication operator (`\"\"` for juxtaposition).", [:Any]), KeywordArgument(:symbolic, [:align], "`Bool`", "`false`", "Use symbolic calculations to clean up the expression.", [:ReactionNetwork]), KeywordArgument(:clean, [:align], "`Bool`", "`false`", "Clean out `1*` terms. Only useful for Catalyst (then named DiffEqBiological) versions 3.4 or below.", [:ReactionNetwork]), KeywordArgument(:rows, [:align], "Iterable or symol", ":all", "Which rows to include in the output.", [:Any]), diff --git a/src/default_kwargs.jl b/src/default_kwargs.jl index 8b894c84..4c28a66a 100644 --- a/src/default_kwargs.jl +++ b/src/default_kwargs.jl @@ -10,7 +10,7 @@ you call it multiple times, defaults will be added or replaced, but not reset. Example: ```julia -set_default(cdot = false, transpose = true) +set_default(mult_symbol = "", transpose = true) ``` To reset the defaults that you have set, use `reset_default`. From e88194d6724afb3acdcaf5e0a324124ed7a5596c Mon Sep 17 00:00:00 2001 From: Herman Sletmoen Date: Wed, 18 Dec 2024 22:04:57 +0100 Subject: [PATCH 4/4] Deprecate cdot; convert it to equivalent mult_symbol --- src/latexoperation.jl | 6 ++++++ test/cdot_test.jl | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/latexoperation.jl b/src/latexoperation.jl index d00fa219..b43b1956 100644 --- a/src/latexoperation.jl +++ b/src/latexoperation.jl @@ -14,6 +14,12 @@ function latexoperation(ex::Expr, prevOp::AbstractArray; kwargs...)::String mult_symbol = get(kwargs, :mult_symbol, "\\cdot") index = get(kwargs, :index, :bracket) + if haskey(kwargs, :cdot) + cdot = kwargs[:cdot] + mult_symbol = cdot ? "\\cdot" : "" + Base.depwarn("Latexify received the deprecated keyword argument cdot = $cdot and converted it to mult_symbol = \"$mult_symbol\". Pass the latter directly to remove this warning.", :latexoperation) + end + op = ex.args[1] string(op)[1] == '.' && (op = Symbol(string(op)[2:end])) diff --git a/test/cdot_test.jl b/test/cdot_test.jl index b78f88e8..e8595099 100644 --- a/test/cdot_test.jl +++ b/test/cdot_test.jl @@ -73,3 +73,9 @@ Markdown.md"| $x \cdot \left( y - 1 \right)$ | +# Deprecation of cdot = ... in favor of mult_symbol = ... +# (cdot takes precedence over mult_symbol) +@test_deprecated latexify(:(x * y); cdot=false) +@test_deprecated latexify(:(x * y); cdot=true) +@test latexify(:(x * y); mult_symbol="garbage", cdot=false) == latexify(:(x * y); mult_symbol="") +@test latexify(:(x * y); mult_symbol="garbage", cdot=true) == latexify(:(x * y); mult_symbol="\\cdot")