-
Notifications
You must be signed in to change notification settings - Fork 156
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
Use fixed width text for compound variable names #1305
Conversation
`capacitor` makes sense as code but does not make as much sense inside of latex output. When you look at it, it italics each of the letters which makes it look like its c times a times ..., which isn't quite right. Instead, this uses `\texttt{name}` in order to more clearly delineate a value which is a compound name. Using the fixed width text also looks quite "computer-y" so it has a nice connection to `verbose` and is clearly a "computer variable" in the expression.
I think this is a good change. Does it also properly render "long" unicode variable names? Like @variables δt αβ b₊Ω0 |
Nice. I think this makes perfect sense for latexify(@variables Ω₀ Ω′ Ω̇) # last one is \Omega\dot (looks better in e.g. VSCode) which currently give what I like (I would dislike these with
I'm not sure what would be a good and simple way to handle all cases 😅 |
What do you think about # ...
if issym(O)
sym = string(nameof(O))
parts = split(sym, NAMESPACE_SEPARATOR)
map!(part -> "\\texttt{$part}", parts, parts[begin:end-1]) # \texttt subsystem names
sym = join(parts, ".") # replace NAMESPACE_SEPARATOR => "."
return Symbol(sym)
end
# ... That would effectively latexify(@variables circuit₊capacitor₊C b₊Ω₀ perturbation₊δP) would give
I think this would also help distinguish subsystems from variables, and leave the variable rendering "untouched". EDIT: Maybe combined with your handling of compound unicode names etc., so compound unicode names are valid subsystem names? |
b93607b
to
a565638
Compare
I don't know about that. |
Sorry for my rambling, I realize now that your changes already handle those tricky cases well. This is much better 👍 |
I wasn't aware of this PR, but I don't think it makes much sense for Catalyst. A reaction like |
To illustrate that reaction now generates:
which is a pretty strange typesetting. |
How would you propose putting a kwarg on it? |
Can't one add kwargs to each Alternatively, in Catalyst we have a global Latex config struct to allow one to change such options. If Symbolics had something like that we could just change it when Catalyst gets loaded. Having such a struct store the wrapping tex command, and length at which to apply it, would probably be enough. This would also have the benefit of modifying printing for systems that are converted from a Catalyst reaction system (if Catalyst changes the convention once loaded). But I guess that could also be considered a drawback as the modifications Catalyst makes would propagate to non-Catalyst created/converted systems too. Alternatively, the suggestion above to only apply |
Or we could add an optional field to MTK systems through which a user can pass a printing options struct to control these kwargs when displaying the system. Then MTK could have a default that Catalyst overrides for Catalyst-generated systems, and Symbolics just needs the printing options struct and associated kwarg in the receipes. |
I like this last option, since it means Symbolics can by default not use |
No? Why would Symbolics not default to |
Why a field? Why would that need to be changed at runtime? |
I actually don't think
is so bad. It disambigues |
Having a field means that when Catalyst generates an |
Why not just a query function |
So there would be a global MTK setting for what gets passed to Symbolics? Having it (optionally) stored in the system means that Catalyst can change it for Catalyst-generated ODESystems and such without impacting the display of a manually created ODESystem in the same notebook and/or REPL session. |
I see what you mean, so you do need a runtime overload. I think it could be fine to have a latexoptions piece added for customization. Though I'm not convinced there isn't a good general solution here. With Catalyst, it would be interesting though to use chemical notation |
Adding support for a user choosing |
Are you envisioning that |
Interesting. string("\\texttt", "{", sym, "}"))
convert(String, LaTeXString(string("\\texttt", "{", sym, "}")) fails with: ERROR: in Latexify.jl:
You are trying to create latex-maths from a `String` that cannot be parsed as
an expression: `\texttt{{\delta}t}`.
`latexify` will, by default, try to parse any string inputs into expressions
and this parsing has just failed.
If you are passing strings that you want returned verbatim as part of your input,
try making them `LaTeXString`s first.
If you are trying to make a table with plain text, try passing the keyword
argument `latex=false`. You should also ensure that you have chosen an output
environment that is capable of displaying not-maths objects. Try for example
`env=:table` for a latex table or `env=:mdtable` for a markdown table.
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] _latexraw(::Val{…}, i::String; kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:158
[3] _latexraw(i::String; parse::Bool, kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:148
[4] process_latexify(args::String; kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
[5] process_latexify
@ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
[6] latexraw
@ ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:58 [inlined]
[7] _latexequation(eq::String; starred::Bool, kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexequation.jl:5
[8] process_latexify(args::Num; kwargs::@Kwargs{})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
[9] process_latexify
@ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
[10] latexify(args::Num; kwargs::@Kwargs{})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:27
[11] latexify(args::Num)
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:25
[12] top-level scope
@ ~/Desktop/test.jl:107
caused by: ParseError:
# Error @ none:1:1
\texttt{{\delta}t}
╙ ── not a unary operator
Stacktrace:
[1] #parse#3
@ ./meta.jl:244 [inlined]
[2] parse
@ ./meta.jl:236 [inlined]
[3] parse(str::String; filename::String, raise::Bool, depwarn::Bool)
@ Base.Meta ./meta.jl:278
[4] parse
@ ./meta.jl:276 [inlined]
[5] _latexraw(::Val{…}, i::String; kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:155
[6] _latexraw(i::String; parse::Bool, kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:148
[7] process_latexify(args::String; kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
[8] process_latexify
@ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
[9] latexraw
@ ~/.julia/packages/Latexify/ieukI/src/latexraw.jl:58 [inlined]
[10] _latexequation(eq::String; starred::Bool, kwargs::@Kwargs{…})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexequation.jl:5
[11] process_latexify(args::Num; kwargs::@Kwargs{})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:49
[12] process_latexify
@ ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:40 [inlined]
[13] latexify(args::Num; kwargs::@Kwargs{})
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:27
[14] latexify(args::Num)
@ Latexify ~/.julia/packages/Latexify/ieukI/src/latexify_function.jl:25
[15] top-level scope
@ ~/Desktop/test.jl:107
Some type information was truncated. Use `show(err)` to see complete types. only LaTeXString(string("\\texttt", "{", sym, "}")) works. @gustaphe can you help us figure out a good solution here? |
I tried #1317 |
Yeah some |
Ok, that is exactly what I was trying to propose (with a user-customized one then stored in the system, or else the default is used). |
This could probably also be handled mostly on the Latexify side (cc @gustaphe), so users could also use the existing |
I guess this is just gustaphe's suggestion, but with |
I'm happy to help if there is some change in Latexify that could make this more general and configurable. I don't quite know the internals of this package well enough though, and I'm not sure I understand all of this thread. Let me know if you need anything specific. The philosophy of Latexify has mostly been that "people will know not to give variables multi-character names if they want them to latexify nicely", but I guess Symbolics sort of erodes that freedom of choice. I would also like to mention the |
This one I don't understand at all, no part of that expression should run |
I think I'd prefer adding the ability to have system-based customization via a SymbolicsLatexParameters object. My thought here is that for Catalyst I like the idea of having a different font for system names, but using regular math for the final symbol after the last separator (regardless of the symbol's length). With such a customization object we could add support for that here in Symbolics and Catalyst could then use it, while it wouldn't be possible with just uniformly setting a global font everything gets wrapped in. |
I would also be happy with that solution. Maybe a little more complicated to implement, but also more flexible. With suitable options it should be able to emulate a global wrapping font, for those that want it. I would also like the ability to distinguish system vs. variable names. |
capacitor
makes sense as code but does not make as much sense inside of latex output. When you look at it, it italics each of the letters which makes it look like its c times a times ..., which isn't quite right.Instead, this uses
\texttt{name}
in order to more clearly delineate a value which is a compound name. Using the fixed width text also looks quite "computer-y" so it has a nice connection toverbose
and is clearly a "computer variable" in the expression.