From Tuan Anh Nguyen, 9 Months ago, written in Python for S60.
Embed
  1. #This program reads the input file, change its labels, write the result to an output file
  2. file_name_in='MLP.tex'     # input file name
  3. file_name_out="Output.tex" # output file name.
  4.  
  5. # in this example the new label will be a01,a02,..., a20, b01,...,b20, ... ,h01,...., h20
  6. #prefixes=['a','b','c','d','e','f','g','h'];
  7. #affixes= [20,20,20,20,20,20,20,20,20,20];
  8.  
  9. prefixes=['a','b','c'];
  10. affixes= [50,50,50];
  11.  
  12. nr_zero=2# if you want to fill in the label by 0, e.g. instead a1, it should be a01
  13. import string
  14.  
  15. def labelsOf(text):
  16.         beg=0;end=0;l=len(text);
  17.         labels=[];
  18.         while (beg<= l):
  19.                 beg=text.find("\label{",end)+7
  20.                 if (beg==6):
  21.                         break;
  22.                 end=text.find('}',beg)
  23.                 labels.append(text[beg:end])
  24.         return labels
  25.  
  26. def designedLabels(prefixes, affixes):
  27.         labels=[];
  28.         l=len(prefixes);
  29.         for i in range(0,l):
  30.                 for j in range(0,affixes[i]):
  31.                         labels.append(prefixes[i]+str(j).zfill(nr_zero))
  32.         return labels;
  33.        
  34. def changeLabels(text,newLabels):
  35.         oldLabels= labelsOf(text);
  36.         l=len(oldLabels)
  37.         for i in range(0,l):
  38.                 for u in ['{',',']:
  39.                         for v in ['}',',']:
  40.                                 text=text.replace(u+oldLabels[i]+v,u+newLabels[i]+v);          
  41.         return text;
  42.  
  43. file_in = open(file_name_in, 'r')
  44. text = file_in.read()
  45. file_in.close()
  46.  
  47.  
  48.  
  49. if (len(labelsOf(text))>len(designedLabels(prefixes, affixes)) ):
  50.         print 'Warning: you do not give enough labels... You should give more than '+ str(len(labelsOf(text)))+ ' labels.';
  51. else:
  52.         text=changeLabels(text,designedLabels(['MMMMMMMMM'],[1000]));
  53.         text=changeLabels(text,designedLabels(prefixes,affixes));
  54.         file_out = open(file_name_out, "w")
  55.         file_out.write(text)
  56.         file_out.close()
  57.         print 'Labels changed!'
  58.  
  59.