Skip to content

Commit

Permalink
Let user hook into variable name Latexifying
Browse files Browse the repository at this point in the history
  • Loading branch information
hersle committed Nov 6, 2024
1 parent 8998fa8 commit cfd3689
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ include("extra_functions.jl")
using Latexify
using LaTeXStrings
include("latexify_recipes.jl")
export latexify_variable

using RecipesBase
include("plot_recipes.jl")
Expand Down
47 changes: 41 additions & 6 deletions src/latexify_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,46 @@ function latexify_derivatives(ex)
end
end

@doc raw"""
latexify_variable(name)
Transform the LaTeX expression of a Symbolics variable name `name` before display.
Symbolics calls this method internally.
Redefine it to customize how variables are shown in a LaTeX environment.
Example
=======
```
julia> using Symbolics, Latexify
julia> @variables car₊wheel₊ω
1-element Vector{Num}:
car₊wheel₊ω
julia> latexify(car₊wheel₊ω)
L"\begin{equation}
car.wheel.\omega
\end{equation}
"
julia> Symbolics.latexify_variable(name) = "\\mathrm{$name}"
julia> latexify(car₊wheel₊ω)
L"\begin{equation}
\mathrm{$name}
\end{equation}
"
```
"""
latexify_variable(name) = name

# Internal dispatch used in _toexpr()
function _latexify_variable(name::Symbol)
name = replace(String(name), NAMESPACE_SEPARATOR => ".") # always do this
name = latexify_variable(name) # do whatever the user wants
return Symbol(name) # always convert back to Symbol
end

recipe(n) = latexify_derivatives(cleanup_exprs(_toexpr(n)))

@latexrecipe function f(n::Num)
Expand Down Expand Up @@ -192,12 +232,7 @@ function _toexpr(O)
end
end
if issym(O)
sym = string(nameof(O))
sym = replace(sym, NAMESPACE_SEPARATOR => ".")
if length(sym) > 1
sym = string("\\mathtt{", sym, "}")
end
return Symbol(sym)
return _latexify_variable(nameof(O))
end
!iscall(O) && return O

Expand Down

0 comments on commit cfd3689

Please sign in to comment.