$\TeX$ conversion, support for \hphantom

6

1

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?

Sidious Lord

Posted 2012-06-09T12:02:59.850

Reputation: 273

Answers

3

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

Rojo

Posted 2012-06-09T12:02:59.850

Reputation: 40 993

1Yes, 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 – 2012-06-09T15:12:12.853

@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 – 2012-06-09T15:40:02.433

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 – 2012-06-09T15:54:08.417

@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 – 2012-06-09T16:07:41.047

Thanks for the suggestion. I'll try to see if I can make it work. – Sidious Lord – 2012-06-09T16:20:03.210

@Rojo, I don't think ConversionRules is what is needed here since they just tell Mathematica how to export a styled cell to say TeX or HTML. I was having a similar problem in which mma didn't give me the correct TeX transformation. I think this is all done with ConvertTeXBoxesToTeX but I have no idea what this function wants so that we can get an \hphantom out of it.

– jmlopez – 2012-06-09T19:33:48.870

@Rojo: No, the TeX W^I{}_J is different from the indicated construction with \hphantom in that the subscript of the former is displaced too far horizontally. – murray – 2012-06-09T20:27:51.970

@jmlopez, see my edit, something like that? – Rojo – 2012-06-09T21:26:25.720

@SidiousLord you too check the edit – Rojo – 2012-06-09T21:26:41.100

@Rojo, That seems to be doing the job. I just found out about the option "BoxRules" but I still can't make it work correctly. Not sure if I should make it another question, but Sidious Lord question has gotten me to think more about how to write proper rules to convert from mms to tex. Consider the following: RawBoxes@ConvertTeXTeXToBoxes[ "\\int_{a\\hphantom{b} c}^{\\hphantom{a} b\\hphantom{c} d}"]. How would you make it write it correctly in mma? You know what, I am making it another question. – jmlopez – 2012-06-09T21:36:50.247

@Rojo, I have accepted your answer. It seems that Invisible sets an option ShowContents -> False and then you use "BoxRules" to search for it and replace it with \hphantom. – Sidious Lord – 2012-06-10T07:05:24.887