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

Customize variable Latex display #1385

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hersle
Copy link
Contributor

@hersle hersle commented Dec 13, 2024

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 \mathtt to 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)

@codecov-commenter
Copy link

codecov-commenter commented Dec 13, 2024

⚠️ Please install the 'codecov app svg image' 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 ⚠️

❗ 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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ChrisRackauckas
Copy link
Member

@jpfairbanks does something like this cover your needs?

@jpfairbanks
Copy link

This is definitely an improvement to allow specifying custom latex for printing symbols. The other two issues are:

  1. We want to truncate namespaces in compressed printing mode

  2. The subscript of a function can depend on the argument types so rendering of a term needs access to the types of the subterms.

@benneti
Copy link
Contributor

benneti commented Dec 17, 2024

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"?

@hersle
Copy link
Contributor Author

hersle commented Dec 17, 2024

  1. 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.

  1. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants