I looked around and couldn't find the answer to this anywhere, so I'm sorry if this is a bad question - I'm pretty new to mathematica. I wrote a program to help me compute some annoying series expansions, and the output is pretty ugly, for example:

$\epsilon ^2 \left(\left(a_{5,1} a_{6,1}+a_{1,1} \left(a_{2,1}+a_{6,1}\right)\right) X_{1,2}+\left(a_{5,1} \left(a_{9,1}+a_{10,1}\right)+a_{1,1} \left(a_{3,1}+a_{9,1}+a_{10,1}\right)\right) X_{1,3}+\left(a_{5,1} \left(a_{7,1}+a_{8,1}\right)+a_{1,1} \left(a_{4,1}+a_{7,1}+a_{8,1}\right)\right) X_{1,4}+\left(a_{6,1} \left(a_{9,1}+a_{10,1}\right)+a_{2,1} \left(a_{3,1}+a_{9,1}+a_{10,1}\right)\right) X_{2,3}+\left(a_{6,1} \left(a_{7,1}+a_{8,1}\right)+a_{2,1} \left(a_{4,1}+a_{7,1}+a_{8,1}\right)\right) X_{2,4}+a_{3,1} \left(a_{4,1}+a_{7,1}+a_{8,1}\right) X_{3,4}\right)+\epsilon \left(X_3 \epsilon a_{3,2}+X_4 \epsilon a_{4,2}+X_1 \left(\epsilon a_{1,2}+\epsilon a_{5,2}+a_{1,1}+a_{5,1}\right)+X_2 \left(\epsilon a_{2,2}+\epsilon a_{6,2}+a_{2,1}+a_{6,1}\right)+X_4 \epsilon a_{7,2}+X_4 \epsilon a_{8,2}+X_3 \epsilon a_{9,2}+X_3 \epsilon a_{10,2}+X_3 a_{3,1}+X_4 a_{4,1}+X_4 a_{7,1}+X_4 a_{8,1}+X_3 a_{9,1}+X_3 a_{10,1}\right)$

I'm wondering if there's a way to have it output this in a more readable way, for example, something like:

$X_{1}*(coefficients)\\ X_{2}*(coefficients)\\ ...\\ X_{1,2}*(coefficents)\\ etc...$

Is there a way to do so? Thanks!

  • 1
    $\begingroup$ Please include the Mathematica code that produced your output for the convenience of those who wish to help. $\endgroup$
    – Mr.Wizard
    Mar 5, 2013 at 22:17

1 Answer 1


One simple way would be something like this:

      Collect[Sum[Expand[(a - b)^i] X[i, j], {i, 3}, {j, 3}], _X] /.
       y_*z_X :> Row[{Style[z, Bold, Red, 22] , "*(", y, ")"}]]

Mathematica graphics

  • $\begingroup$ So in this case, X[1,1] is my $X_{1}$? And I'm slightly confused how to modify this code to match my case - I'm not sure what would go in for "List", for example, as I have no list. $\endgroup$ Mar 5, 2013 at 22:38
  • $\begingroup$ So what do you have? Subscript? Please enter a Mathematica expression into the question. Please also read a bit about Mathematica, FullForm, Apply, Plus, List, etc. Or follow one of the online tutorials or go to a training or some such. $\endgroup$ Mar 5, 2013 at 22:54
  • $\begingroup$ Yes, I'm just using subscripts. The subscripts do not correspond to a matrix or anything (I see now where that confusion may have come from), but rather each $X_{i}$ is a vector field, and each $X_{i,j}$ is a Lie bracket of two vector fields. I've done some tutorials and read about Mathematica, I just never found what I was looking for. $\endgroup$ Mar 5, 2013 at 22:58
  • $\begingroup$ Just post your program to do the series expansions. Tthen people will help you. $\endgroup$ Mar 6, 2013 at 8:35

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.