# Apply Expand only to parts of expression

I have the following term:

term = (x AngleBracket[x (-x+Sum[v'[y[j]],j])])/AngleBracket[x^2]+Sum[v'[y[j]],j];
term //TeXForm


$$\frac{x \left\langle x \left(\sum _j v'(y(j))-x\right)\right\rangle }{\left\langle x^2\right\rangle }+\sum _j v'(y(j))$$

and want to apply Expand only to the part within the AngleBrackets. How can I do that?

The result for this simple example is supposed to be

(x AngleBracket[-x^2 + x Sum[v'[y[j]],j]])/AngleBracket[x^2] + Sum[v'[y[j]],j] //TeXForm


$$\frac{x \left\langle x \sum _j v'(y(j))-x^2\right\rangle }{\left\langle x^2\right\rangle }+\sum _j v'(y(j))$$

It is important to me that a solution should work for any example, no matter the depth of the expression etc.

• does Replace[expr, AngleBracket[a_] :> AngleBracket[Expand[a]],All] give what you need? – kglr May 30 at 13:49

I like using Composition (@*) for this kind of thing:
term /. AngleBracket -> AngleBracket @* Expand //TeXForm

$$\frac{x \left\langle x \sum _j v'(y(j))-x^2\right\rangle }{\left\langle x^2\right\rangle }+\sum _j v'(y(j))$$