6
$\begingroup$

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?

$\endgroup$

1 Answer 1

3
$\begingroup$

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]

Mathematica graphics

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

EDIT

Does this work?

yourTeXForm[expr_] := 
 Convert`TeX`BoxesToTeX[ToBoxes[expr], 
  "BoxRules" -> {StyleBox[t_, 
       op : OptionsPattern[]] /; (ShowContents /. 
         FilterRules[{op}, ShowContents]) === False :> 
     "\\hphantom{" <> Convert`TeX`BoxesToTeX[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]]]

Mathematica graphics

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

$\endgroup$
11
  • 1
    $\begingroup$ 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. $\endgroup$ Jun 9, 2012 at 15:12
  • $\begingroup$ @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 $\endgroup$
    – Rojo
    Jun 9, 2012 at 15:40
  • $\begingroup$ 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}$. $\endgroup$ Jun 9, 2012 at 15:54
  • $\begingroup$ @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 $\endgroup$
    – Rojo
    Jun 9, 2012 at 16:07
  • $\begingroup$ Thanks for the suggestion. I'll try to see if I can make it work. $\endgroup$ Jun 9, 2012 at 16:20

Your Answer

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

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