# Counting multiple $-sign string indices in output and replacing them by normal indices I'm using the package xTensor in xAct. My issue is that inside a module indices are replaced by strings of the form$##### which make the final output unreadable. In a related post Print expressions using local variables in Module without Dollar sign: is that possible there is an answer to the case where there is a single contracted index, but I need a solution for when there are multiple indices. Let me sketch the idea here and give two examples to show the difference.

First we load the package (which can easily be downloaded from the xAct website) and define a manifold with a metric:

<< xActxTensor

DefManifold[M,D,IndexRange[a,f]]
DefMetric[-1,G[-a,-b],CD]


This defines the Christoffel symbol and Ricci tensor of the metric G and you can obtain the explicit expressions in terms of the metric like so:

ChristoffelCD[c,-a,-b]//ChristoffelToMetric
RicciCD[-a,-b]//RiemannToChristoffel//ChristoffelToMetric


The problem is that the output of these lines contain contracted indices which are replaced with something like \$##### because they are redefined inside the module. I want the output to contain just a,b,c,d,e,f, not the dollar signs. The solution from the linked post is to define the following function (horizontal scroll): dollars[expr_] := Module[{}, StringReplace[ToString[expr, FormatType -> StandardForm], c : WordCharacter ~~ "$" ~~ DigitCharacter .. :> c]];


This function does the job for the Christoffel connection, since it only involves one contraction. For example it replaces f\$##### by f: dollars@(ChristoffelCD[c,-a,-b]//ChristoffelToMetric)  However, if you try to do this for the Ricci tensor, then there are multiple contracted indices in the connection squared terms: RicciCD[-a,-b]//RiemannToChristoffel  The squared connection terms contain two contractions denoted with the indices f\$34123 and f\$91232 in the output (the numbers are arbitary). If you then apply the "dollars" function both these indices are replaced by f, so you end up with an object with more than two f indices in some terms, which doesn't make sense: dollars@(RicciCD[-a,-b]//RiemannToChristoffel)  My question is: how do I generalize the function dollars[expr_] to count the number of times a \$-sign index has been used, and then return an expression with no duplicate indices?

Any help is greatly appreciated, cheers.

• btw, when you post code, it should be something that one can run. It seems you are using some package, but you do not even mention that. May 16 at 11:55
• Sorry I forgot to add I am using xTensor.
– Tim
May 16 at 12:02
• Thanks a lot, this seems to solve my issue. The output is slightly different but I think I can work with this.
– Tim
May 16 at 12:07
• There is a technical difficulty I don't know how to solve. When I use the function str@ from the other post, then indices such as f\$23423 and f\$92929 are both replaced by f, so my result contains doubled indices. It would be great if the function knows to replace one by f and the other by e. Does someone know a quick fix for this?
– Tim
May 16 at 13:41
• Apparently there is a command ScreenDollarIndices that does precisely what I am looking for...
– Tim
May 17 at 21:57

$PrePrint = ScreenDollarIndices;  In this way the results will contain all those "dollar indices" (which are fundamental to avoid index collisions) but you will not see them in output. Use InputForm if you want to see the dollar indices of an expression. If at some point you really want to change the actual indices used by an expression, evaluate ScreenDollarIndices on it manually, independently of whether $PrePrint was set or not.
To remove the setting of $PrePrint use Unset[$PrePrint].