1
$\begingroup$

I have

eqns = TraditionalForm[HoldForm[Subscript[W, #][#] = ρ^s[#] Subscript[ω, #] ρ^-s[#]],] & /@ Range[4]

which (with eqns//TableForm) give me

$$ W_1(1)=\rho ^{s(1)} \omega _1 \rho ^{-s(1)}\\ W_2(2)=\rho ^{s(2)} \omega _2 \rho ^{-s(2)}\\ W_3(3)=\rho ^{s(3)} \omega _3 \rho ^{-s(3)}\\ W_4(4)=\rho ^{s(4)} \omega _4 \rho ^{-s(4)} $$

as I expect. But I need to substitute evaluations of s for various argument values. When I do this, for many values of s I get what I expect; but for others, I'm failing the get substitutions I need, or get odd reformatting that I don't expect. For example if s[#]& /@ {1,2,3,4} happens to be {0,23,10,-1}, I get

$$ W_1(1)=\rho ^0 \omega _1 \rho ^0\\ W_2(2)=\rho ^{23} \omega _2 \rho ^{-23}\\ W_3(3)=\rho ^{10} \omega _3 \rho ^{-10}\\ W_4=\frac{\rho ^{-(-1)} \omega _4}{\rho } $$

when what I need is

$$ W_1(1)=\omega _1\\ W_2(2)=\rho ^{23} \omega _2 \rho ^{-23}\\ W_3(3)=\rho ^{10} \omega _3 \rho ^{-10}\\ W_2(2)=\rho ^{-1} \omega _2 \rho ^{1}\\ $$

How can I get Mathematica the make the correct substitutions?

$\endgroup$
1
  • $\begingroup$ Much worse, Mathematica does not correctly copy any of these expressions as LaTeX. $\endgroup$
    – orome
    Mar 9, 2015 at 16:30

2 Answers 2

1
$\begingroup$
formatter[fab_, c_] := Block[{p1, p2, lhs, rhs, fn},
  rhs = If[fab == 0, Subscript[\[Omega], c], 
  If[fab < 0, fn = Abs[fab]; 
  HoldForm[\[Rho]^-f Subscript[\[Omega], c] \[Rho]^f] /. f -> fn, 
  HoldForm[\[Rho]^fab Subscript[\[Omega], c] \[Rho]^-fab]]];
  lhs = Subscript[W, fab][c];
  Replace[Evaluate[{lhs, rhs}], 
  List[a__, b__] :> HoldForm[Set[a , b]], Heads -> True]]

And then

 formatter[0,2]

or

formatter[-1,2]

etc.

Of course you can use formatter[-1,2]//TeXForm or //TraditionalForm if so desired.

$\endgroup$
1
$\begingroup$

Since you want Mathematica to perform some simplifications of your expression, I don't think this is purely formatting question.

Inferring from your desired simplifications of $\rho^i\omega\rho^{-i}$ for different $i$, it is supposed to be some form of multiplication, but different than built-in Times function.

I would start with defining desired properties of your multiplication. To get results from examples in question this should be enough:

ClearAll[myTimes]
myTimes[l___, 1, r___] := myTimes[l, r]
(* To make it more similar to multiplication, you could add something like: *)
(*myTimes[___,0,___]=0;
myTimes[l___,x_,x_,r___]:=myTimes[l,x^2,r]
myTimes[x_]:=x
myTimes[]=1;
SetAttributes[myTimes,{Flat,OneIdentity}]*)
(* but this is not needed for examples from question. *)

Now let's assign desired formatting to myTimes function:

ClearAll[toPowerBoxes];
SetAttributes[toPowerBoxes, HoldAllComplete]
toPowerBoxes[{expr___}, form_] :=
    List @@ Replace[
        HoldComplete[expr],
        {
            Power[x_, y_] :> SuperscriptBox[MakeBoxes[x, form], MakeBoxes[y, form]], 
            x_ :> MakeBoxes[x, form]
        },
        {1}
    ]
MakeBoxes[HoldPattern@myTimes[expr__], StandardForm] ^:=
    RowBox[{"myTimes", "[", RowBox[Riffle[toPowerBoxes[{expr}, StandardForm], ","]], "]"}]
MakeBoxes[HoldPattern@myTimes[expr__], TraditionalForm] ^:=
    With[{boxes = RowBox[toPowerBoxes[{expr}, TraditionalForm]]},
        InterpretationBox[boxes, myTimes[expr]]
    ]

Examples from question, with unevaluated s:

Subscript[W, #][#] == myTimes[ρ^s[#] , Subscript[ω, #], ρ^-s[#]] & /@ Range[4]
% // TableForm // TraditionalForm

$\{W_1[1] == \text{myTimes}[\rho^{s[1]}, \omega_1, \rho^{-s[1]}], ...\}$

(*Out[]//TraditionalForm=*)

$W_1(1)=\rho ^{s(1)}\omega _1\rho ^{-s(1)} \\ ...$

and when s evaluates to something:

Block[{s},
    s[1] = 0; s[2] = 23; s[3] = 10; s[4] = -1;
    Subscript[W, #][#] == myTimes[ρ^s[#] , Subscript[ω, #], ρ^-s[#]] & /@ Range[4]
]
% // TableForm // TraditionalForm
% // TableForm // TeXForm

$\{ W_1[1] == \text{myTimes}[\omega_1], W_2[2] == \text{myTimes}[\rho^{23}, \omega_2, \rho^{-23}], W_3[3] == \text{myTimes}[\rho^{10}, \omega_3, \rho^{-10}], W_4[4] == \text{myTimes}[\rho^{-1}, \omega_4, \rho] \}$

(*Out[]//TraditionalForm=*)

$W_1(1)=\omega _1 \\ W_2(2)=\rho ^{23}\omega _2\rho ^{-23} \\ W_3(3)=\rho ^{10}\omega _3\rho ^{-10} \\ W_4(4)=\rho ^{-1}\omega _4\rho$

(*Out[]//TeXForm=
\begin{array}{c}
 W_1(1)=\omega _1 \\
 W_2(2)=\rho ^{23}\omega _2\rho ^{-23} \\
 W_3(3)=\rho ^{10}\omega _3\rho ^{-10} \\
 W_4(4)=\rho ^{-1}\omega _4\rho  \\
\end{array}*)

You can also select StandardForm expression containing myTimes and use Copy As > LaTeX. Result is the same as output of TeXForm.

$\endgroup$

Your Answer

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

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