Skip to main content

bugs - Incompatibility of Row and TeXForm


Bug introduced in 9.0 and persisting through 11.0




Update: Since no one has been able to give a reason for this change in behavior or provide a solution I am tagging this as a bug. Please report it to Wolfram Support if this affects your use of Mathematica.




In Mathematica 7 Row and TeXForm work harmoniously:



{x^a, Sqrt@b, ArcSin[c]} // Row // TeXForm


x^a\sqrt{b}\sin ^{-1}(c)

As LaTeX:



xa√bsin−1(c)



Unfortunately this is broken in more recent versions. Mathematica 10 returns:




TeXForm::unspt: TeXForm of TemplateSlotSequence[1,] is not supported. >>



This error was reported in version 9 as well.


An attempt at a workaround is to substitute Grid for Row but the LaTeX code generated is different:


{{x^a, Sqrt@b, ArcSin[c]}} // Grid // TeXForm

enter image description here


And it formats differently:




xa√bsin−1(c)



The documentation for TeXForm indicates that it is extensible with rules for Format but I could not find a rule that worked. While I can manually convert a single Row as follows:


StringJoin[ToString /@ TeXForm /@ {x^a, Sqrt@b, ArcSin[c]}]


x^a\sqrt{b}\sin ^{-1}(c)

(Though note that one must copy as Plain Text.)


However attempting to make this a Format rule results in double-conversion and I get this:



enter image description here




Is this indeed a bug as I believe?


What is the best work-around for the problem, preferably automatic?



Answer



Short answer


The TLDR answer is:


System`Convert`CommonDump`templateBoxToDisplay = BoxForm`TemplateBoxToDisplayBoxes

A more long-winded answer follows.



Background


The basic approach used by TeXForm is to create TraditionalForm boxes, and then to convert those boxes into a TeXForm string. The basic internal function that TeXForm uses to convert boxes into a string is System`Convert`TeXFormDump`maketex. For example, consider BesselJ[n, x]. The TraditionalForm boxes are:


MakeBoxes[BesselJ[n, x], TraditionalForm]


TemplateBox[{"n", "x"}, "BesselJ"]



Notice how System`Convert`TeXFormDump`maketex is used to convert the above boxes into a string:


