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:

<< xAct`xTensor`


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:


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:


However, if you try to do this for the Ricci tensor, then there are multiple contracted indices in the connection squared terms:


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:


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.

  • 2
    $\begingroup$ 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. $\endgroup$
    – Nasser
    May 16 at 11:55
  • $\begingroup$ Sorry I forgot to add I am using xTensor. $\endgroup$
    – Tim
    May 16 at 12:02
  • $\begingroup$ Thanks a lot, this seems to solve my issue. The output is slightly different but I think I can work with this. $\endgroup$
    – Tim
    May 16 at 12:07
  • $\begingroup$ 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? $\endgroup$
    – Tim
    May 16 at 13:41
  • 1
    $\begingroup$ Apparently there is a command ScreenDollarIndices that does precisely what I am looking for... $\endgroup$
    – Tim
    May 17 at 21:57

1 Answer 1


It is standard in xTensor work (and in all of xAct, actually) to set at the beginning of the session

$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].


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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