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?

  • 1
    What about sed? – Ian Thompson Mar 25 '14 at 16:22
  • 2
    @IanThompson Indeed: sed 's/Q^{h}_{\([^}]*\)}/q\1/g' should do the trick. (Made minor correction.) – user10274 Mar 25 '14 at 16:25

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:-)

| 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. – user10274 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
  • [^}]* is more general. – user10274 Mar 25 '14 at 16:47

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.