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

Let user hook into variable Latexifying #1350

Closed
wants to merge 1 commit into from
Closed
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
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
Loading