# How can I replace every global instance of “x[2]” with “x_2”

Thanks to Bruno for suggesting that for my specific case, where my results come from Mathematica, it's simply much easier to change the output in there using trivial replacement rules. However, this stands alone as a general question.

This is a slightly different question to what I've seen before, where I can use something like \newcommand{\ga}{\gamma} to make shortcuts of things using \.

I have a series of LONG equations with stuff like x[2] or a[3] in them. I want to be able to always replace any instance of them with something else, like a command that takes a string and replaces it like something similar to

\newcommand{x[2]}{x_2}

and

\newcommand{a[2]}{\beta}

Ideally, this would take account of the number inside, but I could easily just do it for all instances that arise. Any guidance would be great (my document class is Report)

• Sort of sounds like translating math equations from one syntax to another, which reminded me of my very strange answer here: tex.stackexchange.com/questions/332012/…. Not that it will help you in the present case, without significant rework. – Steven B. Segletes Mar 26 at 15:46
• Do you absolutely need to convert as you typeset, or can you convert your source? If the latter, do you think it would be possible to unambiguously define all the situations in which this syntax would occur? – Chris H Mar 26 at 15:48
• @ChrisH that's exactly what I'm hoping for. I'm copying some parts of equations from Mathematica, and is written in terms of coefficients like x[2] for example, where I avoided subscripts in my code. I now want to save myself effort and simply define x[2] = x_2 in Latex, since it'll only ever occur within an equation. – Brad Mar 26 at 15:50
\end{document}
`