8
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Perhaps StringReplace? $\endgroup$ Jan 19, 2015 at 18:25
  • $\begingroup$ Could you provide an example please? $\endgroup$
    – user191919
    Jan 19, 2015 at 18:27
  • $\begingroup$ StringReplace[yourSymbolizedVariable // ToString, StringExpression[b : __, "\[UnderBracket]Subscript\[UnderBracket]", e : __] :> StringJoin["Subscript[", b, ",", e, "]"]] // ToExpression // TeXForm $\endgroup$ Jan 19, 2015 at 23:48

2 Answers 2

6
$\begingroup$

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 *)
$\endgroup$
2
  • $\begingroup$ Can I tell it to consider this the general formatting rule for subscripts? $\endgroup$
    – user191919
    Jan 20, 2015 at 18:36
  • $\begingroup$ @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. $\endgroup$
    – jkuczm
    Jan 20, 2015 at 23:49
5
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ This did the trick for me. For some reason the @jkuczm answer threw an error. $\endgroup$
    – Sterling
    Jan 26, 2021 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.