# Using subscripted variables in the LaTeX output [closed]

When I run the following code in Mathematica:

  mat = {{x1,x2,x3,x6,x7,x8,x11,x12},{x3,x1,x2,x8,x6,x7,x11,x12},{x3,x2,x1,x8,x7,x6,x12,x11},{x2,x3,x1,x7,x8,x6,x11,x12},{x1,x3,x2,x6,x8,x7,x12,x11},{x2,x1,x3,x7,x6,x8,x12,x11},{x4,x4,x4,x9,x9,x9,x13,x13},{x5,x5,x5,x10,x10,x10,x14,x14}}

res = Det[mat];

Factor[res]


I get the following output.

$$-9 (x11-x12) (x2 x6-x3 x6-x1 x7+x3 x7+x1 x8-x2 x8)^2 (-2 x1 x10 x13-2 x10 x13 x2-2 x10 x13 x3+3 x10 x11 x4+3 x10 x12 x4-2 x14 x4 x6+2 x13 x5 x6-2 x14 x4 x7+2 x13 x5 x7-2 x14 x4 x8+2 x13 x5 x8+2 x1 x14 x9+2 x14 x2 x9+2 x14 x3 x9-3 x11 x5 x9-3 x12 x5 x9)$$

Formatted as $$\LaTeX$$, the output is:

-9 (\text{x11}-\text{x12}) (-\text{x1} \text{x7}+\text{x1} \text{x8}+\text{x2} \text{x6}-\text{x2} \text{x8}-\text{x3} \text{x6}+\text{x3} \text{x7})^2 ...

Question

Is there any way to get the $$\LaTeX$$ output to be formatted as subscripted variables instead?

-9 (x_{11}-x_{12}) (-x_1 x_7+x_1 x_8+x_2 x_6-x_2 x_8-x_3 x_6+x_3 x_7)^2...

I could not get TeXForm to work either.

• Does TeXForm do what you have in mind?
– Syed
Commented Mar 4, 2022 at 13:48
• @Syed: I did not think to give that a try - but will do so now. Nope, it returns -9 (\text{x11}-\text{x12}) (-\text{x1} \text{x7}+\text{x1} \text{x8}+\text{x2} \text{x6}-\text{x2} \text{x8}-\text{x3} \text{x6}+\text{x3} \text{x7})^2
– Moo
Commented Mar 4, 2022 at 13:48
– Syed
Commented Mar 4, 2022 at 13:54
• how does Mathematica supposed to know you want x1 output in latex to be x_1? how about if the input is xn is this supposed to come out in latex as x_n also? The Latex conversion now is the correct translation. Commented Mar 4, 2022 at 14:38
• @Nasser: I am asking generally because this is a recurring thing.
– Moo
Commented Mar 4, 2022 at 18:31

This is a possible workaround.

alist = ToExpression["x" <> ToString[#] & /@ Range[15]]
blist = Table[Subscript[x, i], {i, 1, 15}]


$$\left\{\text{x1}\to x_1,\text{x2}\to x_2,\text{x3}\to x_3,\text{x4}\to x_4,\text{x5}\to x_5,\text{x6}\to x_6,\text{x7}\to x_7,\text{x8}\to x_8,\text{x9}\to x_9,\text{x10}\to x_{10},\text{x11}\to x_{11},\text{x12}\to x_{12},\text{x13}\to x_{13},\text{x14}\to x_{14},\text{x15}\to x_{15}\right\}$$

mat = {{x1, x2, x3, x6, x7, x8, x11, x12}, {x3, x1, x2, x8, x6, x7,
x11, x12}, {x3, x2, x1, x8, x7, x6, x12, x11}, {x2, x3, x1, x7, x8,
x6, x11, x12}, {x1, x3, x2, x6, x8, x7, x12, x11}, {x2, x1, x3,
x7, x6, x8, x12, x11}, {x4, x4, x4, x9, x9, x9, x13, x13}, {x5, x5,
x5, x10, x10, x10, x14, x14}}
res = Det[mat];
expr = Factor[res]
expr /. rules // TeXForm


(*

-9 \left(x_2 x_6-x_3 x_6-x_1 x_7+x_3 x_7+x_1 x_8-x_2 x_8\right){}^2 \left(x_{11}-x_{12}\right) \left(-3 x_5 x_9 x_{11}+3 x_4 x_{10} x_{11}-3 x_5 x_9 x_{12}+3 x_4 x_{10} x_{12}+2 x_5 x_6 x_{13}+2 x_5 x_7 x_{13}+2 x_5 x_8 x_{13}-2 x_1 x_{10} x_{13}-2 x_2 x_{10} x_{13}-2 x_3 x_{10} x_{13}-2 x_4 x_6 x_{14}-2 x_4 x_7 x_{14}-2 x_4 x_8 x_{14}+2 x_1 x_9 x_{14}+2 x_2 x_9 x_{14}+2 x_3 x_9 x_{14}\right)

*)

As LateX:

$$-9 \left(x_2 x_6-x_3 x_6-x_1 x_7+x_3 x_7+x_1 x_8-x_2 x_8\right){}^2 \left(x_{11}-x_{12}\right) \left(-3 x_5 x_9 x_{11}+3 x_4 x_{10} x_{11}-3 x_5 x_9 x_{12}+3 x_4 x_{10} x_{12}+2 x_5 x_6 x_{13}+2 x_5 x_7 x_{13}+2 x_5 x_8 x_{13}-2 x_1 x_{10} x_{13}-2 x_2 x_{10} x_{13}-2 x_3 x_{10} x_{13}-2 x_4 x_6 x_{14}-2 x_4 x_7 x_{14}-2 x_4 x_8 x_{14}+2 x_1 x_9 x_{14}+2 x_2 x_9 x_{14}+2 x_3 x_9 x_{14}\right)$$

• I am surprised there is not an option for subscripted variables. Thanks!
– Moo
Commented Mar 4, 2022 at 14:18
• @Moo You can have indexed variables, and format their display however you like. It is just a very bad idea to use Subscript for variables. Commented Mar 4, 2022 at 15:21
• @rhermans: Understood, I only care about for the output as the text count is limited and having all those text items wastes a lot of space. As you can see, I did not use subscripted variables in the problem statement as I know that causes grief. Thanks.
– Moo
Commented Mar 4, 2022 at 15:33
• @Moo You can use ResourceFunction["SymbolToSubscript"][expr]. Commented Jul 19, 2022 at 20:31
• @E.Chan-López: Thanks - I will that a go.
– Moo
Commented Jul 19, 2022 at 20:36