I would like to use TeXForm to generate LaTeX output as usual. However, I use subscripted variables such as c_t. Unfortunately, when using Symbolize from the package Notation for this end, TeXForm does not work as expected. While I expected output such as c_t, I receive:
Needs["Notation`"];
Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]];
TeXForm[c_t]
\text{c$\mathsym{\UnderBracket}$Subscript$\mathsym{\UnderBracket}$t}.
Can anyone think of a better solution?
Thanks.
Answer
When you load Notation` package it automatically assigns definitions to MakeExpression and MakeBoxes function. Those definitions call NotationMakeExpression and NotationMakeBoxes functions respectively.
After symbolizing expression with a pattern, special definition is assigned to NotationMakeExpression function. This definition forces Mathematica to perform some tasks when expression, matching symbolized pattern, is encountered. A symbol, with name reflecting symbolized expression, is constructed, then special rules for this symbol are assigned to NotationMakeBoxes. These rules tell Mathematica what is box representation of constructed symbol in StandardForm.
Since only definitions for StandardForm are added, specially constructed symbol looks like symbolized expression in StandardForm and possibly other forms that use StandardForm boxes, but not in other forms.
TeXForm uses boxes from TraditionalForm not StandardForm. To solve your problem in general way for all symbolized subscripted expressions you could inspect NotationMakeBoxes definition and assign correct TraditionalForm to all symbols that have SubscriptBox assigned to StandardForm.
Let's define some functions that will do this automatically. We start with function that converts boxes in StandardForm to boxes in TraditionalForm:
SetAttributes[standardToTraditionalBoxes, HoldAllComplete]
standardToTraditionalBoxes[boxes_] :=
Function[expr, MakeBoxes[expr, TraditionalForm], HoldAllComplete] @@
MakeExpression[boxes, StandardForm]
Now a function converting NotationMakeBoxes down value to proper MakeBoxes definition for TraditionalForm:
addSubscriptTraditionalForm[
Verbatim[HoldPattern][
Verbatim[Condition][
Notation`NotationMakeBoxes[sym_Symbol, StandardForm],
_
]
] :>
SubscriptBox[x_, y_]
] := (
sym /: MakeBoxes[sym, TraditionalForm] =
SubscriptBox[standardToTraditionalBoxes[x],standardToTraditionalBoxes[y]]
)
And final function traversing down values:
updateSubscriptTraditionalForm[] :=
Scan[addSubscriptTraditionalForm, DownValues[Notation`NotationMakeBoxes]]
updateSubscriptTraditionalForm function should be evaluated after all symbolized expressions were read by Mathematica and before using TeXForm.
Small test
Load Notation` and symbolize all expressions with subscripts:
Needs["Notation`"]
Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]]
Now some strange test expression:
f[a + b]Sin[c]
in FullForm gives:
f\[UnderBracket]LeftBracket\[UnderBracket]a\[UnderBracket]Plus\[UnderBracket]b\[UnderBracket]RightBracket\[UnderBracket]Subscript\[UnderBracket]Sin\[UnderBracket]LeftBracket\[UnderBracket]c\[UnderBracket]RightBracket
in TeXForm, before evaluating updateSubscriptTraditionalForm, gives:
\text{f$\mathsym{\UnderBracket}$LeftBracket$\mathsym{\UnderBracket}$a$\mathsym{\UnderBracket}$Plus$\mathsym{\UnderBracket}$b$\mathsym{\UnderBracket}$RightBracket$\mathsym{\UnderBracket}$Subscript$\mathsym{\UnderBracket}$Sin$\mathsym{\UnderBracket}$LeftBracket$\mathsym{\UnderBracket}$c$\mathsym{\UnderBracket}$RightBracket}
and after updateSubscriptTraditionalForm evaluation, TeXForm gives:
f(a+b)_{\sin (c)}
After using Symbolize ct looks in FullForm like this:
c\[UnderBracket]Subscript\[UnderBracket]t
So result you're getting is a "correct" translation to TeX of above expression.
c\[UnderBracket]Subscript\[UnderBracket]t//TeXForm
(* \text{c$\mathsym{\UnderBracket}$Subscript$\mathsym{\UnderBracket}$t} *)
To get desired result you can add formatting rule to this expression using my TeXUtilities package.
Import["https://raw.githubusercontent.com/jkuczm/MathematicaTeXUtilities/master/NoInstall.m"]
Format[c\[UnderBracket]Subscript\[UnderBracket]t, TeXForm] = TeXVerbatim["c_t"];
c\[UnderBracket]Subscript\[UnderBracket]t//TeXForm
(* c_t *)
Comments
Post a Comment