# $\TeX$ conversion, support for \hphantom

I'm trying to write some $\TeX$ conversion for a tensor with an upper and a lower index, but I would like the lower index to be displaced to the right by the width of the upper index. In (La)TeX I would write this as W^I_{\hphantom{I} J} ($W^I_{\hphantom{I} J}$). I tried to use the Invisible Mathematica command, which seems to be the counterpart of $\TeX$'s \phantom, but instead of getting a space I get the upper index repeated as a lower index.

Here's the code I'm using now, which doesn't quite work:

w /: MakeBoxes[w[i_,j_],fmt_:StandardForm] := (*BUGGY*)
SubsuperscriptBox[MakeBoxes[W,fmt], RowBox[{MakeBoxes[Invisible[i],fmt], MakeBoxes[j,fmt]}], MakeBoxes[i,fmt]]


This does work in the notebook front-end, but not when converting to (La)TeX. What is the best way to achieve the result I'm after?

-

Please let me know if I didn't understand correctly. What if you used a Subscript wrapping a Superscript instead of a Subsuperscript?

Subscript[Superscript[x, 4], 2]


Its TeXForm gives x^4{}_2. Does that render as you want?

EDIT

Does this work?

yourTeXForm[expr_] :=
ConvertTeXBoxesToTeX[ToBoxes[expr],
"BoxRules" -> {StyleBox[t_,
op : OptionsPattern[]] /; (ShowContents /.
FilterRules[{op}, ShowContents]) === False :>
"\\hphantom{" <> ConvertTeXBoxesToTeX[t] <> "}" }]

parseIndices[ind_List] :=
Apply[Sequence,
Row /@ Riffle @@@
Transpose@
ReplaceList[
Partition[ind, 2]\[Transpose], {b___, i : {__}, a___} :> {b,
Invisible /@ i, a}]]

ssp= Subsuperscript[x, parseIndices[Range[10]]]


yourTeXForm[ssp]


"x_{\hphantom{1}1\hphantom{3}3\hphantom{5}5\hphantom{7}7\\ hphantom{9}9}^{2\hphantom{2}4\hphantom{4}6\hphantom{6}8\hphantom{\ 8}10\hphantom{10}}"

-
Yes, I thought about doing it this way, but I would say this is a hack and there should be something more appropriate built-in in Mathematica (but it looks like there isn't anything better at the moment). I have to test this more, but it looks to me like the alignment of indices may come out wrong in some circumstances. –  Sidious Lord Jun 9 '12 at 15:12
@SidiousLord, why does it feel like a hack? To me, that I'm not used to latex so much, inserting an invisible version of your superscript in the subscript feels more like a hack –  Rojo Jun 9 '12 at 15:40
As I said, in \TeX\ the placement of the indices will be a bit off. For example, if I have a big symbol on which I want to put indices, if I follow your method I get $\int_{a}^{b}{}_{c}{}^{d}$ whereas with \hphantom' I get $\int_{a \hphantom{b} c}^{\hphantom{a} b \hphantom{c} d}$. –  Sidious Lord Jun 9 '12 at 15:54
@SidiousLord if you haven't, search for ConversionRules in the site, and check out if you can add your own rules to export some invisible box into a \hphantom. I haven't used ConversionRules` yet –  Rojo Jun 9 '12 at 16:07
Thanks for the suggestion. I'll try to see if I can make it work. –  Sidious Lord Jun 9 '12 at 16:20