Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix doubly-indexed arrays #316

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading