TeXForm and Symbolize

8

3

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<Ctrl>_t]<Shift+Enter>

\text{c$\mathsym{\UnderBracket}$Subscript$\mathsym{\UnderBracket}$t}.

Can anyone think of a better solution?

Thanks.

user191919

Posted 2015-01-19T17:55:44.860

Reputation: 527

Perhaps StringReplace? – Sjoerd C. de Vries – 2015-01-19T18:25:20.267

Could you provide an example please? – user191919 – 2015-01-19T18:27:02.697

StringReplace[yourSymbolizedVariable // ToString, StringExpression[b : __, "\[UnderBracket]Subscript\[UnderBracket]", e : __] :> StringJoin["Subscript[", b, ",", e, "]"]] // ToExpression // TeXForm – Sjoerd C. de Vries – 2015-01-19T23:48:34.637

Answers

6

Adding TraditionalForm for symbolized expressions

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)}

Old answer using TeXUtilities package

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 *)

jkuczm

Posted 2015-01-19T17:55:44.860

Reputation: 14 388

Can I tell it to consider this the general formatting rule for subscripts? – user191919 – 2015-01-20T18:36:19.257

@user191919 Unfortunately no. Format needs to assign formatting to specific symbols and after symbolization each expression with subscript is a separate symbol. But I've edited my answer and added different approach which should solve general problem. – jkuczm – 2015-01-20T23:49:07.637

5

As @jkuczm says, TeXForm is based on TraditionalForm. So, use the WorkingForm option to Symbolize to get it to work with TeXForm:

Needs["Notation`"]
Symbolize[ParsedBoxWrapper[SubscriptBox["_","_"]], WorkingForm->TraditionalForm]

Then:

Subscript[c, t] //TeXForm

c_t

Carl Woll

Posted 2015-01-19T17:55:44.860

Reputation: 112 778

This did the trick for me. For some reason the @jkuczm answer threw an error. – Sterling – 2021-01-26T19:57:54.350