Symbolics.jl
Symbolics.jl copied to clipboard
Customize variable Latex display
I propose this to (optionally) let users customize how variables are displayed in Latex.
- Default behavior is the same as before (untouched single-letter variables, and
\mathtt-ed multi-letter variables). - With
@variables x0 [latex = "x_0"], the default rendering is overridden. - If a variable is namespaced, the customization only applies to the part of the variable name after the final dot. The system name still gets
\mathttto be similar to the default. This is to make it compatible with hierarchical MTK models.
Example:
using ModelingToolkit, Latexify
using ModelingToolkit: t_nounits as t
@variables xa(t) xb(t) [latex = "x_b"]
@named sys = ODESystem([xa ~ 0, xb ~ 0], t)
latexify(xa) # \mathtt{xa}\left( t \right)
latexify(xb) # x_{b}\left( t \right)
latexify(sys.xa) # \mathtt{sys.xa}\left( t \right)
latexify(sys.xb) # \mathtt{sys.}x_{b}\left( t \right)
:warning: Please install the to ensure uploads and comments are reliably processed by Codecov.
Codecov Report
Attention: Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.
Project coverage is 79.72%. Comparing base (
6202fa4) to head (82d9eac). Report is 12 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/variable.jl | 0.00% | 2 Missing :warning: |
:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@ Coverage Diff @@
## master #1385 +/- ##
===========================================
+ Coverage 3.95% 79.72% +75.76%
===========================================
Files 51 51
Lines 4824 4917 +93
===========================================
+ Hits 191 3920 +3729
+ Misses 4633 997 -3636
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
@jpfairbanks does something like this cover your needs?
This is definitely an improvement to allow specifying custom latex for printing symbols. The other two issues are:
-
We want to truncate namespaces in compressed printing mode
-
The subscript of a function can depend on the argument types so rendering of a term needs access to the types of the subterms.
Is there a reason not to use the https://juliapy.github.io/SymPy.jl/dev/reference/#SymPy.@syms-Tuple syntax where one can specify @syms x0::Real=>"x_0"?
- We want to truncate namespaces in compressed printing mode
I am open to suggestions on how to handle namespacing in this PR. Now it applies any custom Latex expression only to the non-namespaced part of a variable name. This is compatible with MTK without modifications to MTK, so I think it is a simple first step.
An alternative could be for Symbolics to not make this distinction, but simply apply the custom Latex expression to the whole variable name. Maybe that is more natural as a pure Symbolics modification, but MTK would require changes to preserve the custom Latex expression when namespaces are prepended to variables. This "joining process" could be a natural place for MTK systems to hook into with a custom Latex expression for their namespace. Maybe it could work like
@variables x0 [latex = "x_0"]
@named sys = NonlinearSystem([x0 ~ 0]; latex = raw"\mathtt{custom}")
latexify(sys.x0) # should display as \mathtt{custom}.x_0
and one could pass latex = "" to truncate the system namespace. I don't have capacity to work this into a very complex system, though.
- The subscript of a function can depend on the argument types so rendering of a term needs access to the types of the subterms.
I think this requires more substantial modifications to the Latex pipeline. But I don't think the changes in this PR would conflict with that in the future.
Is there a reason not to use the https://juliapy.github.io/SymPy.jl/dev/reference/#SymPy.@syms-Tuple syntax where one can specify @syms x0::Real=>"x_0"?
I went with @variables x0 [latex = "x_0"] because it is the most natural extension from all other Symbolics variable options.