TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I realize that this might not strictly be a LaTeX question but I have still decided to post the question here as other's might have the same problem on occasion.

I am working on a rather large document at the moment that contains a bunch of equations. Instead of writing them as:

    Q^{h}_{Con} = 0.15 \cdot Q^{h}_{Transm} + ...

I would rather do this:

\qCon = 0.15 \cdot \qTransm + ...

Now it is not hard to do this if you have that idea when you start writing on a document. In my case however I already have many pages written (including a nomenclature). Now I would like to replace all mentions of e.g. $Q^{h}_{Con}$ with $\qCon$. Doing this I could make sure that I don't have a typo somewhere and up with a $Q^{h}_{Com}$.

Going through all files on their own is way too complicated and the tex editor I am using only offers search and replace in all opened files. So I have tried to use the Terminal (on Mac) to do so. I have been able to use 'grep' to find all the instances of e.g. $Q^{h}_{Con}$ but could not figure out how to replace it with $\qCon$.

Anyone has an idea?

share|improve this question
What about sed? – Ian Thompson Mar 25 '14 at 16:22
@IanThompson Indeed: sed 's/Q^{h}_{\([^}]*\)}/q\1/g' should do the trick. (Made minor correction.) – Marc van Dongen Mar 25 '14 at 16:25
up vote 8 down vote accepted

something like

for i in *.tex
sed -i -e 's/Q^{h}_{\([a-zA-Z]*\)}/\\q\1 /g' $i

Probably does something. (Save a copy of your files first:-)

share|improve this answer
sed -i.bak ... is good for back ups :) – cmhughes Mar 25 '14 at 16:32
@cmhughes yes although I never trust it, I'd zip up the directory first:-) – David Carlisle Mar 25 '14 at 16:36
That doesn't work as it only affects Q^{h}_{Con}s. – Marc van Dongen Mar 25 '14 at 16:37
@MarcvanDongen hm seems like I only read the last line of the question in detail:-) – David Carlisle Mar 25 '14 at 16:40
@MarcvanDongen fixed thanks – David Carlisle Mar 25 '14 at 16:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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