Skip to content

Commit

Permalink
Merge pull request #325 from hersle/mult_symbol
Browse files Browse the repository at this point in the history
Replace boolean cdot keyword argument in favor of more flexible mult_symbol string
  • Loading branch information
gustaphe authored Dec 20, 2024
2 parents 927c39e + e88194d commit a811c57
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
2 changes: 1 addition & 1 deletion docs/src/table_generator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
2 changes: 1 addition & 1 deletion src/default_kwargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
12 changes: 9 additions & 3 deletions src/latexoperation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ 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)

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]))

Expand Down Expand Up @@ -45,7 +51,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

Expand Down
30 changes: 18 additions & 12 deletions test/cdot_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -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$ |
Expand All @@ -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$ |
Expand All @@ -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")
14 changes: 7 additions & 7 deletions test/latexify_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}()
Expand Down

0 comments on commit a811c57

Please sign in to comment.