TeXForm and Symbolize



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:

Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]];


Can anyone think of a better solution?



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



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:

            Notation`NotationMakeBoxes[sym_Symbol, StandardForm],
    ] :>
        SubscriptBox[x_, y_]
] := (
        sym /: MakeBoxes[sym, TraditionalForm] = 

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:

Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]]

Now some strange test expression:

f[a + b]Sin[c]

in FullForm gives:


in TeXForm, before evaluating updateSubscriptTraditionalForm, gives:


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:


So result you're getting is a "correct" translation to TeX of above expression.

(* \text{c$\mathsym{\UnderBracket}$Subscript$\mathsym{\UnderBracket}$t} *)

To get desired result you can add formatting rule to this expression using my TeXUtilities package.

Format[c\[UnderBracket]Subscript\[UnderBracket]t, TeXForm] = TeXVerbatim["c_t"];

(* c_t *)


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


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

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


Subscript[c, t] //TeXForm


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