Skip to content

Commit 3351502

Browse files
committed
Override default variable latexification with custom [latex = ...] field
1 parent 74756eb commit 3351502

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/latexify_recipes.jl

+21-8
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ recipe(n) = latexify_derivatives(cleanup_exprs(_toexpr(n)))
5252
cdot --> false
5353
fmt --> FancyNumberFormatter(5)
5454
index --> :subscript
55-
snakecase --> true
56-
safescripts --> true
55+
56+
# Prevent Latexify.jl from interfering if variable has custom [latex = ...] field (it should escape its own underscores etc.)
57+
custom = unwrap(n) isa Symbolic && hasmetadata(unwrap(n), VariableLatex)
58+
snakecase --> !custom
59+
safescripts --> !custom
60+
function_name --> false
5761

5862
return recipe(n)
5963
end
@@ -191,13 +195,22 @@ function _toexpr(O)
191195
return frac_expr
192196
end
193197
end
194-
if issym(O)
195-
sym = string(nameof(O))
196-
sym = replace(sym, NAMESPACE_SEPARATOR => ".")
197-
if length(sym) > 1
198-
sym = string("\\mathtt{", sym, "}")
198+
if issym(O)
199+
name = string(nameof(O))
200+
201+
# If given, use custom [latex = ...] option for the variable name (does not affect subsystem names)
202+
# Otherwise fall back to a sane default, where multi-letter variables are rendered with a fixed width font
203+
name = replace(name, NAMESPACE_SEPARATOR => '.')
204+
if hasmetadata(O, VariableLatex)
205+
seppos = findlast('.', name)
206+
sysname = isnothing(seppos) ? "" : "\\mathtt{$(name[begin:seppos])}" # always \mathtt system names (consistent with multi-letter vars)
207+
varname = getmetadata(O, VariableLatex) # override
208+
name = string(sysname, varname)
209+
elseif length(name) > 1
210+
name = "\\mathtt{$name}"
199211
end
200-
return Symbol(sym)
212+
213+
return Symbol(name)
201214
end
202215
!iscall(O) && return O
203216

src/variable.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const IndexMap = Dict{Char,Char}(
1717
abstract type AbstractVariableMetadata end
1818
struct VariableDefaultValue <: AbstractVariableMetadata end
1919
struct VariableSource <: AbstractVariableMetadata end
20+
struct VariableLatex <: AbstractVariableMetadata end
2021

2122
function recurse_and_apply(f, x)
2223
if symtype(x) <: AbstractArray
@@ -252,9 +253,8 @@ function construct_vars(macroname, v, type, call_args, val, prop, transform, isr
252253
lhs, :($lhs = $rhs)
253254
end
254255

255-
function option_to_metadata_type(::Val{opt}) where {opt}
256-
throw(Base.Meta.ParseError("unknown property type $opt"))
257-
end
256+
option_to_metadata_type(::Val{:latex}) = VariableLatex
257+
option_to_metadata_type(::Val{opt}) where {opt} = throw(Base.Meta.ParseError("unknown property type $opt"))
258258

259259
function setprops_expr(expr, props, macroname, varname)
260260
expr = :($setmetadata($expr, $VariableSource, ($(Meta.quot(macroname)), $varname,)))

0 commit comments

Comments
 (0)