TracePrint[
ToString[BesselJ[n, x], TeXForm],

_System`Convert`TeXFormDump`maketex,
TraceInternal->True
]


System`Convert`TeXFormDump`maketex[TemplateBox[{n,x},BesselJ]]


System`Convert`TeXFormDump`maketex[RowBox[{SubscriptBox[J,n],(,x,)}]]


...


"J_n(x)"




By default, System`Convert`TeXFormDump`maketex uses System`Convert`CommonDump`ExpandTemplateBoxes to convert TemplateBox objects into normal boxes:


TracePrint[
System`Convert`TeXFormDump`maketex[TemplateBox[{"n","x"},"BesselJ"]],
_System`Convert`CommonDump`ExpandTemplateBoxes
]


System`Convert`CommonDump`ExpandTemplateBoxes[TemplateBox[{n,x},BesselJ]]


System`Convert`CommonDump`ExpandTemplateBoxes[TemplateBox[{n,x},BesselJ],Display]


"J_n(x)"




and:


System`Convert`CommonDump`ExpandTemplateBoxes[TemplateBox[{"n","x"},"BesselJ"]]


RowBox[{SubscriptBox["J", "n"], "(", "x", ")"}]



Summarizing, TeXForm does the following:



  1. Create TraditionalForm boxes


  2. Convert TemplateBox objects into normal boxes

  3. Convert normal boxes into a TeXForm string


The problem with Row


So, where do things go wrong with Row objects? In Mathematica version 7 and earlier, Row objects were formatted as RowBox objects directly, and so there was no need to convert TemplateBox objects into normal boxes. However, starting in versions 8 or 9, Row objects were formatted as TemplateBox objects. For example, in M9 (I don't have ready access to earlier versions):


MakeBoxes[Row[{x, y}], TraditionalForm]


TemplateBox[{"x", "y"}, "RowDefault"]




One possible fix for this issue is to restore the old functionality of formatting Row objects as normal boxes, and this is what the global variable BoxForm`$UseTemplateSlotSequenceForRow controls. I don't like this solution, as it means that Row objects will never use TemplateBox, which I think is a shame (I think TemplateBox offers both speed and memory advantages). Also, it only fixes one symptom of the issue, and not the underlying problem. So, let's figure out the underlying problem.


TemplateSlotSequence


Here is the TemplateBox format for a Row object:


boxes = MakeBoxes[Row[{x, y}, None], TraditionalForm]


TemplateBox[{"x", "y"}, "RowNoSeparators"]



(I used a Row object with a separator for reasons I will explain later). These are the normal boxes that TeXForm creates for this TemplateBox:


System`Convert`CommonDump`ExpandTemplateBoxes[

TemplateBox[{"x","y"}, "RowNoSeparators"]
]


RowBox[{TemplateSlotSequence[1]}]



Notice the appearance of a TemplateSlotSequence object. This kind of object is not supported by System`Convert`TeXFormDump`maketex. Also, just like other Slot objects, the TemplateSlotSequence object only has meaning when it is inside of a Function object, so the above output is not a valid form of normal boxes. This is the reason for the error that gets reported:


ToString[Row[{x, y}, None], TeXForm]



TeXForm::unspt: TeXForm of TemplateSlotSequence[1] is not supported.


""



Single argument Row


The above error doesn't happen for single argument Row because System`Convert`TeXFormDump`maketex was hacked to support it. Compare:


TracePrint[
System`Convert`TeXFormDump`maketex[TemplateBox[{"x", "y"}, "RowNoSeparators"]],
_System`Convert`CommonDump`ExpandTemplateBoxes
]



System`Convert`CommonDump`ExpandTemplateBoxes[TemplateBox[{x,y},RowNoSeparators]]


System`Convert`CommonDump`ExpandTemplateBoxes[TemplateBox[{x,y},RowNoSeparators],Display]


TeXForm::unspt: TeXForm of TemplateSlotSequence[1] is not supported.


""



with:


TracePrint[
System`Convert`TeXFormDump`maketex[TemplateBox[{"x", "y"}, "RowDefault"]],
_System`Convert`CommonDump`ExpandTemplateBoxes

]


"xy"



Notice that the "RowDefault" TemplateBox never gets processed by System`Convert`CommonDump`ExpandTemplateBoxes because System`Convert`TeXFormDump`maketex has a special DownValues for this case.


Non-Row TemplateSlotSequence issues


The failure to support TemplateSlotSequence is not restricted to Row objects. For example:


ToString[BellY[a, b, {c, d}], TeXForm]



TeXForm::unspt: TeXForm of TemplateSlotSequence[3,,] is not supported.


"Y_{a,b}()"



ToString[WignerD[{j, m1, m2}, \[Theta]], TeXForm]


TeXForm::unspt: TeXForm of TemplateSlotSequence[4,,] is not supported.


"D_{\text{m1},\text{m2}}^j()"




So, changing the formatting of Row so that it doesn't use a TemplateBox is not really sufficient, it won't fix the above errors.


Fix


The function used by TeXForm to convert TemplateBox objects into normal boxes is System`Convert`CommonDump`templateBoxToDisplay:


TracePrint[
System`Convert`CommonDump`ExpandTemplateBoxes[TemplateBox[{"x", "y"}, "RowNoSeparators"]],
_System`Convert`CommonDump`templateBoxToDisplay,
TraceInternal->True
]



System`Convert`CommonDump`templateBoxToDisplay[TemplateBox[{x,y},RowNoSeparators]]


RowBox[{TemplateSlotSequence[1]}]



Instead of using System`Convert`CommonDump`templateBoxToDisplay one could use BoxForm`TemplateBoxToDisplayBoxes:


template = TemplateBox[{"x","y"},"RowNoSeparators"];

System`Convert`CommonDump`templateBoxToDisplay[template]
BoxForm`TemplateBoxToDisplayBoxes[template]



RowBox[{TemplateSlotSequence[1]}]


RowBox[{"x", "y"}]



Note that the latter function produces acceptable boxes. I don't know why TeXForm uses its own custom function to convert TemplateBox objects. So, a simple fix is to redefine System`Convert`CommonDump`templateBoxToDisplay:


System`Convert`CommonDump`templateBoxToDisplay = BoxForm`TemplateBoxToDisplayBoxes;

Now, TeXForm will work properly with any object whose format uses a TemplateSlotSequence TemplateBox:


ToString[BellY[a, b, {c, d}], TeXForm] //OutputForm
ToString[WignerD[{j, m1, m2}, θ], TeXForm] //OutputForm
ToString[Row[{a, b}, ","], TeXForm] //OutputForm



Ya,b(c,d)


Djm1,m2(0,θ,0)


a,b



Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...