Skip to content

Commit

Permalink
Merge pull request #316 from gustaphe/double_index
Browse files Browse the repository at this point in the history
Fix doubly-indexed arrays
  • Loading branch information
gustaphe authored Oct 15, 2024
2 parents c7ac169 + ff1b3c2 commit ad0ab40
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/latexoperation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,15 @@ function latexoperation(ex::Expr, prevOp::AbstractArray; kwargs...)::String

if ex.head == :ref
if index == :subscript
return "$(args[1])_{$(join(args[2:end], ","))}"
if prevOp[1] == :ref
container = "\\left( $(op) \\right)"
else
container = opname
end
return "$(container)_{$(join(args[2:end], ","))}"
elseif index == :bracket
argstring = join(args[2:end], ", ")
prevOp[1] == :ref && return "$op\\left[$argstring\\right]"
return "$opname\\left[$argstring\\right]"
else
throw(ArgumentError("Incorrect `index` keyword argument to latexify. Valid values are :subscript and :bracket"))
Expand Down Expand Up @@ -264,7 +270,7 @@ function convert_subscript!(ex::Expr, kwargs...)
end

function convert_subscript(str::String; snakecase=false, function_name=false, kwargs...)
subscript_list = split(str, "_")
subscript_list = split(str, r"\\?_")
if snakecase
return join(subscript_list, "\\_")
else
Expand Down
8 changes: 3 additions & 5 deletions src/latexraw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ function _latexraw(inputex::Expr; convert_unicode=true, kwargs...)

recurseexp!(lstr::LaTeXString) = lstr.s
function recurseexp!(ex)
prevOp = Vector{Symbol}(undef, length(ex.args))
fill!(prevOp, :none)
prevOp = fill(:none, length(ex.args))
if Meta.isexpr(ex, :call) && ex.args[1] in (:sum, :prod) && Meta.isexpr(ex.args[2], :generator)
op = ex.args[1]
term = latexraw(ex.args[2].args[1])
Expand Down Expand Up @@ -184,9 +183,8 @@ Check if `x` represents something that could affect the vector of previous opera
"""
function _getoperation(ex::Expr)
ex.head == :comparison && return :comparison
if ex.head != :call
return :none
end
ex.head == :ref && return :ref
ex.head == :call || return :none
if length(ex.args) > 1 && (op = ex.args[1]) isa Symbol
if length(ex.args) == 2
# These are unary operators
Expand Down
3 changes: 3 additions & 0 deletions test/latexraw_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ raw"$a = \left[

@test latexify(:(u[1, 2]); index = :bracket) == raw"$u\left[1, 2\right]$"
@test latexify(:(u[1, 2]); index = :subscript) == raw"$u_{1,2}$"
@test latexify(:(u[1][1]); index = :bracket) == raw"$u\left[1\right]\left[1\right]$"
@test latexify(:(u[1][1]); index = :subscript) == raw"$\left( u_{1} \right)_{1}$"
@test latexify(:(u_x[1][1]); index=:subscript, snakecase=true) == raw"$\left( u\_x_{1} \right)_{1}$"

array_test = [ex, str]
@test all(latexraw.(array_test) .== desired_output)
Expand Down

0 comments on commit ad0ab40

Please sign in to comment.